Build Internet has a brand new theme, and that's only the beginning. Read the full story or hide this bar

How to Center a Website With CSS

Centering a website is a key part of many CSS layouts. While there are many ways to do it in a variety of situations, the following is an explanation that is purely CSS, avoids messing with percentages, and is compatible with all modern browsers.

I’ve gotten in the habit of starting by resetting any default margins or padding set the browser. Browsers have default style sheets that are loaded automatically as a foundation for any other CSS. Firefox, for example has a default body margin of 8px. This is a simple way to reset any default values that could interfere with the CSS we are trying to write.

More extensive “reset” stylesheets are available, but for the purpose of this exercise we will only reset the essentials, and keep the code to a minimum.

*{
   margin:0;
   padding:0;
}

I will be using a main div tag with an ID of “wrapper” for this tutorial. You can set the width to any value your site needs (keep resolutions in mind!) I have picked a width of 960px in favor of a higher resolution average (1024×768). The basic page structure is broken down below:

Centered page breakdown

We are first going to assign a style for the #wrapper div tag that will center it within the browser. This is done using the margin parameter. Remember to specify the width of the #wrapper or the element will not center. After all, it’s hard to center something that takes up 100% of width available. For testing purposes you may want to assign a background color and height to the #wrapper to see the results.

#wrapper{
   width:960px;
   margin:0 auto;
}

Put simply, Internet Explorer has some issues. When all the other browsers are told to jump, Internet Explorer reinvents the term jump and then hits the deck. One of the joys of web design is compensating for these differences in interpretation. In this case, Internet Explorer 6 will need some special attention center the main div (#wrapper).

In order to compensate for IE6′s shortcomings we have to first set the text alignment to center. This will ensure that the wrapper gets centered on the page.

body{
   text-align:center; /*For IE6 Shenanigans*/
}

The body tag encapsulates the wrapper, so the text alignment on it is inherited by the wrapper. Odds are you will want the text to be left-aligned. To do this we will need to  update the #wrapper styles to set the alignment back to the left.

#wrapper{
   width:960px;
   margin:0 auto;
   text-align:left;
}

That should do it! This small block of CSS can center any website needed, simply change the sizes. Your complete CSS code should resemble the block below:

*{
   margin:0;
   padding:0;
}

body{
   text-align:center; /*For IE6 Shenanigans*/
}

#wrapper{
   width:960px;
   margin:0 auto;
   text-align:left;
}

Wordpress.com stats not installed! Posted Monday, November 24th, 2008 / Back to Top

I this post. Tweet
SPONSOR

