Lecture 19: Introducing the CSS Box Model

Overview:

  1. The CSS Box Model
  2. border-related Properties
  3. padding-related Properties
  4. margin-related Properties
  5. width Property
  6. height Property
  7. The Windows IE 5.x Box Model
  8. Some Box Model Applications

The CSS Box Model

border-related Properties

Property Usage and Effect Values Accepted Recommendations
border-top-color Determines the color for the top border of an element. A pre-defined color name, hexadecimal value (e.g., #fff), an rgb value: rgb(255,0,0) or rgb(0%,50%,25%), or 'transparent' The border-color shorthand property is usually more efficient.
border-right-color Determines the color for the right border of an element. A pre-defined color name, hexadecimal value (e.g., #fff), an rgb value: rgb(255,0,0) or rgb(0%,50%,25%), or 'transparent' The border-color shorthand property is usually more efficient.
border-bottom-color Determines the color for the bottom border of an element. A pre-defined color name, hexadecimal value (e.g., #fff), an rgb value: rgb(255,0,0) or rgb(0%,50%,25%), or 'transparent' The border-color shorthand property is usually more efficient.
border-left-color Determines the color for the left border of an element. A pre-defined color name, hexadecimal value (e.g., #fff), an rgb value: rgb(255,0,0) or rgb(0%,50%,25%), or 'transparent' The border-color shorthand property is usually more efficient.
border-color Shorthand for border-top-color, border-right-color, border-bottom-color, and border-left-color properties. A pre-defined color name, hexadecimal value (e.g., #fff), an rgb value: rgb(255,0,0) or rgb(0%,50%,25%), or 'transparent'

If you specify one value (e.g., border-color: green;) the border-color is the same on all sides of the element

If you specify two values (e.g., border-color: green black;) the first value counts for top and bottom border-colors, while the second value counts for the left and right border-colors

If you specify three values (e.g., border-color: #fff #000 #eee;) the first value counts for the top border-color, the second value counts for the left and right border-colors, and the third value counts for the bottom border-color

If you specify four values (e.g., border-color: #fff #000 #eee #ccc;) the values progress clockwise around the element. The first value is used for the top border-color, the second value is used for the right border-color, the third value is used for the bottom border-color, and the fourth value is used for the left border-color
The default border-color is black.

If possible, use the 'border' shorthand (or a variant such as 'border-top') rather than this property; it is more efficient.
border-top-style Determines the style for the top border of an element. dotted | dashed | solid | double | groove | inset | outset | none The border-style shorthand property is usually more efficient.
border-right-style Determines the style for the right border of an element. dotted | dashed | solid | double | groove | inset | outset | none The border-style shorthand property is usually more efficient.
border-bottom-style Determines the style for the bottom border of an element. dotted | dashed | solid | double | groove | inset | outset | none The border-style shorthand property is usually more efficient.
border-left-style Determines the style for the left border of an element. dotted | dashed | solid | double | groove | inset | outset | none The border-style shorthand property is usually more efficient.
border-style Shorthand for border-top-style, border-right-style, border-bottom-style, and border-left-style properties. dotted | dashed | solid | double | groove | inset | outset | none

If you specify one value (e.g., border-style: double;) the border-style is the same on all sides of the element

If you specify two values (e.g., border-style: double solid;) the first value counts for top and bottom border-styles, while the second value counts for the left and right border-styles

If you specify three values (e.g., border-style: solid none dashed;) the first value counts for the top border-style, the second value counts for the left and right border-styles, and the third value counts for the bottom border-style

If you specify four values (e.g., border-style: solid none double solid;) the values progress clockwise around the element. The first value is used for the top border-style, the second value is used for the right border-style, the third value is used for the bottom border-style, and the fourth value is used for the left border-style
Dotted and dashed render as solid in Windows IE 5.01 and Netscape 4.x.

IE 6 renders 1px dotted borders as dashed; larger sizes display properly as dotted. IE 7 fixed this so that 1px dotted renders properly.

Some border styles need to have a width of a few pixels before they can render properly. For example, a 1px double border will render as a solid line because at least 3 pixels of width are necessary for proper rendering.

Browsers differ in how they color the tops and bottoms of borders. Extensive cross-browser, cross-platform testing is advised.

If possible, use the 'border' shorthand (or a variant such as 'border-top') rather than this property; it is more efficient.
border-top-width Determines the width for the top border of an element. Number and unit (e.g., px, em, %), as well as keywords (thin | medium | thick) The border-width shorthand property is usually more efficient.
border-right-width Determines the width for the right border of an element. Number and unit (e.g., px, em, %), as well as keywords (thin | medium | thick) The border-width shorthand property is usually more efficient.
border-bottom-width Determines the width for the bottom border of an element. Number and unit (e.g., px, em, %), as well as keywords (thin | medium | thick) The border-width shorthand property is usually more efficient.
border-left-width Determines the width for the left border of an element. Number and unit (e.g., px, em, %), as well as keywords (thin | medium | thick) The border-width shorthand property is usually more efficient.
border-width Shorthand for border-top-width, border-right-width, border-bottom-width, and border-left-width properties. Number and unit (e.g., px, em, %), as well as keywords (thin | medium | thick)

If you specify one value (e.g., border-width: 3px;) the border-width is the same on all sides of the element

If you specify two values (e.g., border-width: 3px 6px;) the first value counts for top and bottom border-widths, while the second value counts for the left and right border-widths

If you specify three values (e.g., border-width: 3px 6px 5px;) the first value counts for the top border-width, the second value counts for the left and right border-widths, and the third value counts for the bottom border-width

If you specify four values (e.g., border-width: 3px 6px 2px 4px;) the values progress clockwise around the element. The first value is used for the top border-width, the second value is used for the right border-width, the third value is used for the bottom border-width, and the fourth value is used for the left border-width
The border-width shorthand property is usually more efficient than specifying individual border widths for various sides.
border-top Combines the other border-related properties into one sequence that applies to the top border of an element. Sequence of Values: border-width border-style border-color

Note that each segment can only be given a single value.
Use the 'border' shorthand if all sides need to be consistent.
border-right Combines the other border-related properties into one sequence that applies to the right border of an element. Sequence of Values: border-width border-style border-color

Note that each segment can only be given a single value.
Use the 'border' shorthand if all sides need to be consistent.
border-bottom Combines the other border-related properties into one sequence that applies to the bottom border of an element. Sequence of Values: border-width border-style border-color

Note that each segment can only be given a single value.
Use the 'border' shorthand if all sides need to be consistent.

This can be used for links as an alternative to text-decoration; just be sure to specify 'text-decoration: none' to avoid a double effect.
border-left Combines the other border-related properties into one sequence that applies to the left border of an element. Sequence of Values: border-width border-style border-color

Note that each segment can only be given a single value.
Use the 'border' shorthand if all sides need to be consistent.
border Combines the other border-related properties into one sequence that applies to all border sides of an element. Sequence of Values: border-width border-style border-color

Note that each segment can only be given a single value.
Highly recommended (it is the shorthand of shorthands when it comes to border appearance, combining the 'border-top', 'border-right', 'border-bottom', and 'border-left' shorthand properties).

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Borders</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, h2 {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 18px;}
    h2 {font-size: 16px;}
    .outset {border: 3px outset #ccc;}
    .solid {border: 3px solid #000;}
    .dotted {border-left: 2px dotted #000; border-top: 2px dotted #000;}
    .top {border-top: 3px dashed blue;}
    .groove {border: 3px groove green;}
    .double {border-bottom: 6px double red;}
    .inset {border: 4px inset #000;}
    --></style>
</head>
<body>

<h1 class="outset">This Level 1 heading has a 3px #ccc outset border</h1>

<p class="solid">This paragraph has a 3px solid #000 border on all
sides</p>

<h2 class="dotted">This Level 2 heading has 2px #000 dotted borders on
its top and left sides.</h2>

<p class="top">This paragraph has a 3px dashed blue border on just its
top side</p>

<h2 class="groove">This Level 2 heading has 3px green groove borders</h2>

<p class="double">This paragraph has a red 6px double bottom border</p>

<p class="inset">This paragraph has a 4px inset #000 border</p>

</body>
</html>

See how this example renders

padding-related Properties

Property Usage and Effect Values Accepted Recommendations
padding-top Determines the padding space at the top of an element. Number and unit (e.g., px, em, %) Use the 'padding' shorthand if multiple sides need to be consistent.
padding-right Determines the padding space on the right side of an element. Number and unit (e.g., px, em, %) Use the 'padding' shorthand if multiple sides need to be consistent.
padding-bottom Determines the padding space at the bottom of an element. Number and unit (e.g., px, em, %) Use the 'padding' shorthand if multiple sides need to be consistent.
padding-left Determines the padding space on the left side of an element. Number and unit (e.g., px, em, %) Use the 'padding' shorthand if multiple sides need to be consistent.
padding Shorthand for padding-top, padding-right, padding-bottom, and padding-left properties. Number and unit (e.g., px, em, %)

If you specify one value (e.g., padding: 3px;) the padding is the same on all sides of the element

If you specify two values (e.g., padding: 3px 6px;) the first value counts for top and bottom padding, while the second value counts for the left and right padding

If you specify three values (e.g., padding: 3px 6px 5px;) the first value counts for the top padding, the second value counts for the left and right padding, and the third value counts for the bottom padding

If you specify four values (e.g., padding: 3px 6px 2px 4px;) the values progress clockwise around the element. The first value is used for the top padding, the second value is used for the right padding, the third value is used for the bottom padding, and the fourth value is used for the left padding
Highly recommended; the padding shorthand property is usually more efficient than specifying individual paddings for various sides.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Padding</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, h2 {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 18px;}
    h2 {font-size: 16px;}
    .outset {border: 3px outset #ccc; padding: 5px;}
    .solid {border: 3px solid #000; padding: 0 30px;}
    .dotted {border-left: 2px dotted #000; border-top: 2px dotted #000;
             padding: 5px 0 0 5px;}
    .top {border-top: 3px dashed blue; padding-top: 15px;}
    .groove {border: 3px groove green; padding: 20px 5px;}
    .double {border-bottom: 6px double red;}
    .inset {border: 4px inset #000;}
    --></style>
</head>
<body>

<h1 class="outset">This Level 1 heading has a 3px #ccc outset border
and padding of 5px on all sides</h1>

<p class="solid">This paragraph has a 3px solid #000 border on all
sides and padding of 30px on the left and right sides</p>

<h2 class="dotted">This Level 2 heading has 2px #000 dotted borders on
its top and left sides, as well as 5px padding on those sides.</h2>

<p class="top">This paragraph has a 3px dashed blue border on just its
top side and a top padding of 15px</p>

<h2 class="groove">This Level 2 heading has 3px green groove borders,
with padding of 20px on the top and bottom and 5px on the left and
right sides</h2>

<p class="double">This paragraph has a red 6px double bottom border</p>

<p class="inset">This paragraph has a 4px inset #000 border</p>

</body>
</html>

See how this example renders

margin-related Properties

Property Usage and Effect Values Accepted Recommendations
margin-top Determines the margin space above the element. Number and unit (e.g., px, em, %) Keep in mind that many block-level elements have default top margins greater than 0.

Overlapping vertical margins (top and bottom margins) from separate elements collapse into each other. Use the 'margin' shorthand if multiple sides need to be consistent.
margin-right Determines the margin space to the right of an element. Number and unit (e.g., px, em, %) Overlapping horizontal margins (right and left margins) from separate elements will concatenate (add together).

Use the 'margin' shorthand if multiple sides need to be consistent.
margin-bottom Determines the margin space below the element. Number and unit (e.g., px, em, %) Keep in mind that many block-level elements have default bottom margins greater than 0.

Overlapping vertical margins (top and bottom margins) from separate elements collapse into each other. Use the 'margin' shorthand if multiple sides need to be consistent.
margin-left Determines the margin space to the left of an element. Number and unit (e.g., px, em, %) Overlapping horizontal margins (right and left margins) from separate elements will concatenate (add together).

Use the 'margin' shorthand if multiple sides need to be consistent.
margin Shorthand for margin-top, margin-right, margin-bottom, and margin-left properties. Number and unit (e.g., px, em, %)

If you specify one value (e.g., margin: 3px;) the margin is the same on all sides of the element

If you specify two values (e.g., margin: 3px 6px;) the first value counts for top and bottom margin, while the second value counts for the left and right margin

If you specify three values (e.g., margin: 3px 6px 5px;) the first value counts for the top margin, the second value counts for the left and right margin, and the third value counts for the bottom margin

If you specify four values (e.g., margin: 3px 6px 2px 4px;) the values progress clockwise around the element. The first value is used for the top margin, the second value is used for the right margin, the third value is used for the bottom margin, and the fourth value is used for the left margin
Highly recommended; the margin shorthand property is usually more efficient than specifying individual margins for various sides.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Margin</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body, h1, h2 {font: 12px verdana,geneva,lucida,arial,sans-serif;}
    h1 {font-size: 18px;}
    h2 {font-size: 16px;}
    .outset {border: 3px outset #ccc; padding: 5px; margin: 0;}
    .solid {border: 3px solid #000;
            padding: 0 30px;
            margin: 10px 30px;}
    .dotted {border-left: 2px dotted #000; border-top: 2px dotted #000;
             padding: 5px 0 0 5px;}
    .top {border-top: 3px dashed blue; padding-top: 15px;}
    .groove {border: 3px groove green;
             padding: 20px 5px;
             margin: 40px;}
    .double {border-bottom: 6px double red; margin: 0;}
    .inset {border: 4px inset #000; margin: 0;}
    blockquote {border: 4px double #000;}
    --></style>
</head>
<body>

<h1 class="outset">This Level 1 heading has a 3px #ccc outset border
and padding of 5px on all sides; its default margin has been turned
off so only the 'body' margin is giving it space above (the
paragraph below sets up the margin space there)</h1>

<p class="solid">This paragraph has a 3px solid #000 border on all
sides and padding of 30px on the left and right sides; margin was
also at 30px on the left and right sides and 10px on the top
and bottom sides</p>

<h2 class="dotted">This Level 2 heading has 2px #000 dotted borders on
its top and left sides, as well as 5px padding on those sides.</h2>

<p class="top">This paragraph has a 3px dashed blue border on just its
top side and a top padding of 15px</p>

<h2 class="groove">This Level 2 heading has 3px green groove borders,
with padding of 20px on the top and bottom and 5px on the left and
right sides; margins were set at 40px on all sides</h2>

<p class="double">This paragraph has a red 6px double bottom border
and has its default margin turned off</p>

<p class="inset">This paragraph has a 4px inset #000 border and also
has its default margin turned off</p>

<blockquote>This blockquote was included so that its default margins
could be seen; it has a 4px double #000 border.</blockquote>

</body>
</html>

See how this example renders

width Property

Property Usage and Effect Values Accepted Recommendations
width Determines the horizontal space for content inside the element.

The total horizontal space occupied by the element is the sum of the width, left/right padding, and left/right border.
Number and unit (e.g., px, em, %) or 'auto' (the default) This is very useful, since by default block-level elements fill up their parent element and that is not always what you want.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Width</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font: 14px verdana,geneva,lucida,arial,sans-serif;}
    #box1 {width: 600px;
           padding: 10px;
           border: 5px double #000;}
    #box2 {width: 500px;
           padding: 20px;
           border: 5px groove #000;
           margin-top: 10px;}
    --></style>
</head>
<body>

<div id="box1">This division has a width for content of 600 pixels,
with padding on all sides of 10px and a border on all sides of 5px
double #000.  The total horizontal space occupied by the division
is 630 pixels (5 + 10 + 600 + 10 + 5)</div>

<div id="box2">This division has a width for content of 500 pixels,
with padding on all sides of 20px and a border on all sides of 5px
groove #000.  The total horizontal space occupied by the division
is 550 pixels (5 + 20 + 500 + 20 + 5).  The top margin of 10px
separates this from the division above.</div>

</body>
</html>

See how this example renders

height Property

Property Usage and Effect Values Accepted Recommendations
height Determines the vertical space for content inside the element.

The total vertical space occupied by the element is the sum of the height, top/bottom padding, and top/bottom border.
Number and unit (e.g., px, em, %) or 'auto' (the default) I seldom specify heights, because browsers do not enforce that as a maximum height. Specifying a height sets that as the minimum height, but sufficient amounts of content (or a user who has scaled text quite large) can then exceed that height.

As a result, I usually prefer to let content set the height of an element.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Height</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font: 14px verdana,geneva,lucida,arial,sans-serif;}
    #box1 {width: 600px;
           padding: 10px;
           border: 5px double #000;
           height: 200px;}
    #box2 {width: 500px;
           padding: 20px;
           border: 5px groove #000;
           margin-top: 10px;
           height: 10em;}
    --></style>
</head>
<body>

<div id="box1">This division has a width for content of 600 pixels,
with padding on all sides of 10px and a border on all sides of 5px
double #000.  The total horizontal space occupied by the division
is 630 pixels (5 + 10 + 600 + 10 + 5).  The height for content is set
at 200px, which ultimately totals 230 pixels (5 + 10 + 200 + 10 + 5).
</div>

<div id="box2">This division has a width for content of 500 pixels,
with padding on all sides of 20px and a border on all sides of 5px
groove #000.  The total horizontal space occupied by the division
is 550 pixels (5 + 20 + 500 + 20 + 5).  The top margin of 10px
separates this from the division above. The height is set at 10em,
which ultimately gets 50 pixels added to it from padding and border.
</div>

</body>
</html>

See how this example renders

The Windows IE 5.x Box Model

Some Box Model Applications

  1. Disabling the default spacing assigned to 'body': Be sure to set both margin and padding to 0 (some browsers have a default padding for body greater than zero).
  2. Inserting vertical space between list items: Apply to 'li'; margins work better here than padding. Useful because list items otherwise have no extra spacing between them and the extra space improves readability.
  3. Centering a block-level container on the page: Make sure the element has a width and then specify margin-right and margin-left values of 'auto', which splits the available horizontal space outside the element between the two sides (thus centering the element).

    Windows IE 5.x does not understand this and needs 'text-align: center' specified, which it then improperly uses to center the element. When using both approaches to accommodate all browsers note that the text-align will inherit down into the elements nested inside, so some overrides (setting content back to 'text-align: left') will be necessary.
  4. Turning off the blue borders around linked images: Setting the border for the image to either 'none' or '0' will disable the blue border. Old browsers (e.g., Netscape 4.x) do not understand this.
  5. Disabling margin space above/below headings and paragraphs in Gecko-based browsers and Opera: Browsers such as IE will generally disable the margin-top for a heading or paragraph if it is inside a table cell or division (that is just part of their internal stylesheet). Gecko-based browsers and Opera keep the default margin intact, which results in rendering differences cross-browser because in those browsers the content is pushed further down.

Sample code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Embedded CSS: Box Model Applications</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css"><!--
    body {font: 12px verdana,geneva,lucida,arial,sans-serif;
          margin: 0;
          padding: 0;
          text-align: center; /* for Windows IE 5.x centering */}
    h1 {font-size: 18px; font-weight: normal;}
    li {margin-bottom: 1em;}
    img {border: none;}
    .noTopMargin {margin-top: 0;}
    .ctr {margin: 0 auto 10px;
          width: 500px;
          text-align: left;
          border: 1px solid #000;}
     --></style>
</head>
<body>

<div class="ctr">
<h1 class="noTopMargin">This heading has had its default top margin
disabled in all browsers.</h1>

<ul>
    <li>List items with space between them</li>
    <li>List items with space between them</li>
    <li>List items with space between them</li>
</ul>
</div>

<div class="ctr">
<h1>This heading, however, still shows its default top margin.
Compare rendering in IE to Gecko-based browsers and Opera.</h1>

<p>The linked image below should not have a blue border:</p>

<a href="http://www.sydneyoperahouse.com/"><img src="sydney2.jpg"
alt="A view of the Sydney Opera House from a distance" width="113"
height="150" /></a>
</div>

</body>
</html>

See how this example renders