XML technologies / CSS / Advanced websites with CSS / A simple CSS layout / Positioning page elements / The CSS-box model
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
To a stylesheet, every HTML element on a page sits in its own rectangle, called a box. This is obvious when styling large content areas, such as the three identified in our design, but it also applies to their individual components: headings, lists, list items and even portions of text.
The following figure shows the basic box model.
Figure: The basic CSS box model.
At the center is the content itself. Do not take “content” to mean just words or images forming a news panel or link list. Anything contained in a box counts as its content.
As the figure shows, visible width combines content width, padding and border. The margin sets the distance to neighboring elements and remains invisible. Visible height similarly combines content height, padding and border, with margins determining where the next element may start.
Margin, border and padding can each be defined separately for left, right, top and bottom, or with shorthand properties. Borders are slightly more complicated because they also have characteristics such as line style and color.
First we will use padding in a layout, then margins, which behave similarly, and finally border properties and variations.
The next sections demonstrate CSS rules on a one-box layout.
Start with the following figure: a box with no padding, margin or border definitions. Its width and height equal those of its content.
Figure: Box without property definitions.
The element h1 in this example has a gray background, set with the property background-color, to distinguish its interior from the outside. This HTML creates the page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box ohne Eigenschaftsdefinitionen</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {
background-color: #ffffff;
}
h1 {
background-color: #c0c0c0;
font-family: helvetica, arial, sans-serif;
}
</style>
</head>
<body>
<h1>Hilfe! Ich stecke in einem Box-Modell fest!</h1>
</body>
</html>Following our “CSS rather than HTML” approach, subsequent changes affect only the stylesheet, not HTML. Changed or added fragments are highlighted.
Pixels versus percentages
The box model is a layout tool for precise positioning. Pixels, abbreviated px, are preferred for constant spacing and borders. For a flexible layout adapting dynamically to the viewport, use percentages, or % values. Such layouts stretch or contract their elements according to the width the user gives the browser window.
Padding properties
A CSS rule can define four specific padding properties: padding-left, padding-right, padding-top and padding-bottom.
Define one padding property on our unstyled box to see what happens. Change the example’s stylesheet as follows; changes are highlighted:
h1 {
background-color: #c0c0c0;
color: black;
padding-left: 25px;
}The following figure shows the result: the text is now 25 pixels from the box’s visible left edge.
Figure: Box model with padding-left.
Define other sides similarly, here with a different value on each:
h1 {
background-color: #c0c0c0;
color: black;
padding-left: 25px;
padding-top: 15px;
padding-bottom: 30px;
padding-right: 20px;
}Figure: Box with different padding on each side.
The figure shows how those different padding values affect the browser display.
The right padding seems not to have taken effect: despite setting padding-right to 20 pixels, the box still reaches the viewport’s right edge.
This is because padding-right inserts space between the text’s right end and the gray heading’s end. It is hard to see because the heading is a block element spanning the entire width, leaving enough room for text on the right. Narrow the window below the width of the element h1 and the padding becomes visible.
Figure: Examples of padding-right.
The first screenshot makes this clearer: the window is narrow enough that “fest” would otherwise fit on the second line, but our padding-right of 20 pixels pushes it to the third.
The second screenshot makes padding clearer still. The window fits one word per line, although the gap to its edge could accommodate more. Our padding restricts content space. Removing it from the stylesheet produces the third screenshot at the same window size.
A shorthand property simplifies setting all four sides: padding. It accepts up to four space-separated values. The table shows the results of the four possible combinations in padding.
Table: Number of values in padding shorthand.
Number of values | Effect on padding |
|---|---|
1 | The value applies to all four sides. |
2 | The first value applies to top and bottom; the second to left and right. |
3 | The first value applies to top, the second to right and left, the third to bottom. |
4 | The values apply to top, right, bottom and left, in that order. |
Remembering the order:
Remember the clockwise order, or use the mnemonic “trouble”: top, right, bottom, left.
The shorthand property padding makes the rule above much shorter:
h1 {
background-color: #c0c0c0;
color: black;
padding: 15px 20px 30px 25px;
}For matching top/bottom and left/right padding, define padding with just two values:
h1 {
background-color: #c0c0c0;
color: black;
padding: 15px 25px;
}For equal padding on all sides, give the shorthand property padding a single value:
h1 {
background-color: #c0c0c0;
color: black;
padding: 25px;
}What about relative values, such as em lengths or percentages? Their results differ: em scales with the box content’s font size, while percentages relate to its total content width. To illustrate, create a page with two light-gray headings on a darker background:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Box ohne Eigenschaftsdefinitionen</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
body {
background-color: #808080;
color: black;
}
h1, h4 {
background-color: #c0c0c0;
color: black;
}
</style>
</head>
<body>
<h1>Hilfe! Ich stecke in einem Box-Modell fest!</h1>
<h4>Aber für eine gute, alte h4-Überschrift wie mich, ist es hier nicht zu eng. Im Gegenteil, es ist sogar recht geräumig.</h4>
</body>
</html>The code gives the page a dark-gray background and adds an element h4 using the same rule as the element h1.
The next figure shows this page in the browser.
Figure: Starting example without proportional padding.
Now give both headings relative padding of 1em:
body {
background-color: #808080;
color: black;
}
h1, h4 {
background-color: #c0c0c0;
color: black;
padding: 1em;
}As shown below, padding scales with each heading’s font size.
Figure: Relative padding with em.
em, a height measurement:
1em denotes the height of the capital M in the heading’s font. Since a heading h1 normally has a larger font than a heading h4, its padding is also larger despite both sharing the same rule.
This example shows what happens if we use percentages instead of em lengths:
body {
background-color: #808080;
color: black;
}
h1, h4 {
background-color: #c0c0c0;
color: black;
padding: 10%;
}Figure: Proportional padding using percentages.
Percentages produce much larger padding, as shown above. The rule gives both headings 10% padding. This refers to the element’s content width, but headings are blocks, whose content width equals the viewport width.
We colored the heading backgrounds to reveal otherwise invisible padding. This useful design trick exposes the box-model grid. The next figure shows the same page without heading backgrounds: their relative spacing remains, giving a harmonious design.
Figure: Padding without background colors.
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