Web Development & Design Foundations
with HTML5
Tenth Edition
Chapter 3
Configuring Color and Text
with CSS
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-1
Learning Outcomes (1 of 2)
In this chapter, you will learn how to . . .
• Describe the evolution of style sheets from print media
to the Web
• List advantages of using Cascading Style Sheets
• Use color on web pages
• Create style sheets that configure common color and
text properties
• Apply inline styles
• Use embedded style sheets
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-2
Learning Outcomes (2 of 2)
• Use external style sheets
• Configure element, class, id, and contextual selectors
• Utilize the “cascade” in CSS
• Validate CSS
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-3
Overview of Cascading Style Sheets
(CSS)
See what is possible with CSS:
• Visit http://www.csszengarden.com
Style Sheets
• used for years in Desktop Publishing
• apply typographical styles
and spacing to printed media
CSS
• provides the functionality of style sheets (and much more) for web
developers
• a flexible, cross-platform, standards-based language developed by
the W3C.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-4
Figure 3.1 The power of a single CSS file
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-5
CSS Advantages
Greater typography and page layout control
Style is separate from structure
Styles can be stored in a separate document
and associated with the web page
Potentially smaller documents
Easier site maintenance
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-6
Types of Cascading Style Sheets
Inline Styles
Embedded Styles
External Styles
Imported Styles
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-7
Cascading Style Sheets (1 of 2)
• Inline Styles
– body section
– HTML style attribute
– apply only to the specific element
• Embedded Styles
– head section
– HTML style element
– apply to the entire web page document
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-8
Cascading Style Sheets (2 of 2)
• External Styles
– Separate text file with .css file extension
– Associate with a HTML link element in the head section
of a web page
• Imported Styles
– Similar to External Styles
– We’ll concentrate on the other three types of styles.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3-9
CSS Syntax
Style sheets are composed of “Rules” that describe the
styling to be applied.
Each Rule contains a Selector and a Declaration
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 10
Figure 3.2 Using CSS to set the text color to blue
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 11
CSS Syntax Sample
Configure a web page to display blue text and yellow
background.
body { color: blue;
background-color: yellow; }
This could also be written using hexadecimal color
values as shown below.
body { color: #0000FF;
background-color: #FFFF00; }
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 12
Common Formatting CSS Properties
• See Table 3.1 Common CSS Properties, including:
– background-color
– color
– font-family
– font-size
– font-style
– font-weight
– line-height
– margin
– text-align
– text-decoration
– width
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 13
Using Color on Web Pages
Computer monitors display color as intensities of red, green,
and blue light
RGB Color
The values of red, green, and blue vary from 0 to 255.
Hexadecimal numbers (base 16) represent these color
values.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 14
Figure 3.4 Color swatches and
hexadecimal color values
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 15
Hexadecimal Color Values
• # indicates a hexadecimal value
• Hex value pairs range from 00 to FF
• Three hex value pairs describe an RGB color
#000000 black #FFFFFF white
#FF0000 red #00FF00 green
#0000FF blue #CCCCCC grey
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 16
Figure 3.5 Partial color chart
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 17
Web Color Palette
• A collection of 216 colors
• Display the most
similar
on the Mac and PC
platforms
• Hex values:
00, 33, 66, 99, CC, FF
• Color Chart
http://webdevfoundations.net
/color Source: http://webdevfoundations.net/color
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 18
Making Color Choices
How to choose a color scheme?
• Monochromatic
– http://meyerweb.com/eric/tools/color-blend
• Choose from a photograph or other image
– http://www.colr.org
• Begin with a favorite color
– Use one of the sites below to choose other colors
– http://colorsontheweb.com/colorwizard.asp
– https://color.adobe.com/create/color-wheel
– http://paletton.com
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 19
Support Web Accessibility Verify
Sufficient Contrast
When you choose colors for text and background,
sufficient contrast is needed so that the text is easy to
read.
Use one of the following online tools to verify contrast:
• http://webaim.org/resources/contrastchecker
• http://snook.ca/technical/colour_contrast/colour.html
• http://juicystudio.com/services/luminositycontrastratio.
php
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 20
Configuring Color with Inline CSS
Inline CSS
• Configured in the body of the web page
• Use the style attribute of an HTML tag
• Apply only to the specific element
Example: configure red color text in an <h1> element:
<h1 style="color:#ff0000">Heading text is red</h1>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 21
Configuring Color with Inline CSS (2)
Example 2: configure the red text in the heading
configure a gray background in the heading
Separate style rule declarations with a ; symbol
<h1 style="color:#FF0000;background-
color:#cccccc">This is displayed as a red heading
with gray background</h1>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 22
CSS Embedded (Internal) Styles
Configured in the head section of a web page.
Use the HTML <style> element
Apply to the entire web page document
Style declarations are contained between the opening and
closing <style> tags
Example: Configure a web page with white text on a black
background
<style>
body { background-color: #000000;
color: #FFFFFF;
}
</style>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 23
Figure 3.10 The web page after
embedded styles are configured
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 24
CSS Embedded Styles
• The body selector sets the global style rules for the
entire page.
• These global rules are overridden for <h1> and <h2>
elements by the h1 and h2 style rules.
<style>
body { background-color: #E2FFFF;
color: #15495E;}
h1 { background-color: #237B7B;
color: #E2FFFF;}
h2 { background-color: #B0E6E6;
color: #237B7B;}
</style>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 25
Checkpoint 3.1
1. List three reasons to use CSS on a web page.
2. When designing a page that uses colors other than
the default colors for text and background, explain
why it is a good reason to configure style rules for
both text color and background color.
3. Describe one advantage to using embedded styles
instead of inline styles.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 26
Configuring Text with CSS
CSS properties for configuring text:
• font-weight
– Configures the boldness of text
• font-style
– Configures text to an italic style
• font-size
– Configures the size of the text
• font-family
– Configures the font typeface of the text
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 27
The font-size Property
Accessibility Recommendation: Use em or percentage font sizes–these
can be easily enlarged in all browsers by users
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 28
Figure 3.11 Common fonts
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 29
The font-family Property
• Not everyone has the same fonts installed in their
computer
• Configure a list of fonts and include a generic family
name
p { font-family: Arial, Verdana, sans-serif; }
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 30
Figure 3.14 CSS configures color and text
properties on the web page
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 31
Embedded Styles Example
<style>
body { background-color: #E2FFFF; color: #15495E;
font-family: Arial, Verdana, sans-serif; }
h1 { background-color: #237B7B; color: #E2FFFF;
line-height: 200%;
font-family: Georgia, "Times New Roman", serif;
text-indent: 1em;
text-shadow: 3px 3px 5px #000000; }
h2 { background-color: #B0E6E6; color: #237B7B;
font-family: Georgia, "Times New Roman", serif;
text-align: center; }
nav { font-weight: bold;
font-size: 1.25em;
word-spacing: 1em; }
p { font-size: .90em; text-indent: 3em; }
ul { font-weight: bold; }
</style>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 32
More CSS TEXT Properties (1 of 2)
• line-height
– Configures the height of the line of text
(use the value 200% to appear double-spaced)
• text-align
– Configures alignment of text within a block display
element
• text-indent
– Configures the indentation of the first line of text
• text-decoration
– Modifies the appearance of text with an underline,
overline, or line-through
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 33
More CSS TEXT Properties (2 of 2)
• text-transform
– Configures the capitalization of text
• letter-spacing
– Configures space between text characters
• word-spacing
– Configures space between words
• text-shadow
– Configures a drop shadow on text
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 34
CSS Selectors
CSS style rules can be configured for an:
• HTML element selector
• class selector
• id selector
• descendant selector
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 35
Using CSS with “class”
class Selector <style>
.new { color: #FF0000;
• Apply a CSS
font-style: italic;
rule to a certain “class” of
}
elements on a web page
</style>
• Does not associate the
style to a specific HTML element
Configure with .classname
• code CSS to create a class called “new” with red italic text.
Apply the class:
<p class=“new”>This is text is red and in italics</p>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 36
Using CSS with “id”
id Selector <style>
#new { color: #FF0000;
• Apply a CSS font-size:2em;
rule to ONE element font-style: italic;
on a web page. }
Configure with #idname </style>
• Code CSS to create an id called “new”
with red, large, italic text.
Apply the id:
<p id=“new”>This is text is red, large, and in italics</p>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 37
CSS Descendant Selector
Specify an element within the context of its container
(parent) element.
<style>
AKA contextual selector #content p { color: #00ff00;
The example configures a }
</style>
green text color only for
p tags located within an element assigned to the id
named content
Advantage of contextual selectors:
Reduces the number of classes and ids you need to
apply in the HTML
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 38
span element
Purpose:
• configure a specially formatted area displayed in-line
with other elements, such as within a paragraph.
There is no additional empty space above or below
a span – it is inline display.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 39
span Element Example
Embedded CSS:
<style>
.companyname { font-weight: bold;
font-family: Georgia, “Times New Roman”, serif;
font-size: 1.25em; }
</style>
HTML:
<p>Your needs are important to us at <span class=“companyname”
>Acme Web Design</span>.
We will work with you to build your website.</p>
Your needs are important to us at Acme Web Design. We will
work with you to build your website.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 40
External Style Sheets - 1
CSS style rules are contained in a text file separate
from the HTML documents.
The External Style Sheet text file:
• extension “.css”
• contains only style rules
• does not contain any HTML tags
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 41
External Style Sheets - 2
Multiple web pages can associate with the same external
style sheet file.
site.css
index.html
body {background-color:#E6E6FA;
color:#000000;
clients.html
font-family: Arial, sans-serif;
font-size:90%; }
h2 { color: #003366; }
about.html
nav { font-size: 16px;
font-weight: bold; }
Etc…
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 42
link Element
A self-contained tag
Placed in the head section
Purpose: associates the external style sheet file with the
web page.
Example:
<link rel=“stylesheet” href=“color.css”>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 43
Using an External Style Sheet
External Style Sheet color.css
body { background-color: #0000FF;
color: #FFFFFF;
}
To associate the external style sheet called color.css, the
HTML code placed in the head section is:
<link rel=“stylesheet” href=“color.css”>
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 44
Checkpoint 3.2
1. Describe a reason to use embedded styles. Explain
where embedded styles are placed on a web page.
2. Describe a reason to use external styles. Explain
where external styles are placed and how web pages
indicate they are using external styles.
3. Write the code to configure a web page to use an
external style sheet called “mystyles.css”.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 45
Centering Page Content with CSS
#container { margin-left: auto;
margin-right: auto;
width: 80%; }
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 46
Figure 3.23 The page content is centered
within the browser viewport
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 47
Figure 3.24 The “cascade” of Cascading
Style Sheets
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 48
W3C CSS Validation
http://jigsaw.w3.org/css-validator/
Partial screenshot of http://jigsaw.w3.org/css-validator
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 49
Summary
This chapter introduced you to Cascading Style Sheet
Rules associated with color and text on web pages.
You configured inline styles, embedded styles, and
external styles.
You applied CSS style rues to HTML, class, id, and
descendent selectors.
You are able to submit your CSS to the W3C CSS
Validation test.
Copyright © 2021, 2019, 2017 Pearson Education, Inc. All Rights Reserved 3 - 50