Home > Articles > Web Development > HTML/CSS

Styling Web Pages with CSS 3

  • Print
  • + Share This
Cascading Style Sheets level 3 (CSS 3) introduces many new features that you'll want to leverage for your own web pages. Jeff Friesen introduces you to CSS Backgrounds and Borders Module Level 3's boxes with multiple backgrounds, borders consisting of images, borders with rounded corners, drop shadows, and box decoration breaks features.
Like this article? We recommend

Cascading Style Sheets (CSS) is a textual language for describing the styles (presentation semantics) of web pages written in HTML and related markup languages. CSS level 3 (CSS 3) introduces many new features that you'll want to leverage for your own web pages.

Unlike the large and monolithic CSS 2 specification, CSS 3 has been divided into several documents for easier maintenance. Each document is called a module and adds new features or extends features defined by CSS 2. The following list identifies four of these modules:

  • CSS Backgrounds and Borders Module Level 3 extends CSS 2.1 by introducing boxes with multiple backgrounds, borders consisting of images, borders with rounded corners, drop shadows, and box decoration breaks.
  • CSS Multi-column Layout Module describes multi-column layouts in CSS. Content can be flowed into multiple columns with a gap and a rule between adjacent columns.
  • CSS Text Module Level 3 defines properties for text manipulation and specifies their processing model. It covers line breaking, justification and alignment, whitespace handling, and text transformation.
  • CSS Transitions describes how CSS properties can be made to change smoothly from one value to another over a given duration. This contrasts with properties immediately changing from old to new property values.

For brevity, this article introduces you to CSS Backgrounds and Borders Module Level 3 only. You'll learn about the boxes with multiple backgrounds, borders consisting of images, borders with rounded corners, drop shadows, and box decoration breaks features. You can download this article's code here.

Before delving into these features, it helps to have a basic understanding of the CSS box model, which describes the rectangular boxes generated for elements in a web page's Document Object Model tree. Check out Figure 1.

Figure 1 A box is organized into a content area, padding, a border, and a margin

According to the box model, a box has a rectangular content area, a band of padding around the content, a border around the padding, and a margin outside the border—the margin might be negative. Margins have no influence on the background and border.

You can think of the rectangular content area as the context box; the padding; and content areas as the padding box; and the border, padding, and content areas as the border box.

Boxes with Multiple Backgrounds

A box has a background layer that may be fully transparent (the default setting) or filled with a color and/or one or more images. The background properties specify what color and images to use; and how they are sized, positioned, tiled, and so on.

CSS 3 provides the following background properties:

  • background specifies all background properties in a single declaration.
  • background-attachment specifies whether any supplied background images are fixed with regard to the viewport or scroll along with the element or its contents.
  • background-clip specifies the background's painting area. This property is new in CSS 3.
  • background-color specifies an element's background color.
  • background-image specifies an element's background image or images. The ability to specify multiple images is new in CSS 3.
  • background-origin specifies the positioning area of background images. This property is new in CSS 3.
  • background-position specifies the starting position of a background image.
  • background-repeat specifies the means by which a background image is repeated.
  • background-size specifies the size of the background image(s). This property is new in CSS 3.

The background layer is actually a sequence of layers, where the number of layers is determined by the number of comma-separated values specified for the background-image property (and the background color).

Each image is sized, positioned, and tiled according to the corresponding value in the other background properties. The lists are matched up from the first value; excess values at the end are not used. If a property doesn't have enough comma-separated values to match the number of layers, the browser calculates its value by repeating the list of values until there are enough.

Consider the following example:

background-image: url(flower.png), url(ball.png), url(grass.png);
background-position: center center, 20% 80%, top left, bottom right;
background-origin: border-box, content-box;
background-repeat: no-repeat;

This example has the same effect as the following example with the extra position dropped and the missing values for background-origin and background-repeat filled in (emphasized for clarity):

background-image: url(flower.png), url(ball.png), url(grass.png);
background-position: center center, 20% 80%, top left;
background-origin: border-box, content-box, border-box;
background-repeat: no-repeat, no-repeat, no-repeat;

