Horizontally Center Your Website Structure Using CSS
Written by David Walsh on Sunday, November 25, 2007
Every web programmer knows that you can’t code a commercial website for a specific browsers or user experience level, much less a screen resolution. Since you can’t count on a user having the latest and greatest, you need to use the lowest common denominator of browsers resolution. In today’s web world, 1024×768 is usually the lowest browser resolution coded for.
I prefer to create my websites using a fixed width so that I can have complete control over the website layout. A fixed width website looks the same in all browsers which can prevent users with extremely large or small resolutions from experiencing layout issues. Instead of simply allowing the website to align completely left on the screen (resulting in high-resolution users seeing your website take up only half their screen), I center the site. This is a very simple task.
Step One: XHTML
Declare a DOCTYPE. Create an initial “wrap” DIV that will be the website’s wrapper.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>My Site</title> </head> <body> <div id="wrap"> <!-- WEBSITE GOES HERE --></div> </body> </html>
Step Two: CSS
Declare the wrap ID — you MUST declare a width (otherwise, how would you center it?) Use left and right margins of “auto.”
#wrap { width:900px; margin:0 auto; } /* "auto" makes the left and right margins center the wrapper */That’s It?
Yes! Centering your website is that easy. This website currently uses this layout. Does my website take up most of your screen? That means you have a low browser resolution.
Follow via RSS Epic Discussion
Be Heard!
I want to hear what you have to say! Share your comments and questions below.











