1. Home
  2. Computing & Technology
  3. Web Design / HTML

How To Use CSS to Center a Document with a Fixed Width Layout

By , About.com Guide

Fixed width layouts can be hard to center with some of the popular browsers out there, but it is possible, with only a few lines of code.
Difficulty: Average
Time Required: 10 minutes
Here's How:
  1. Create a new Web page with a CSS style sheet in your HTML editor.
  2. Create a div element as the main element on the page where you'll store everything on the page.
  3. Give that div element an ID that is unique on the page.
  4. Open your CSS style sheet and set the width of your div element.
    div#main {
      width: 750px;
    }
  5. Add automatic margins to center your div.
    div#main {
      width: 750px;
      margin-left: auto;
      margin-right: auto
    }
  6. To fix it for Netscape 4, and IE 4 - 6(quirks mode) add a text-align on the body.
    body {
    text-align: center;
    }
  7. But then all the text inside is centered, so re-align the text in your #main div by adding text-align: left; in there.
    div#main {
      width: 750px;
      margin-left: auto;
      margin-right: auto;
      text-align: left;
    }
  8. Save your page and your style sheet.
  9. Test in several Web browsers.
Tips:
  1. This will center the layout box but not the content within it. Use text-align to center the inner content.
Explore Web Design / HTML
About.com Special Features

Vote in the Reader's Choice Awards

What are the best instant messengers, apps, and editors? You decide, in our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Web Design / HTML

©2010 About.com, a part of The New York Times Company.

All rights reserved.