The first image in the list (flower.png) is the layer closest to the user, the next one (ball.png) is painted behind the first, and so on. When present, the background color is painted below all the other layers.

Background Clipping

The background-clip property determines the background painting area, which is the area in which the background is painted. This property has the following syntax:

background-clip: <box> [ , <box> ]*

<box> is one of the following values:

  • border-box: The background is painted within (clipped to) the border box. This is the default.
  • padding-box: The background is painted within (clipped to) the padding box.
  • content-box: The background is painted within (clipped to) the content box.

Listing 1 demonstrates background-clip.

Listing 1—Demonstrating the background-clip property

<!DOCTYPE html>
<html>
  <head>
    <title>
      background-clip demo
    </title>
  </head>

  <body>
    <div style="width: 150px; height: 150px; padding: 20px; float: left; margin: 20px;
                background-clip: border-box; background-color: #ff0; border: dashed;
                border-width: 10px;">
      <code>border-box</code>
    </div>

    <div style="width: 150px; height: 150px; padding: 20px; float: left; margin: 20px;
                background-clip: padding-box; background-color: #ff0; border: dashed;
                border-width: 10px;">
      <code>padding-box</code>
    </div>

    <div style="width: 150px; height: 150px; padding: 20px; float: left; margin: 20px;
                background-clip: content-box; background-color: #ff0; border: dashed;
                border-width: 10px;">
      <code>content-box</code>
    </div>
  </body>
</html>

Figure 2 shows the difference between each background-clip value in the context of the Firefox browser.

Figure 2 The background painting area is clipped to the border box, padding box, and content box

Background Images

The background-image property specifies an element's background image(s)—each image may have a transparent background. This property has the following syntax:

background-image: <bg-image> [ , <bg-image> ]*

<bg-image> is either an image URI (e.g., url(back.png)) or none (nothing is drawn, but a layer is still created).

Listing 2 demonstrates background-image.

Listing 2—Demonstrating the background-image property

<!DOCTYPE html>
<html>
  <head>
    <title>
      background-image demo
    </title>
  </head>

  <body>
    <div style="background-image: url(butterfly.gif), url(dahlia.gif), url(forest.jpg); 
                width: 615px; height: 467px; background-repeat: no-repeat;
                background-position: center, right bottom; background-color: #0f0; 
                border-style: dashed; border-width: 10px; border-color: #ff0;">
    </div>
  </body>
</html>

This example presents three images: a butterfly on the topmost layer, a dahlia on the layer underneath, and a forest behind the butterfly and dahlia. The public domain butterfly and dahlia images are courtesy of gif-favicon.com. The public domain forest image is courtesy of George Hodan.

background-repeat's no-repeat value applies to all three image layers. background-position's center value centers the butterfly, and its right bottom value places the dahlia in the lower-right corner of the background forest image. The forest image is given the default value of 0% 0%, which positions it with its upper-left corner aligned with the upper-left corner of the box's padding box.

Figure 3 shows the butterfly and dahlia positioned over the forest in the context of the Internet Explorer browser.

Figure 3 A monarch butterfly is centered, and a dahlia is positioned on the bottom right

Background Origin

The background-origin property specifies the background positioning area for elements rendered as single boxes or specifies which boxes the box-decoration-break property (discussed later) operates on to determine the background positioning area(s) for elements rendered as multiple boxes. In other words, it determines the origin from which the background-position property's value is offset. This property has the following syntax:

background-origin: <box> [ , <box> ]*

<box> is one of the following values:

  • border-box: The position is relative to the border box.
  • padding-box: The position is relative to the padding box.
  • content-box: The position is relative to the content box.

Listing 3 demonstrates background-origin.

Listing 3—Demonstrating the background-origin property

