Center an Absolute Position DIV

Posted on October 8, 2009 by Aaron Ortega

So you have information in a div, and you have its position set to absolute, and you want to center it on your page. Seems impossible to do, but with a little thinking we can pull it off.

Let’s say this is the code we are using, and we want to center the “absolutecenter” div.


<div id="container">
<div id="absolutecenter">This is what I want to center</div>
</div>

The trick to it is to add a width to the #absolutecenter id. Once we establish a width for the container div, we can position the left side of the div 50% across our parent div (#container).


#absolutecenter {
position: absolute;
width: 100px;
top: 0;
left: 50%;
margin-left: -50px;
}

Now here is the trick. Take the width you assigned to #absolutecenter and divide it by 2. This number will be the number you use for the left margin, which will be a negative margin. The negative margin of -50px will move #absolutecenter to the middle of your container div. All done!

Did you like this post? Share it!

1 Comment

  1. sara says:

    Wow this was potentially one of the most intelligent blurbs I have come across on the topic so far. I do not know where you get all your data but up! I’m going to send a few individuals over here to take a look at this post. Amazing, just plain awesome. I am just getting into writing articles myself, nothing compared to your writing potential (lol) but I would love for you to have a look at my stuff someday! right here

    December 14, 2009 at 12:18 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

Archive