Styling Web Pages with CSS 3
- Boxes with Multiple Backgrounds
- Borders Consisting of Images / Borders with Rounded Corners
- Drop Shadows / Box Decoration Breaks
Cascading Style Sheets (CSS)is a textual language for describing thestyles(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 amoduleand adds new features or extends features defined by CSS 2. The following list identifies four of these modules:
- CSS Backgrounds and Borders Module Level 3extends 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 Moduledescribes 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 3defines properties for text manipulation and specifies their processing model. It covers line breaking, justification and alignment, whitespace handling, and text transformation.
- CSS Transitionsdescribes 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 toCSS Backgrounds and Borders Module Level 3only. 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 candownload this article's code here.
Before delving into these features, it helps to have a basic understanding of theCSS box model, which describes the rectangularboxesgenerated for elements in a web page's Document Object Model tree. Check out Figure 1.
Figure 1A 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:
- backgroundspecifies all background properties in a single declaration.
- background-attachmentspecifies whether any supplied background images are fixed with regard to the viewport or scroll along with the element or its contents.
- background-clipspecifies the background's painting area. This property is new in CSS 3.
- background-colorspecifies an element's background color.
- background-imagespecifies an element's background image or images. The ability to specify multiple images is new in CSS 3.
- background-originspecifies the positioning area of background images. This property is new in CSS 3.
- background-positionspecifies the starting position of a background image.
- background-repeatspecifies the means by which a background image is repeated.
- background-sizespecifies 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 thebackground-imageproperty (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 forbackground-originandbackground-repeatfilled 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
Thebackground-clipproperty determines thebackground painting area, which is the area in which the background is painted. This property has the following syntax:
background-clip:[ , ]*
- 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 demonstratesbackground-clip.
Listing 1—Demonstrating thebackground-clipproperty
background-clip demo border-box
padding-box
content-box
Figure 2 shows the difference between eachbackground-clipvalue in the context of the Firefox browser.
Figure 2The background painting area is clipped to the border box, padding box, and content box
Background Images
Thebackground-imageproperty specifies an element's background image(s)—each image may have a transparent background. This property has the following syntax:
background-image:[ , ]*
Listing 2 demonstratesbackground-image.
Listing 2—Demonstrating thebackground-imageproperty
background-image demo
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 ofgif-favicon.com. The public domain forest image is courtesy ofGeorge Hodan.
background-repeat'sno-repeatvalue applies to all three image layers.background-position'scentervalue centers the butterfly, and itsright bottomvalue places the dahlia in the lower-right corner of the background forest image. The forest image is given the default value of0% 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 3A monarch butterfly is centered, and a dahlia is positioned on the bottom right
Background Origin
Thebackground-originproperty specifies thebackground positioning areafor elements rendered as single boxes or specifies which boxes thebox-decoration-breakproperty (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 thebackground-positionproperty's value is offset. This property has the following syntax:
background-origin:[ , ]*
- 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 demonstratesbackground-origin.
Listing 3—Demonstrating thebackground-originproperty
background-origin demo border-box
padding-box
content-box
Figure 4 shows the difference between eachbackground-originvalue in the context of the Chrome browser.
Figure 4背景是抵消5像素定位区域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
Thebackground-sizeproperty specifies the size of the background images via lengths, percentages, or either of the keywordscoverorcontain. This property has the following syntax:
background-size:[ , ]*
[| | auto ]{1,2} | cover | contain
If only one value is specified, the second value is assumed to beauto. When one value is defined and the other value isauto, the background image is scaled according to the specified length or percentage such that its intrinsic proportions (ratio) are maintained. (Ifbackground-sizeisn't specified,autois the default value for width and height. When both values areauto, the image is shown at its original size.)
Keywordcovercauses 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.
Keywordcontaincauses 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 demonstratesbackground-size.
Listing 4—Demonstrating thebackground-sizeproperty
background-size demo background-size: auto
background-size: 150px
background-size: 50% 25%
background-size: cover
background-size: contain
图5显示了si虎形象zed over its background area in the context of the Opera browser.
Figure 5The tiger image is courtesy ofWikimedia Commons