Use Css to center a div

February 5, 2009 by: B.Harding

Using tables to layout a page is considered outdated. The “new” way to do it is with Div tags a alot of Cascading Style Sheets.

To center a table is easy. You just set align=”center”. But what about when using the new way?

When you want to center a div in the middle of the page use either a style or a css class to set the following attibutes:

width: a fixed width;
margin-right:auto;
margin-left:auto;

Note this tip is only for horizontal alignment. Centering vertically will be discussed in a future post.

example:

<div style=”width:600px; margin-left:auto; margin-right:auto;”>

Hello Middle.

</div>

Filed under: Uncategorized
Tags: ,

Leave a Reply