Jump to content

[SOLVED] How to get the center of the window?


julia k

Recommended Posts

hello!

 

I'm struggling for last two days to find out a way to absolutely position an element on the center of the screen. Right now I'm using these two functions to get the width and height of the window:

 

        getWidth : function()
        {
            var Width = 0;
            if (typeof(window.innerWidth) == 'number') {
                return Width = window.innerWidth;
            }
            else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                return Width = document.documentElement.clientWidth;
            }
            else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                return Width = document.body.clientWidth;
            }
            return Width;
        },
        getHeight : function()
        {
            var Height = 0;
            if (typeof(window.innerWidth) == 'number') {
                return Height = window.innerHeight;
            }
            else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                return Height = document.documentElement.clientHeight;
            }
            else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                return Height = document.body.clientHeight;
            }
            return Height;
        }

 

This code works great if the window doesn't have a scrollbar, but if there is a scrollbar and, let's say that I have a button which when clicked will display a popup window or a div in the center of the screen the popup window or the div will not be visible unless I scroll the window to the top...

 

Can anyone show me how to display an object in the center of the screen so I don't have to scroll the window back to top?

 

Thank you in advance :)

Link to comment
Share on other sites

After taking some time to reverse-engineer the code, here is all you need to center a div on the page:

#content
    {
position: absolute;

left: 50%;
width: 250px;
margin-left: -125px;

top: 50%;
height: 70px;
margin-top: -35px;
    }

 

If you need a differnt size div, just ensure the margins are negative values for 1/2 the width and height.

Link to comment
Share on other sites

wow! Thank you very much, mjdamato, that really helped!

 

I have a great experience with CSS, I mean I can do a lot of things with it, but i have an other question related to that code: Why is there the need for margin-left and margin top when the left and top attributes are already set? because this is why my code wasn't working in the first place. Can you please explain me why? I'd hate to just move on without actually knowing the answer to this question.

 

Thanks again :)

 

Link to comment
Share on other sites

When positioning an element using absolute positioning it is done so relative to the top-left position of the element. So, if you used top:50% and left:50%, it will set the top-left corder of the element to the center of the page. But, to get the entire element positioned in the center you will need to offset half the height and half the width. That is what the negative values are for. Try removing those values from the attributes and you will see what I mean.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.