XML technologies / CSS / Advanced websites with CSS / Fixed-width layouts / Centering the page
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
First, center the page and define its width. Add another element div around the entire page. Create this new element div with the name #wrapper and open it immediately after the opening tag <body>, closing it before the closing tag </body>.
<body>
<div id="wrapper">
<div id="header">
...
</div> <!-- content -->
</div> <!-- wrapper -->
</body>Create an external stylesheet with the following CSS rules and save it as fixedwidth.css.
body {
margin: 0;
padding: 0;
text-align: center;
min-width: 740px;
}
#wrapper {
text-align: left;
width: 740px;
margin-left: auto;
margin-right: auto;
}That is all you need to set the page width and center its content. Let us examine the rules in turn.
The first rule applies to the element body. It removes its margins and padding and sets the layout’s minimum width to 740 pixels. It also sets text alignment to center.
Next add a rule for the new element #wrapper added to the document. Set its left and right margins to auto and its width to 740 pixels. To change layout width, change width in both rules. A 740-pixel width leaves background visible on both sides even at an 800×600 resolution.
Viewing the document now should show centered content 740 pixels wide.
Add a few declarations to the rule for body to insert the background image and color from the design.
body {
margin: 0;
padding: 0;
text-align: center;
min-width: 740px;
background-color: #050845;
color: white;
background-image: url(img/bg.jpg);
background-repeat: repeat-x;
font: small Arial, Helvetica, Verdana, sans-serif;
}We have now added both background color and image, just as with our fluid layouts. Text appears in a small sans-serif font.
The background image currently covers the whole page. Give the content area’s element div with ID #wrapper a lighter, more readable background, leaving the image visible only around the content.
#wrapper {
text-align: left;
width: 740px;
margin-left: auto;
margin-right: auto;
background-color: #fdf8f2;
color: #01407a;
padding: 10px;
}The rule above also defines 10 pixels of padding, creating space between the elements div with IDs #wrapper and #content. The next figure shows the page so far.
Figure: The background image gives the page shape.
The header
This header is simple: a logo and a title line. Normal document flow already puts the logo in place, so we can style the title below it with this rule:
#tagline {
font-weight: bold;
color: #050845;
font-style: italic;
margin: 0 0 0.5em 0;
padding: 0 0 0 20px;
}Remember to assign the opening tag <p> of the title line the attribute id="tagline".
This rule makes the text dark blue, bold and italic. A bottom margin adds space below the title. On the left, padding adds padding to place the title below the logo. The next figure shows the result.
Figure: The finished header.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg