eire web design home page contact eire-web design eire web design and development site map
stepping on in business
• CSS Centering fixed-width layouts

CSS Centering fixed-width layouts



Do you ever wonder how you can center a containing div?
The use of “tables” or “center” tag makes the job a lot easier, but it will fail validation.
Another way of doing this is using CSS stylesheet.

body{padding:0px; margin:0px; text-align:center;}

#div_container{margin-left: auto;margin-right: auto;width:800px;}

The only problem with the above solution is that “#div_container” will also have the text centered.
To overcome this problem you have to specify the “text-align” tag as below:

body{padding:0px; margin:0px; text-align:center;} #div_container{margin-left: auto;margin-right: auto;width:800px;text-align:left;}

W3C Visual formatting model explains: “If both ‘margin-left’ and ‘margin-right’ are ‘auto’, their used values are equal.
This horizontally centers the element with respect to the edges of the containing block.”

This entry was posted on Monday, October 15th, 2007 at 10:59 am and is filed under CSS. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...


  • 2 Responses to “CSS Centering fixed-width layouts”

Joe Lennon Says:
December 14th, 2007 at 3:16 pm

Great blog, some informative and useful articles. With this example, I generally use the following:


body { margin: 0; padding: 0; }
#div_container { width: 800px; margin: 0 auto; }

This basically does the same thing as what you have done above, but without the two text-align attributes in the CSS code.

Keep up the good work with the blog.
Joe

Louie @ Eire Web Design Says:
December 14th, 2007 at 5:39 pm

Hey Joe,

Thanks for the pointer and a nice Welcome to our blog.
I love to write more but not enough time.

  • Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Links...

Chicklets...
  • http://www.eire-webdesign.ie/blog/feed/
    http://www.eire-webdesign.ie/blog/feed/
    Google Reader or Homepage
    Add to My Yahoo!
    Subscribe with Bloglines
    Subscribe in NewsGator Online
    add to msn
    Add to My AOL
    Add to Technorati Favorites!
    pageflakes
    windows live