Chapter 03
Chapter 03
KEY CONCEPTS
1
Learning Outcomes
2
Learning Outcomes
In this lesson, 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
◦ Use external style sheets
◦ Configure element, class, id, and contextual selectors
◦ Utilize the “cascade” in CSS
◦ Validate CSS
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.
4
CSS
Advantages
5
Types of
Cascading Style Sheets
Inline Styles
Embedded Styles
External Styles
Imported Styles
6
Inline Styles
◦ body section
Cascading
◦ HTML style attribute
◦ apply only to the specific element
Style Sheets
Embedded Styles
◦ head section
◦ HTML style element
◦ apply to the entire web page document
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.
7
CSS Syntax
8
CSS Syntax Sample
9
The color property
Color configures the text (foreground) color of an element
body { color: blue; }
Text in the body will be blue
10
Common Formatting
CSS Properties
See Table 3.1 Common CSS Properties, including:
◦ background-color Background color
◦ color Foreground (text color)
◦ font-family Name of a font
◦ font-size Size of font
◦ font-style Style of font
◦ font-weight Boldness or weight of font
◦ line-height Spacing allowed for line of text
◦ margin Configure margin around element
◦ text-align Alignment of text
◦ text-decoration Determine if text (hyperlink) underlined
◦ width Width of content of an element
11
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.
12
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
13
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
14
Making Color Choices
15
Support Web Accessiblity
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
16
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
17
Configuring Color with Inline CSS (2)
<h1 style="color:#FF0000;background-color:#cccccc">This is
displayed as a red heading with gray background</h1>
18
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>
19
CSS Embedded Styles
• The body selector sets the global
style rules for the entire page.
21
Checkpoint 3.1
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.
Visitors may set their browsers to certain colors.
Change both text color and background color properties
provide good contrast between text and background.
22
Checkpoint 3.1
3. Describe one advantage to using embedded styles instead
of inline styles.
Embedded styles are coded once in the header section of the
web page and apply to the entire page.
This is more efficient than using inline styles to code
individual styles on HTML elements.
23
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
24
The font-size Property
Accessibility Recommendation: Use em or percentage font sizes – these can be
easily enlarged in all browsers by users
25
The font-family Property
26
Embedded Styles
Example
<style>
body { background-color: #E6E6FA;
color: #191970;
font-family: Arial, Verdana, sans-serif; }
h1 { background-color: #191970;
color: #E6E6FA;
line-height: 200%;
font-family: Georgia, "Times New Roman", serif; }
h2 { background-color: #AEAED4;
color: #191970; text-align: center;
font-family: Georgia, "Times New Roman", serif; }
p {font-size: .90em; text-indent: 3em; }
ul {font-weight: bold; }
</style>
27
More CSS TEXT
Properties
◦ 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
◦ 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
28
CSS Selectors
CSS style rules can be
configured for an:
◦HTML element selector
◦class selector
◦id selector
◦descendant selector
29
Using CSS with “class”
class Selector
◦ Apply a CSS <style>
rule to a certain "class" of .new { color: #FF0000;
elements on a web page font-style: italic;
◦ Does not associate the }
style to a specific HTML element </style>
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>
30
Using CSS with “id”
id Selector
<style>
◦ Apply a CSS #new { color: #FF0000;
rule to ONE element font-size:2em;
on a web page. font-style: italic;
Configure with #id name }
</style>
◦ Code CSS to create an id called “new”
with red, large, italic text.
31
CSS Descendant Selector
Specify an element within the context of its
container (parent) element.
AKA contextual selector <style>
#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
32
span element
Purpose:
◦ configure a specially formatted area displayed in-
line with other elements, such as within a
paragraph.
33
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 Web site.</p>
34
External Style Sheets - 1
35
External Style Sheets - 2
Multiple web pages can associate with
the same external style sheet file.
site.css
body {background-color:#E6E6FA; index.html
color:#000000;
font-family: Arial, sans-serif; clients.html
font-size:90%; }
h2 { color: #003366; }
nav { font-size: 16px; about.html
font-weight: bold; }
Etc…
36
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">
37
Using an External Style Sheet
External Style Sheet color.css
38
Checkpoint 3.2
1. Describe a reason to use embedded styles. Explain
where embedded styles are placed on a web page.
39
Checkpoint 3.2
2. Describe a reason to use external styles. Explain where external
styles are placed and how web pages indicate they are using
external styles.
External styles can be used to configure the text and color
formatting for some or all of the pages on a website in one
file.
This single file can be changed, and all the web pages associated
with it will display the new styles in the next time they are
rendered in a browser
External styles are placed in a separate text file that uses a .css file
extension.
Web pages use the <link> ta to indicate that they are using an
external style sheet.
40
Checkpoint 3.2
3. Write the code to configure a web page to use an external
style sheet called “mystyles.css”.
41
Centering Page Content with CSS
42
The “Cascade”
43
W3C CSS Validation
http://jigsaw.w3.org/css-validator/
44
Learning Outcomes
In this lesson, you learned 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
◦ Use external style sheets
◦ Configure element, class, id, and contextual selectors
◦ Utilize the “cascade” in CSS
◦ Validate CSS
45
Summary
46