Home > Articles > Web Development > HTML/CSS

Styling Web Pages with CSS 3

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

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020