XML technologies / CSS / CSS Cookbook / Page layouts / Single-column layouts

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

You want to create a layout with one main column, as shown in the following figure.

A single-column page layout

Figure: A single-column page layout.

Solution

Give the left and right margins of the element body percentage values:

body {
 margin-left: 15%;
 margin-right: 15%;
}

Discussion

Percentage left and right margins on the document body keep the column width flexible, allowing content to span the width of the user’s browser window.

For a fixed-width column, define the property width for the element body with an absolute length, such as pixels:

body {
 width: 600px;
}

This places the column on the window’s left side. To center it, enclose all content in an element div with a unique attribute id, for example frame:

<div id="frame">
 [...]
</div>

In the CSS rule, assign the left padding of the element body a value of 50%:

body {
 width: 600px;
 padding-left: 50%;
}

Use an ID selector to assign the column a negative value equal to half its width. Make sure the column has the same width as the element body.

#frame {
 /* Spaltenbreite festlegen */
 width: 600px;
 margin-left: -300px;
}

You may think the same result can be achieved by setting both left and right margins to auto:

#frame {
 width: 600px;
 margin-left: auto;
 margin-right: auto;
}

Unfortunately, this otherwise good, straightforward approach fails in Internet Explorer for Windows. This solution therefore uses a workaround compatible with most common browsers.

See also

The recipe Centering elements on a web page for centering elements in a web document, and the recipe Horizontal navigation menus for creating a horizontal tabbed navigation menu.

  

 

 

 

back to top