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.

Jo has been amazing. Through 1-2-1 training sessions she has helped me to make significant improvements to my website functionality. This has not only been cost effective but it also has empowered me to use the skills that I have gained to edit my website myself in the future. She has simplified what I thought would be tricky tasks. I will definitely continue to work with her in the future and I couldn’t recommend her highly enough.

Felicity Wood