47 Comments 4 Mentions

  1. Dustin Author Editor

    Very simple and easy :). Straight forward and to the point tutorial that is easy to follow and has a touch of originality. I like it, bookmarked.

    December 6, 2008 · Reply

  2. Matt Author Editor

    There’s a better method, but what you have is great as well. Instead of using text-align:center; which will center all elements in the body tag, use margin-left:auto; and margin-right:auto; . Using text-align:center will center all text and makes you override it in each div, or the main div, like you did in the #wrapper. You won’t need to using the margin-left and margin-right.

    But thanks for taking the time to submit a great tutorial for those who don’t know CSS very well, :).

    January 18, 2009 · Reply

  3. Brian Author Editor

    @Matt – Isn’t “margin:0 auto;” the same thing as “margin-left:auto; and margin-right:auto;” ?

    January 22, 2009 · Reply

  4. Matt Author Editor

    Yeah, ^_^. It’s just a coding preference, I don’t like combining much of the rules ’cause I feel that individual rules for each side gives more control, for instance, with differing colored borders on each side to give a 3d-ish appearance (although I don’t, it’s just an example). Sorry for a late response, been a bit busy at work creatin’ a customer service course.

    February 1, 2009 · Reply

  5. Matt Author Editor

    Lol, mixed two different posts with a copy/paste, sorry. Scrap that borders part. I”ve heard some people have problems in IE using margin:0 auto;, so I also do it just to avoid any potential issues in that department in case they ever did arise, for whatever reason. But yes, it’s the same.

    February 1, 2009 · Reply

  6. Siiri Author Editor

    Thank you so much!!! I’ve been trying to do this for about 2 weeks with no results. Every other instruction I managed to find didn’t work. But with your help here I finally was able to fix my problem. Very simple.. thanks :)

    February 19, 2009 · Reply

  7. Jarrid Author Editor

    Rather than using text-align:center; use the following
    #wrapper{
    width:960px;
    margin:0 auto 0 auto;
    }
    Now you do not have to use the text-align:center; on the body to make it work in IE6.

    April 13, 2009 · Reply

    • Zach Dunn Author Editor

      @Jarrid

      I’m a little confused with the difference of your solution from the one listed in this tutorial.

      Margin:0 auto; is shorthand for exactly what you have written. The text-align changes have to occur in order to prevent IE6 from rendering incorrectly. The option laid out above will center a website, and do so in all browsers. It really can’t be cut down any more — everything listed is needed.

      April 13, 2009 · Reply

  8. пpивидeниe Author Editor

    Хм… что-то у меня ссылка не открывается, которую указали. Это у всех так, или только у меня?

    May 30, 2009 · Reply

  9. Lieur Author Editor

    Lieuy euy, kurang ngerti
    tapi gpp, walu bgaimana pun
    tutorialx bagus banget

    OK dech,, Thanks a lot for u’r Tutorial^^

    June 11, 2009 · Reply

  10. New Author Editor

    Hi there, I am just learning CSS. Can you please tell me how to do the side colors.

    June 21, 2009 · Reply

  11. Raz Author Editor

    Actually I too would like to know how you would add a little color or design to the left and right sides of the now centered website! White sides can be a little boring… Besides having a solid color is it easy to implement a repeating background image on the sides like a logo?

    Thanks!

    July 1, 2009 · Reply

    • Sam Dunn Author Editor

      @Raz
      It’s simply a matter of adding something like “background-color: #000000;” to the style of the body

      July 1, 2009 · Reply

  12. awk Author Editor

    Hi,
    I have tried this all morning but seem to be getting nowhere!
    Can I paste my css somwhere so someone may be able to point out my problem?

    Thanks

    August 4, 2009 · Reply

  13. Matt Author Editor

    Thank you so much!!! I have been trying to do this for hours to no avail and then I found your wonderful website! Your explanation was so simple an easy!

    August 17, 2009 · Reply

  14. Phil Davies Author Editor

    Thanks for a great tutorial helps lot

    December 11, 2009 · Reply

  15. Pavitra Author Editor

    What about centering it vertically …?

    February 27, 2010 · Reply

  16. local Author Editor

    what about center tag?

    March 10, 2010 · Reply

  17. Dannielle Author Editor

    I know NOTHING about css. But I am trying to learn. This code seems simple, but I don’t know where to put the css code. I tried creating a new css and putting it there but did not work. Can someone tell me exactly where to put this css code in my site. Also, do I need to do it for every page? I have over 20 pages! Thanks

    August 2, 2010 · Reply

    • stijn Author Editor

      every page should contain a linking line in the head

      December 10, 2010 · Reply

  18. Mike Behnken Author Editor

    Hello, I desperately need to center my site and make the background black… my CSS sucks so I cannot figure out how to do it… If someone has an easy solution, please contact me

    October 3, 2010 · Reply

  19. Eswar Author Editor

    Good ways
    simple way to set center margin:0px auto;

    some times center tag can also do center

    Here are another ways to center http://www.templatespoint.com/blog/2009/06/how-to-center-main-div/

    November 19, 2010 · Reply

  20. Montreal Web Design Author Editor

    I would recommend to try Compass Style (compass-style.org), it is a CSS generator/framework that makes it easy to create CSS for a website.

    January 18, 2011 · Reply

  21. Brenna Ahern Author Editor

    This article just saved my sanity! Thank you!!

    February 4, 2011 · Reply

  22. jadox Author Editor

    Hi all,
    The web page I designed works perfectly in firefox, but is all messed up in IE, where the content shoots to the right and the link don’t work. From my research I think the problem is something to do with the doc type but I still haven’t been able to fix it. I don’t think the problem is my css.
    http://www.pipersquickspan.com.au

    body {
    margin:0 auto;
    text-align:center;
    }
    #container {
    margin-left:auto;
    margin-right:auto;
    text-align:center;
    width: 985px;
    display:block;
    position:relative;
    }

    February 28, 2011 · Reply

  23. Janet Author Editor

    This didn’t work for me :( Only my top banner centered, nothing else. Any advice?

    March 15, 2011 · Reply

  24. tuttifrutti Author Editor

    @janet

    you need to use a main div tag in order to center a page. in this tutorial #wrapper is the main div

    March 22, 2011 · Reply

  25. Oliste Author Editor

    Something important when we try to center a webpage is to set POSITION:RELATIVE;, inside the wrapper.

    #wrapper{
    width:960px;
    margin:0 auto;
    text-align:left;
    position:relative;
    }

    DON’T FORGET IT!!

    March 29, 2011 · Reply

  26. Mzenene Author Editor

    Thanks man, worked like a charm, i was close to quitting.

    April 19, 2011 · Reply

  27. NameCo Author Editor

    very good products, just to say to my friends.

    May 25, 2011 · Reply

  28. web tasarım izmir Author Editor

    nice review ,and awesome article thanks for the post, I found this blog just yet but I am adding you to my bookmarks

    June 23, 2011 · Reply

  29. HurdanStore Author Editor

    Mmmm my website works fine with firefox, but in IE it’s completely mess, please tell what should i do now, i need my body content in to be centered. One more thing is that my body is stretching in big screens and getting small in small screens horizontally. Just try my website in Firefox and in IE and you’ll notice. Does someone know how to align wy website body in centre and stop stretching? lol gosh replies asap

    July 29, 2011 · Reply

  30. HurdanStore Author Editor

    My website is: hurdanstore. com

    July 29, 2011 · Reply

  31. Alexander Roche Author Editor

    Thanks for this!!!!!!

    August 15, 2011 · Reply

  32. Lii Author Editor

    nice tut bro

    September 9, 2011 · Reply

  33. Akshay Author Editor

    margin: auto; working fine for me .. !

    September 14, 2011 · Reply

  34. kleet81 Author Editor

    Hi The best advise. It helped with my design. I have added you guys to my top website advise folders bookmark.
    Thank you and
    God Bless

    September 20, 2011 · Reply

  35. Nik Author Editor

    THANK YOUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU SOOOOOOOOOOOOO MUCHHHHHHHHHHHHHH

    October 5, 2011 · Reply

  36. Madhu Kaleeckal Author Editor

    How we can avoid this unlimited resource..

    Thanks

    November 3, 2011 · Reply

  37. Kim Author Editor

    This is my first website…..I cannot seem to get my website centered on internet explorer, it is perfect on firefox, I have read all the suggestions above but do not know where to start, ie which file do I make these amendment to, please help

    Thanks Kim

    November 11, 2011 · Reply

  38. manish Author Editor

    user
    body{text-align:center;}
    #wapper{width:960px; margin:0 auto; text-align:left;}

    November 18, 2011 · Reply

  39. silas Author Editor

    very useful . Thanks for the post

    November 19, 2011 · Reply

  40. David Author Editor

    Thanks a lot….

    It saved hours of my time…

    at the end of my frustration, i found this… thank you very very mcuh

    January 5, 2012 · Reply

  41. Mark Author Editor

    Very well done indeed. Easy to follow and it works – wow

    January 12, 2012 · Reply

  42. Jen Author Editor

    Extremely helpful. All of my IE shenanigans have been resolved.

    January 19, 2012 · Reply

 

Join the Conversation

Back to Top / Comment RSS

2011 Build Internet. Created by One Mighty Roar. Icons by Komodo Media. Back to Top