<!DOCTYPE html>
<html>
  <head>
    <title>
      background-origin demo
    </title>
  </head>

  <body>
    <div style="width: 150px; height: 150px; padding: 20px; float: left; margin: 20px;
                background-origin: border-box; background-color: #ff0; border: dashed;
                border-width: 10px; background-image: url(circle.png); 
                background-position: 5px 5px; background-repeat: no-repeat;">
      <code>border-box</code>
    </div>

    <div style="width: 150px; height: 150px; padding: 20px; float: left; margin: 20px;
                background-origin: padding-box; background-color: #ff0; border: dashed;
                border-width: 10px; background-image: url(circle.png);
                background-position: 5px 5px; background-repeat: no-repeat;">
      <code>padding-box</code>
    </div>

    <div style="width: 150px; height: 150px; padding: 20px; float: left; margin: 20px;
                background-origin: content-box; background-color: #ff0; border: dashed;
                border-width: 10px; background-image: url(circle.png);
                background-position: 5px 5px; background-repeat: no-repeat;">
      <code>content-box</code>
    </div>
  </body>
</html>

Figure 4 shows the difference between each background-origin value in the context of the Chrome browser.

Figure 4 The background positioning area is offset 5 pixels from the upper-left corner of the outer edge of the border box, the outer edge of the padding box, and the outer edge of the content box

Background Sizing

The background-size property specifies the size of the background images via lengths, percentages, or either of the keywords cover or contain. This property has the following syntax:

background-size: <bg-size> [ , <bg-size> ]*

<bg-size> has the following syntax:

[ <length> | <percentage> | auto ]{1,2} | cover | contain

<length> refers to one or two values that denote image extents (e.g., 100px or 100px 50px). The first value is the width; the second value (when present) is the height. Similarly, <percentage> refers to one or two values that denote a percentage of the background positioning area (e.g., 60% 30%). The first value is a percentage of this area's width; the second value (when present) is a percentage of this area's height.

If only one value is specified, the second value is assumed to be auto. When one value is defined and the other value is auto, the background image is scaled according to the specified length or percentage such that its intrinsic proportions (ratio) are maintained. (If background-size isn't specified, auto is the default value for width and height. When both values are auto, the image is shown at its original size.)

Keyword cover causes the background image to be scaled while preserving the image's aspect ratio. The image is made as large as possible so that it completely covers the background positioning area. The image's width or height might exceed background positioning area dimensions; those parts of the image that exceed these dimensions will not be visible.

Keyword contain causes the background image to be scaled while preserving the image's aspect ratio. The image is kept as large as possible. Because it must be completely contained within the background positioning area (i.e., neither the image's width nor its height can exceed background positioning area dimensions), some parts of the background may not be covered by the image.

Listing 4 demonstrates background-size.

Listing 4—Demonstrating the background-size property

<!DOCTYPE html>
<html>
  <head>
    <title>
      background-size demo
    </title>
  </head>

  <body>
    <div style="width: 200px; height: 200px; float: left; margin: 20px;
                background-image: url(tiger.png); background-size: auto;
                background-color: #ff0; color: #fff; font-size: 1.5em;">
      <code>background-size: auto</code>
    </div>

    <div style="width: 200px; height: 200px; float: left; margin: 20px;
                background-image: url(tiger.png); background-size: 150px;
                background-color: #ff0; background-repeat: no-repeat;
                color: #fff; font-size: 1.5em">
      <code>background-size: 150px</code>
    </div>

    <div style="width: 200px; height: 200px; float: left; margin: 20px;
                background-image: url(tiger.png); background-size: 50% 25%;
                background-color: #ff0; background-repeat: no-repeat;
                color: #fff; font-size: 1.5em">
      <code>background-size: 50% 25%</code>
    </div>

    <div style="width: 200px; height: 200px; float: left; margin: 20px;
                background-image: url(tiger.png); background-size: cover;
                background-color: #ff0; background-repeat: no-repeat;
                color: #fff; font-size: 1.5em">
      <code>background-size: cover</code>
    </div>

    <div style="width: 200px; height: 200px; float: left; margin: 20px;
                background-image: url(tiger.png); background-size: contain;
                background-color: #ff0; background-repeat: no-repeat;
                color: #fff; font-size: 1.5em">
      <code>background-size: contain</code>
    </div>
  </body>
</html>

Figure 5 shows the tiger image sized over its background area in the context of the Opera browser.

Figure 5 The tiger image is courtesy of Wikimedia Commons

  • + Share This
  • 🔖 Save To Your Account