Centering A Web Page

Last Updated: Nov. 09

Having trouble centering your web page in all browsers? Insert this simple code into the beginning your CSS file:

body {
margin: 0 auto;
text-align: center;
}

Short explanation:
The margin:0 auto; part covers Firefox and most other browsers while the text-align:center; part aligns everything in the center in Internet Explorer.

Further:
If the text on your entire page ends up centered also, just add text-align:left;, text-align:justify;, or text-align:right; (whichever you want) to the area you are working with using css.

Example:

body {
margin: 0 auto;
text-align: center;
}
#main {
text-align: right;
}

One More Thing:
If you want to center a specific div or table, identify the table and then use margin:0 auto;. You must also set the width of this area! This is so that the margin is set equally horizontally.

For instance:

#main {
margin: 0 auto;
width:500px;
}

Bookmark and Share