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 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:[ ,]*

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 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:[ ,]*

is either an image URI (e.g.,url(back.png)) ornone(nothing is drawn, but a layer is still created).

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:[ ,]*

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 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:[ ,]*

has the following syntax:

[|| auto ]{1,2} | cover | contain

refers to one or two values that denote image extents (e.g.,100pxor100px 50px). The first value is the width; the second value (when present) is the height. Similarly,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 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

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, simplyemailinformation@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 ourContact 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

皮尔森可能使用第三方网络趋势分析ervices, 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 theAccount page. If a user no longer desires our service and desires to delete his or her account, please contact us atcustomer-service@informit.comand we will process the deletion of a user's account.

Choice/Opt-out


用户总是可以做一个知情的选择时ther 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.e-skidka.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 toNevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read ourSupplemental privacy statement for California residentsin conjunction with this Privacy Notice. TheSupplemental privacy statement for California residentsexplains 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
  • 为了保护个人安全与安全,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
  • 培生公司和其他附属公司s 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


Pleasecontact usabout 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