Centering Using CSS

In html 3, there's a “center” tag for centering:

<center><p>hi</p></center>

In html 4 transitional, you can use “align="center"”.

Here's how to do it with CSS.

Blow is a centered block done using CSS.

<div style="margin-left: auto; margin-right: auto; text-align:center;">Hi</div>

In summary, use “text-align:center” to center a text inside a tag. Use “margin-left:auto; margin-right:auto;” to center a block level tag.

2007-03
© 2006 by Xah Lee.