How to center a website accross all browsers using CSS
Now I’m not sure about you, but I really do hate websites that are stuck on the left of the screen, and from talking to a few friends, I know I’m not alone with this pet hate. The thing is, that many beginner site designers may also dislike this left alignment, but simply do not know how to make their site appear in the center of the screen, but others may have centered their site absolutely perfect in their favourite browser, but are totally unaware that it may not be centered in others.
Today, I’d like to share my own method of centering your website in any browser using CSS – I’m going to be using CSS in the head of the HTML document today for simplicity, although ideally you should place it into an external/linked stylesheet on your own pages.
The trick to centering pages in all browsers is using what’s commonly known as a ‘wrapper’, applying some auto margin to that div in the CSS, and then using a small workaround so that the wrapper is centered in IE.
I’m going to start by using a basic outline template, demonstrating the basic page structure and illustrating what I mean buy the wrapper – which is commonly just a div to contain everything in.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>Title</title>
-
<meta name="robots" content="index, follow" />
-
<meta name="keywords" content="" />
-
<meta name="description" content="" />
-
<style type="text/css">
-
body, html {
-
margin: 0;
-
padding: 0;
-
}
-
#wrapper {
-
width: 800px; /* your wrapper obviously needs a width otherwise it will be 100% */
-
margin: 0 auto; /* This is the proper way to center in a standards compliant browser */
-
}
-
/* This is the IE workaround*/
-
body { text-align: center; }
-
#wrapper { text-align: left; }
-
-
/*Now to just add some colour to see it in action*/
-
body { background: #000; }
-
#wrapper { background: #fff; color: #000; }
-
</style>
-
</head>
-
-
<body>
-
<div id="wrapper">
-
<h1>Title</h1>
-
<p>Content to go here.</p>
-
</div>
-
</body>
-
</html>
Now you can see the structure is fairly simple – the wrapper is just a div with a id of “wrapper”, which we specify a width for, and then set the margins to “0 auto”, which means no pixels for top and bottom margins, and auto for left and right margins. the auto value for margins basically gives the left and right edges equal space between the edge of the wrapper div and the edge of the browser window.
Next, we have to add the workaround for IE6, because it doesn’t understand auto as a value for margin. We basically do this by using text-align:center for the body to center the div on the page, but this will also center everything inside the div too, so we then have to re-align this to the left again using text-align:left for the div itself.
I’ve applied some background colours to the body and the div, so that you can see the centering effect for yourself – go ahead and try it for your own pages, and test it in different browsers…enjoy.

March 4th, 2009 at 4:13 am
I added from the open style to the close tag to my css file. 4.01 transitional index file. It worked just fine with IE6,7. But Firefox some tables went left, and The Iframe centered. well everything went all over. I guess its the 4.01. The only way I can center in all browsers is the center tag. I have to say the effects on this page are great. have not open in ff yet.
March 11th, 2009 at 6:07 am
I’ve been looking around allllll night for a resource that would actually work for me – and you did it! Great job and great example. Keep up the good work!
May 20th, 2009 at 3:46 am
Thanks… worked like a charm..
very well explained!
June 22nd, 2009 at 8:25 pm
Hi, I have a client who wants their site to be centered no matter the size of the browser window – their designer created a very wide site (1334 pixels), so many browser windows are much smaller, and the default is to position it left until the window is wide enough to allow it to center – Any way to make it center even in the smaller windwo – so the overhand is on both left and right sides?? THanks!!
October 28th, 2009 at 5:36 pm
I have been to many blog sites all saying the same thing to center a site across all browsers. Even my web design teacher is stumped by what could be going wrong with my site. I have a body style with 0 auto margins, a text-align of center, and a position of absolute. My wrapper div has a text-align left with 0 auto margins, a position of relative, and a width of 800px. All my divs within the wrapper div have an absolute position. I’ve been tinkering with this code for a few days now and am going out of my mind. It only works in internet explorer, but ff, safari, opera…. STILL TO THE LEFT!!!! Please help me!!!
October 28th, 2009 at 5:38 pm
Does absolute positioning have anything to do with it? Is it not working because I have a basic navigation spry menu created in dreamweaver? What is GOING ON!!!?