How to: Center a Div With Absolute Positioning

24th November 2010

When using relative positioning the easiest way to center one or a number of div tags is to create a wrapper without any float and set both the left and right margins to auto. However centering becomes problematic when using absolute positioning because using auto left and right margins no longer works in the same way. Instead, you can use this technique…

Select the div you wish to center and take note of what width you have given it. Set the left attribute to 50% and the left margin to minus whatever half of your width is. For example, if you had a 800px wide div you wanted to center you would set the left position to 50% and the left margin to -400px.

Preview the page and you should see that the div is perfectly centered, unless you have a width which is not easily divisible by two and has had to be rounded, then it will just be very very close.

Leave a Reply

Jo did a wonderful job creating my new website — just look at it. She grasped the brief immediately, and then she added her own creative spin. I found her very easy to work with, partly because she listens attentively. She’s also been prompt and patient with my follow-up questions as I’ve learned how to navigate my new site. All in all this was an incredibly painless experience with an outstanding result. Thanks Jo

The Oxford Astrologer