How to align a DIV box at the center using CSS

Posted on May 16, 2009

Here are some examples of how the new web standard of CSS deals with horizontal positioning.

<div style=”text-align: center;”>test</div>

test

<div style=”text-align: center; width: 200px;”>test</div>

test

<div style=”text-align: center; width: 400px;”><div style=”width: 200px;”>test</div></div>

test

<div style=”margin-left: auto; margin-right: auto;”>test</div>

test

<div style=”margin-left: auto; margin-right: auto; width: 200px;”>test</div>

test

<div style=”width: 400px;”><div style=”margin-left: auto; margin-right: auto; width: 200px;”>test</div></div>

test