Great site. I thought I was a great web designer but I’ve relaized there’s a lot more to learn. Thanx.
But here’s a tip from me (I guess you already know it but some of your readers might not):
html
{
height: 100%;
margin-bottom: 0.01em;
}
Add this to your style to always have a scroll on the page. Otherwise the centered div will jump when switching from a page with a scroll to a page without. The width of the page is dependant of whether there is a scroll on a page or not.
Hi!
I never really got my head out of the fixed layout world handed to us by the printing industry. I’m not able to realise a design for this new internet web based medium – so instead I’ll carry on pretending I’m using paper and ink. Hard luck if you’ve got a 1900×1280 wide screen, or an older 800×600 set-up eh?
To be fair, I shouldn’t really claim to be a web designer because I’m not really designing for the web.
That’s pretty old news…
http://www.simplebits.com/notebook/2004/09/08/centering.html
@Martin: Thank you for sharing. I always do notice the scrollbar “jump” in FireFox but it’s never been a standard in our web shop to do anything with it. I do agree that it could make the site smoother.
@Tim: I don’t try to set the world on fire with every post. When I view my web stats (Google Analytics), I see keywords for what people are looking for to get to my site. I saw a fair amount of keywords that related to centering a website, so I decided to create this post. If you look at the “About Me” section, part of the goal of this website is to save people time with quick tidbits — that’s exactly what this is.
It is really nice to reminded of these small bits of useful code. Keep it up!
Hi David,
Woah. This post is from the future =).
Very nice simple trick for centering, I’d say 90% of every site I’ve ever designed uses this. It is good that you pointed out that DOCTYPE, this trick doesn’t work unless you have that properly declared.
Nice tip David. Thanks for sharing this with us. Savvy designers probably know it already, but less experienced developers like myself certainly appreciate tips like this. Please keep up the good work. Thanks also to Martin for his tip to prevent page jumping!
FYI, this won’t work for IE6 and below in quirks mode. You need to add a text-align: center to the body and a text-align: left to your container.
I agree with what has already been said, as we develop as web designers we can soon forget the simple little tricks we used to use because we found some new more complicated designery way of doing the same thing. It’s can be good to get back to basics…..
As a programmer, I use this daily and don’t really think much of it. Quite frankly, I’m surprised the article has been so highly viewed. CSS is clearly the bridge between programmers and designers.
Here is another quick fix for the scroll bar issue…
html { overflow-y:scroll; }
Hi David,
Thank you for this tip. I have used it loads of time before, but this time when I came to use it, I just had that one thing missing… the width of the wrapper class.
R
This was a very useful article. Thank you.
Thanks for the additional comments too.
Thanks a lot man, I’ve been trying to figure this out for quite some time but other articles never really got to the point. I am happy that you were able to help me out efficiently and quickly.
Regardless of what anyone may say, keep it up for sure. And I’ll be sure to keep this site and you in reference if I have any questions pertaining to css, html layouts.
I can not get this to work. It looks so simple, but it still doesn’t work for me? Can someone e-mail me a fix? I don’t know how long it will be till I come back here :(
P.S. email is tsteuwer @ gmail dot com
Do you have a link to your page Troy? If so, we can take a look and make suggestions.
Regarding centering a site structure using css and wrap:
I have some external css links as well. They seem to negate the centering that you describe above. How do I use external links, either in conjunction or exclusively, with this wrap procedure?
Thanks again,
Jeff
Below is the code for a page I created using CSS and multiple and classes (sorry, I’m in the process changing them to ids). I was able to center the structure but all the divs don’t seem to fit correctly. How do I position within the wrap ID?
@Jeff: Could you provide an example URL? It would make answering your question a lot easier.
I’m really new to css and was wondering if you could help me out.
Is there a way to do this with the background as well so that the background follows the page?
I’ve centered my background with ‘background-position: center’, but lets say my page is 1000px in width. If I shrink the page to below 1000px
the background will shrink from the outside in but my page shrinks from the bottom right, so then my background ends up not matching the page.
Thanks,
dan
@Dan: Can you provide a sample page? I’m not quite understanding your question. Thanks!
I’m trying to post to your blog but I can’t find the link to do so.
Below is a sample of a page I’m trying to create using pure CSS. I want to nest a CSS rollover inside another div. I can create the div sections but the CSS rollover will not appear. Is there a way to do this or do I just use Javascript.
HTML:
Jeff Steiman Production Design Online Print Portfolio Site
right
CSS:
@charset “UTF-8″;
/* CSS Document */
#container{
width:707px;
margin-left:auto;
margin-right:auto;
height: 786px;
background-image: url(images/bg_Atty.jpg);
}
#header{
}
#header2{
height: 109px;
}
#left{
float:left;
width:300px;
height:200px;
padding-top: 19px;
padding-left: 17px;
}
#right{
margin-left:320px;
}
#footer{
clear:both;
}
#nav {
width:220px;
height:200px;
}
.btnHome a {
display:block;
margin-left: 17px;
background-image:url(/images/btnHome.jpg)0 0;
background-repeat: no-repeat;
}
.btnHome a:hover {
display:block;
background-position: 0px -24px;
}
@Jeff: Can you send me a sample page URL? Thanks.
Hi David i really thank u for the kind of information u have given, i t is so useful to me. THANK YOU once again. I just want to keep in touch with you, can u please reply to my mail id.
Hi David. I came across this and it worked out great; for Firefox. The page is still naturally aligned left for some reason in Internet Explorer. If you could reply to this as soon as possible, it would be much appreciated.
EDIT: Nevermind; problem solved. (:
Great job! keep up the good work!
One question…”I tried out the wrap method but the words I typed which were HOME, TANKTOPS, T-SHIRT, etc… are centered throughout, but what I am trying to do is insert these words into an “image” following the movement of the “image” background… This is what I have for this part, please… I need help:
HOME
TUBETOP
TANKTOP
T-SHIRT L
EGGING
BLOUSE
I am trying to insert the into the
HOME
TUBETOP
TANKTOP
T-SHIRT L
EGGING
BLOUSE
@James: To be completely honest, I have no idea what you’re asking me. Please provide a URL.
Hey again Dave! I finally got a web hosting server and so glad I can get some worldwide feedback. I along with the rest of us appreciate what you are doing here and hope good things will happen to you.
The url I just made is http://www.bargain4clothing.com
*From looking at page source, i used a different method of centering. I think your method is more professional, thats why I think I should delete mine and use yours.
*My method can only center images, not layers. I would like to know how to center them without using absolute positioning.
Hopefully you can help and hopefully it will turn out greato!
Thanks again so much and have a great day!
Awesome! I have been looking for this code for quite some time. Thanks a bunch!
This code isn’t working in Internet Explorer. It shoves the whole page to the right instead of centering it.
It does how ever work fine in Firefox and Safari.
What I can I do to fix it?
Well never mind!!
HERES A TIP IF YOU ARE HAVING PROBLEMS WITH IE
if the doctype is not set to XHTML, IE6 and 7 both seem to have issues centering it. If that is the case simply add a “text-align:center;” to the BODY and a “text-align:left;” to the wrapper (to counter act the inherited text-alignment of the BODY).
Maybe this was said in some other way previously, but I have no problems with redundancy. Great stuff! thanks!
Hey david
this site is awesomee!!!
n this was exactly wat i needed
i dint try it yet , i thought u could take a look at the site n help me
the page has abbsolute positioned objects
it is fine in browsers of a normal desktop pc
but in widescreen laptops it is to the left
so i think this code is the right thing as i just learnt css
hope u take a look at the site and if its not too much trouble i hav some problem wit my menu in which i used thee hover option i know its not supported i IE6 but in IE7 the drp downs go berserk so i hope u can help me wit tht.
thanks a lott if its posibble mail me at hameed.ansari@hotmail.com
Thanks! In two minutes this CSS solved what was taking me hours to figure out.
Learn how to size your text and your website layout in em instead. :) It’s poor practice to force an user to use the text sizes you specify because some users may well need to have larger text if their eyesight is failing or they have some other eye problem. It’s off-putting to them.
Hi
I have managed to center in ie7 but firefox and google chrome elude me i have also centered in ie6 but there i have other issues i hate ie6 i have posted the relevant code below if you could tell me what to do i would be very happy thanks already for getting me this close its been driving me nuts.
The site i am working on is http://www.portugalmidlands.com
relevant styles are
body {
background-color: #000000;
text-align: center;
}
.wrap {
width:980px;
margin-left: 0 auto;
margin-right: 0 auto;
text-align:left;
}
.leftcolumn {
background-color:#ffffff;
border: 1px solid #cc0000;
padding: 10px;
width: 230px;
float: left;
font-family: georgia;
font-size: 12px;
color: #000000;
text-decoration: none;
text-align: justify;
position: relative ;
}
.centrecolumn {
background-color:#ffffff;
width:670px;
font-family: georgia;
font-size: 12px;
color: #000000;
text-decoration: none;
text-align: justify;
padding: 20px;
position: relative;
margin-left: 260px;
}
@Paul: This is what it should be:
.wrap { width:980px; margin: 0 auto; text-align:left; }
Hi Walsh, I have an inquiry. What about if you used a photo imaging software to design your site and it is basically a bunch of “absolute positioned” images that somehow are always positioned all the way to the right when interpreted by a browser. I have a tired a lot of different ideas, but none so far work. I don’t really want to start again from scratch. I am not a coder either so a lot of this css or html is greek to me.
Maybe you could look at my site and give me some advice on what can be done about centering the page. http://www.bengonsioroski.com
Thanks for your time,
Ben
david you rock my world : )
love your work!
THANK YOU!!! I’m used to using Dreamweaver CS3, but only had access to Dreamweaver 8 to do my portfolio site. I was so frustrated about not being able to figure out where “site properties” was & why my page would not center. I finally searched “centering using CSS” & came across this blog on my first search. I’m so glad. It was SO easy to fix my site! I’m uploading it right now! Thanks! :-D
Thank you David!! it helped me =P
David,
Thanks a ton!! I tried your CSS centering technique and it worked like a charm. Two questions:
I hear that to make it work with older versions of IE it is necessary to add some workaround code involving some text alignment stuff. Being something of a newbie, would you be willing to give us that code and exactly where it gets plugged into your centering example?
To make my website stand out I would like to set a background color, probably black, for the wrapper. How can I do that?
With much thanks for you help to all of us!
Clay
David, sorry about the URL typo. Likely you would have caught it, but if not see above.
Clay
THANK YOU SO MUCH. I’m just breaking into web development and this help out GREATLY.
again thanks
im trying to use your code for my website. im very new to web design and im still working on my first site. i don’t have it uploaded yet so im still pulling it out of a folder on my computer, but it won’t center with the code up above. will it only work when it’s uploaded?
hello
i am trying your example code to center my website but it’s not working. can someone please have a look at my code and see what i should be adding or taking out at http://www.gracefullearning.com/index.html. i tried changing my DOCTYPE to be exactly like yours but then my images jumped all over the place, so i had to put back my original DOCTYPE.
ps: can you please example in simple dummy terms as i didn’t write my original code it was generated from a free website design program.
thanks
grace mrspuff@gmail.com
Hi, thanks for the info.. I found that wrapping /putting in a container alot on the internet. But that means I can’t use position:absolute with that (obviously)..
@tk maybe that’s your problem too
so i can’t position the images and text right.. i started css and always used position:absolute.. cause that’s how I can control the layout to the very last point… so i don’t get it… when you center how can you still put e.g. the menu on the left hand side and the content on the right… i dont want to do relative positioning like div.menu{ height:200px; …} and div.content{top: -200px;…} to get to the top again…
I tried to read myself through code of websites.. but they use either a lot of script or are way too confusing… is there an easy way ?
i propably am a lost cause… but if you have a simple way to explain.. or a link where i could find sth. i’d really appreciate it.
Hi using position absolute for your whole page really is a non starter it should be used as a last resort and you can position everything to the pixel using css anyway the only thing i use css for is the drop down menus.