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.

Despite the fact that I am a dinosaur when it comes to technology you had the patience to see me through to being able to give you the text and pictures, which you then turned into a really good finished article. I have had many emails from people who have seen it, all of whom have congratulated me on the website. But the congratulations really relate to your work.

(Mike Mackenzie)