CSS and CSS3 20 Lessons To Successful Web Development
CSS and CSS3 20 Lessons To Successful Web Development
ISBN: 978-0-07-185021-6
MHID: 0-07-185021-X
TERMS OF USE
PART II CSS3
10 Introduction to CSS3
11 Using Selectors and Attribute
Selectors
12 Setting Backgrounds
13 Attaching Borders
14 Controlling Box Shadows,
Overflow, and Columns
15 Adding Colors and Opacity
16 Creating Text Effects and
Changing the Box Model
17 Linking to Web Fonts
18 Making 2D Transformations
19 Applying Specific
Transformations
20 Directing 3D Transformations
A Answers to the Self-Test
Questions
Index
Contents
Acknowledgments
Introduction
LESSON 1 Introduction to
CSS
How the Document Object Model
Works
Correct HTML Structure and
Nesting
About Cascading Style Sheets
Importing a Style Sheet
Importing CSS from Within
HTML
Embedded Style Settings
Using IDs
Using Classes
Summary
Self-Test Questions
LESSON 2 Learning the CSS
Rules
Selectors
IDs
Classes
Multiple Assignments
Comments
Single-Line Comments
Style Types
Default Styles
User Styles
External Style Sheets
Internal Styles
Inline Styles
It’s All About Separation of Style
and Content
Summary
Self-Test Questions
LESSON 3 Applying
Declarations to IDs and
Classes
Using IDs
A Specific Case
IDs Are Single-Use Only
Using Classes
Assigning Multiple Classes
Accessing IDs and Classes with
JavaScript
The O() Function
The S() Function
The C() Function
Using Hyphenated Property
Names
Why Use JavaScript?
Summary
Self-Test Questions
LESSON 4 Accessing
Selectors
The Type Selector
The Descendant Selector
The Child Selector
The ID Selector
Narrowing ID Scope
The Class Selector
Narrowing Class Scope
The Attribute Selector
The Universal Selector
Selecting by Group
For JavaScript Programmers
Points of Interest
Summary
Self-Test Questions
LESSON 5 Working with the
Cascade
Style Sheet Creators
Style Sheet Methods
Style Sheet Selectors
Calculating Specificity
When Rules Are Particularly
Important
So What Are Divs and Spans
Anyway?
Specifying Measurements
Pixels
Points
Inches
Centimeters
Millimeters
Picas
Ems
Exs
Percent
Summary
Self-Test Questions
LESSON 7 Manipulating
Color and Position
Colors
Using rgb()
Short Color Strings
Gradients
Linear Gradients
Radial Gradients
Controlling Color Stops
Repeating Gradients
An Example Document
Alpha Transparency
An Example Document
CSS3 Browser Incompatibilities
Older Browsers
Let George Do It
The prefixfree.js Utility
Positioning Elements
Absolute Positioning
Relative Positioning
Fixed Positioning
An Example Document
Summary
Self-Test Questions
PART II CSS3
LESSON 10 Introduction to
CSS3
Attribute Selectors
Improved Backgrounds
Enhanced Borders
Box Shadows
Element Overflow
Multicolumn Layout
Extended Color Handling and
Opacity
Additional Text Effects
Box Sizing
Resizing and Outlines
Web Fonts
Transitions and Transformations
Summary
Self-Test Questions
LESSON 12 Setting
Backgrounds
The background-clip Property
The background-origin Property
Compatibility Issues
The background-size Property
Using the auto Value
Multiple Backgrounds
Summary
Self-Test Questions
LESSON 13 Attaching
Borders
The border-color Properties
The border-style Properties
The border-image Properties
The border-radius Properties
Summary
Self-Test Questions
LESSON 18 Making 2D
Transformations
2D Transformations
The none Transformation
Type
The matrix(w,s1,s2,h,x,y)
Function
Argument 1
Argument 2
Argument 3
Argument 4
Argument 5
Argument 6
Internet Explorer Prior to IE9
Summary
Self-Test Questions
LESSON 20 Directing 3D
Transformations
The perspective Property
The transform-origin Property
About the Three Dimensions
The translate3d(x,y,z) Function
The scale3d(x,y,z) Function
The rotate3d(x,y,z,r) Function
The backface-visibility
Property
The transform-style Property
Transitions
The transition-property
Property
The transition-duration
Property
The transition-delay
Property
The transition-timing-
function Property
The cubic-bezier()
Function
Shorthand transition
Property Syntax
Summary
Self-Test Questions
Index
Acknowledgments
Basic CSS
Introduction to CSS
The example files from this book are in a file you can
download at 20lessons.com. The files for this lesson are
saved in it as embeddedstyles.htm, example.htm,
importedstyles.htm, importedstyles2.htm, styles.css,
styletag.htm, usingclasses.htm, and usingids.htm.
CORRECT HTML
STRUCTURE AND
NESTING
To follow recommended HTML structure and to
ensure your documents are readable by the
maximum number of browsers and other clients,
attribute values within tags should be contained
in either single or double quotation marks like
this: <a href=′http://yahoo.com′>, even
though nearly all browsers allow you to omit
them, like this: <a href=http://yahoo.com>.
You should also close (end) every tag, and do
so in the correct order. For example, you
shouldn’t close a document by issuing </html>
followed by </body> because the proper nesting
of tags would be broken by this reversal. The
correct way to close a document is with </body>,
followed by </html>.
ABOUT CASCADING
STYLE SHEETS
Using CSS you can apply styles to your web
pages to make them look exactly how you want.
This works because CSS is connected to the
DOM so that you can quickly and easily restyle
any element. For example, if you don’t like the
default look of the <h1>, <h2>, and other heading
tags, you can assign new styles to override the
default settings for the font family and size used,
or whether bold or italics should be set, and
many more properties too.
One way you can add styling to a web page is
by inserting the required CSS into the head of a
web page between the <head> and </head> tags.
So, to change the style of the <h1> tag you might
use the following CSS:
USING IDS
A better solution for setting the style of an
element is to assign an ID to it in the HTML, like
this:
Note the use of the # symbol, which specifies that only the
element that has an ID of highlight should be styled with
this statement.
USING CLASSES
If you would like to apply the same style to many
elements, you don’t have to give each one a
different ID because you can specify a class to
manage them all, like this:
<h1 class=′highlight′>Hello</h1>
SUMMARY
Now that you have learned some of the basics of
CSS, in Lesson 2, I’ll show you how CSS rules
work, and how you can apply them in different
ways.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
SELECTORS
Each CSS rule starts with a selector, which is the
thing upon which the rule is affected. For
example, in the CSS rule below, h1 is the selector
that is being given a font size 200 percent larger
than the default:
h1 { font-size:200%; }
Providing a new value to the font-size
property of the selector ensures that the
contents of all <h1> tags will be displayed at a
font size of 200 percent relative to the default
size. This is achieved by placing one or more
declarations within the { and } symbols that
follow the selector (in this case, font-
size:200%;). The part before the : (colon)
symbol is the property, while the remainder is
the value applied to it.
Lastly, there is a ; (semicolon) to end the
statement which, in this instance, is not required
(but would be required if another declaration
were to follow). For the sake of avoiding tricky to
track down errors, in this book I always include
the semicolons, even when they are not
necessary.
IDs
Selectors may be element types such as h1, img,
p, or table, or they can be ID or class names. ID
names are ones you provide to an element by
assigning a value to its id attribute, like this:
<p class=′intro′>
MULTIPLE ASSIGNMENTS
You can create multiple property assignments in
a couple of different ways. First, you can
concatenate them on the same line, like this:
h1 { font-size:200%; color:blue; }
h1 { font-size:200%;color:blue; }
Or you can space the assignments out a little
more, so that they line up below each other in a
column at the colons, like this:
COMMENTS
It is a good idea to comment your CSS rules,
even if you describe only the main groups of
statements rather than all or most of them. You
can do this in two different ways. First, you can
place a comment within a pair of /* and */
comment markers, like this:
Single-Line Comments
Although not in the CSS specification, I have
tested the JavaScript form of single line (or to-
end-of-line) commenting, and can report that it
works on all major browsers. You may like to try
it for yourself when you wish to quickly comment
out a single rule, but be aware that it is not
official, although since it is supported by all main
browsers, perhaps it will become so.
Anyway, to make a single-line CSS comment,
just place the tag // before the part of the line to
comment out, like this (shown in bold):
STYLE TYPES
There are a number of different style types,
ranging from the default styles set up by your
browser (and any user styles you may have
applied), through inline or embedded styles, to
external style sheets. The styles defined in each
type have a hierarchy of precedence, from low to
high.
Default Styles
The lowest level of style precedence is the
default styling applied by a web browser. These
styles are created as a fallback for when a web
page doesn’t have any styles, and they are
intended to be a generic set of styles that will
display reasonably well in most instances.
Pre-CSS, these were the only styles applied to
a document, and only a handful of them could be
changed by a web page (such as font face, color
and size, and a few element sizing arguments).
User Styles
These are the next highest precedence of styles,
and they are supported by most modern
browsers but are implemented differently by
each. In fact, these seem to be becoming a thing
of the past; Google Chrome, for example, has
even stopped supporting its custom.css file
(editing the contents changes nothing anymore),
although Microsoft’s Internet Explorer browser
still supports user styles, as shown in Figure 2-1.
FIGURE 2-1 Applying a user style sheet to
Internet Explorer
Inline Styles
Lastly, inline styles are where you assign CSS
declarations directly to an element. They also
have the highest precedence of any style type,
and are used like this (highlighted in bold):
<a href=′http://google.com′
style=′color:green;′>Visit Google</a>
<a href=′http://google.com′
class=′link′>Visit Google</a>
SUMMARY
You will now have a basic knowledge of CSS
rules and syntax, the various types of styles
available, and how to access them. In Lesson 3,
we’ll look into taking things a little further by
addressing elements in groups by class names,
and individually by ID names.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
1. In CSS, what is a selector?
USING IDS
The basic premise behind CSS is that it is better
to separate out the styling and layout
(presentation) of a web page from its content.
There are some good reasons for this:
A Specific Case
For example, in the case of assigning IDs,
suppose you are creating a news page with a
photograph. If you place the photo in a <div>
and give that <div> a unique ID, you can easily
style a frame for the photo that can be changed
at any time without going back and modifying
the main document contents.
What’s more, if your boss then asks you to
turn the photo into a slideshow, you can probably
write some JavaScript to access the ID and
create such an effect without having to make any
changes to the news item.
In this instance you might use HTML such as
the following:
USING CLASSES
More often than not you’ll find classes to be a
more useful way to reference elements than via
ID. This is because a class can apply to more
than one element and so, once you have decided
what parts will appear in a document, you can
simply apply classes to these parts and all
elements that are incorporated in them will
display in the same way.
For example, in the previous section, the IDs
that were assigned could just as well have been
classes. In which case the HTML might look like
this:
width:320px;
float:left;
myobject =
document.getElementById(′idname′)
<script src=′mainfunctions.js′></script>
myobject = O(′idname′)
O(′idname′).innerHTML = ′Replacement
HTML′
O(′idname′).style.color = ′blue′
S(′idname′).color = ′blue′
objarray = C(′heading2′)
O(′idname′).setAttribute(′style′, ′font-
size:24pt′)
SUMMARY
This lesson has brought you up-to-speed on using
IDs and classes both from the <style> section of
a document and from external style sheets, and
even from JavaScript. In Lesson 4 we move on to
looking at the CSS selectors you can use to
manipulate objects by ID and class.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
p { text-align:justify; }
h1 { font-size:26pt; }
THE DESCENDANT
SELECTOR
Descendant selectors let you apply styles to
elements that are contained within other
elements. For example, the following rule sets all
text within <b> elements to red, but only if that
text occurs within a <p> element (like this HTML:
<p><b>Hello</b> there</p>):
p b { color:red; }
ul li b { color:blue; }
THE CHILD SELECTOR
The child selector is similar to the descendant
selector but is more constraining about when the
style will be applied by selecting only those
elements that are direct children of another
element. For example, the following rule uses a
previously discussed descendant selector, which
will change any bold text within a paragraph to
red, even if the bold text is itself within italics
(like this <p><i><b>Hello</b> there</i></p>):
p b { color:red; }
p > b { color:red; }
THE ID SELECTOR
If you give an element an ID name (like this:
<div id=′mydiv′>), you can directly access it
from CSS in the following way, which changes all
the text in the <div> to italic:
#mydiv { font-style:italic; }
Narrowing ID Scope
You can narrow the scope of action of an ID by
specifying the types of elements to which it
should apply. For example, the following rule
applies the setting only to paragraphs that use
the ID myid:
p#myid { font-weight:bold; }
In this example only paragraphs using the ID
myid (like this: <p id=′myid′>) will receive the
new property value. Any other element types that
may try to use the ID (such as <div id=′myid′>)
will be ignored.
Remember, also, that the ID should be applied
only to a single element.
.myclass { margin-left:10px; }
p.main { text-indent:30px; }
THE ATTRIBUTE
SELECTOR
Many HTML tags support attributes, and using
this type of selector can save you from having to
use IDs and classes for referencing them. For
example, you can directly reference attributes in
the following manner, which sets all elements
with the attribute type=′submit′ to a width of
100 pixels:
[type=′submit′] { width:100px; }
THE UNIVERSAL
SELECTOR
The wildcard or universal selector matches any
element, so the following rule will make a
complete mess of a document by giving a green
border to all of its elements:
SELECTING BY GROUP
In CSS it is possible to apply a declaration block
to more than one element, class, or any other
type of selector at the same time by separating
the selectors with commas. Therefore, for
example, the following rule will place a dotted
orange line underneath all paragraphs, the
element with the ID of idname, and all elements
using the class classname:
Remember that to group selectors together, you must
separate them from each other with commas. If you forget
the commas, the rules will become descendant instead of
group and you won’t get the results you expect.
FOR JAVASCRIPT
PROGRAMMERS
If you don’t program with JavaScript, you can
skip this section (don’t worry; there won’t be any
questions about it at the end) and move on to the
summary. Otherwise stick with me and I’ll
explain a few interesting details about the
selectors.htm example and how it works.
Therefore, let’s go through the document a
section at a time, starting from the beginning:
This document is fairly standard in that it
starts with a <!DOCTYPE html> declaration, and
features an <html> and <head> tag, followed by a
<title> element. But then there’s an empty
<style></style> pair of tags, which is unusual.
This is here because the JavaScript will later fill
that style section with some rules.
Next there’s the set of CSS rules used by the
document to style the table—all straightforward
stuff, with some rule types you’ve already seen
and some you haven’t. We will get to all the rules
in this book so bite your tongue and hold on to
any questions—they will be answered. Although
if you are curious about the final two rules
(tr:nth-child()), they are how the alternating
colors are achieved for the table rows.
Next, there’s a large section of JavaScript in a
<script> element, which I’ll get to in just a
moment. First though I want to show you the end
of the document from the closing </head> tag
onward:
It’s not a lot, is it? That’s because the <body>
of the document is empty. Again, this is because
the JavaScript will take care of populating it.
Speaking of which, let’s take a look at the
programming behind this example that does just
that.
Inside the <script> tags all the JavaScript is
enclosed within the following structure:
document.body.innerHTML = output +
′</table>′
Points of Interest
What is particularly interesting to note from the
point of view of integrating JavaScript and CSS is
the way that this example inserts CSS rules into
a document using the insertRule() function.
This may be something you’ll have use for in a
future project.
Other things to note include replacing < and >
with < and > when you actually want to
display those characters in a document (and not
have them interpreted as being part of an HTML
tag), and the fact that it is possible to construct
the entire body section of a document from
JavaScript should you have reason for so doing.
Oh, and not forgetting that, using JavaScript
programs (such as this one) that are integrated
tightly with CSS and the DOM, you may need to
place your code within an onload event handler
to ensure that all the CSS and other things you
wish to access from JavaScript are actually ready
when you come to call on them.
SUMMARY
You now know how to apply all the various CSS
selectors to your web pages. Whether you need
to uniquely reference them by ID, in groups by
class, by their attributes, or by their
descendancy, or in any other way, you now have
the tools you need to precisely style any HTML
document. In Lesson 5 I’ll explain the term
Cascade in Cascading Style Sheets, and how it
applies to selectors.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
1. As inline styles
1. Referencing by individual ID
CALCULATING
SPECIFICITY
The specificity of a selector is calculated by
creating three-part numbers based on the
selector components in the numbered list in the
previous section. These compound numbers start
off looking like [0:0:0]. When a selector is
processed, each selector that has an ID
component increments the first number by 1, so
that the compound number would become
[1:0:0]. Let’s say that there is one ID reference in
a particular selector, so the compound number
becomes [1:0:0].
Then the number of class and attribute
components in the selector is placed in the
second part of the compound number. Let’s say
there are two of them, and so the number
becomes [1:2:0].
Finally, all selector components that reference
element tags are counted, and this number is
placed in the last part of the compound number.
Let’s say there is one, so the final compound
number becomes [1:2:1], which is all that is
needed to compare this selector’s specificity with
any another.
Let’s examine an example rule that would
fulfill this specificity:
p { font-weight:bold !important; }
SPECIFYING
MEASUREMENTS
CSS supports an impressive range of different
units of measurement, enabling you to tailor your
web pages precisely to specific values, or by
relative dimensions. The ones I generally use
(and believe you will also find the most useful)
are pixels, points, and percent.
Pixels
The size of a pixel varies according to the
dimensions and resolution of the user’s monitor.
One pixel equals the width/height of a single dot
on the screen, and so this measurement is best
suited to monitors. For example:
.classname { margin:5px; }
Points
A point is equivalent in size to 1/72 of an inch.
The measurement comes from a print design
background and is best suited for that medium,
but it is also commonly used on monitors. For
example:
.classname { font-size:14pt; }
.classname { width:3in; }
Centimeters
Centimeters are another unit of measurement
best suited for print. One centimeter is a little
over 28 points. For example:
.classname { height:2cm; }
Millimeters
A millimeter is 1/10th of a centimeter (or almost
3 points). Millimeters are another measure best
suited to print. For example:
.classname { font-size:5mm; }
Picas
A pica is another print typographic
measurement, which is equivalent to 12 points.
For example:
.classname { font-size:1pc; }
Ems
One em is equal to the current font size and is
therefore one of the more useful measurements
for CSS because it is used to describe relative
dimensions. For example:
.classname { font-size:2em; }
Exs
An ex is also related to the current font size; it is
equivalent to the height of a lowercase letter ‘x.’
This is a less popular unit of measurement that is
most often used as a good approximation for
helping to set the width of a box that will contain
some text. Some designers think of the ex as
being the height equivalent of using em units for
width.
For example:
.classname { width:20ex; }
Percent
This unit is related to the em, in that 1 em is
equivalent to 100 percent (when used on a font).
When not relating to a font, this unit is relative to
the size of the container of the property being
accessed. For example:
.classname { height:120%; }
SUMMARY
In this lesson you have learned about the
cascade, how to override the cascade when
necessary using the !important keyword, and
how to choose and specify exactly the
measurements you need for the sizes and
dimensions you require for the elements in your
web pages. In Lesson 6, we’ll move on to
manipulating fonts with CSS, and handling
typography in general.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
FONT FAMILY
This property assigns the font family for use. It
also lets you supply a list of font families in order
of preference from left to right, so that styling
can fall back gracefully when the browser
doesn’t have the preferred font family installed.
For example, to set the default font for <p>
elements you could use a CSS rule such as this:
p { font-family:Verdana, Arial,
Helvetica, sans-serif; }
FONT STYLE
With this property you can choose to display a
font in one of three ways. The following rules
create three classes (normal, italic, and oblique)
that can be applied to elements to create these
effects:
FONT SIZE
There are two ways you can change the size of
text. These are by relative amounts, or by
providing fixed values. A fixed setting looks like
the following rule, which sets the default
paragraph font size to 14 point:
p { font-size:14pt; }
FONT WEIGHT
This property specifies whether or not to display
a font in bold by assigning values of either
normal or bold, like this:
.bold { font-weight:bold; }
Text Decoration
You can use the text-decoration property to
apply underline, line-through, overline, and
blink effects to text. For example, the following
rule creates a new class called overline that
applies overlines to text. The weight of these
lines will be the same as that of the font:
.overline { text-decoration:overline; }
Horizontal Alignment
There are four types of text alignment available
in CSS: left, right, center, and justify. In the
following rule, default paragraph text is set to
full justification:
p { text-align:justify; }
.toupper { text-transform:uppercase; }
Text Indenting
With the text-indent property, you can indent
the first line of a block of text by an amount of
your choosing. For example, the following rule
indents the first line of every paragraph by 30
pixels, although a different unit of measurement
(perhaps a percent increase) could also be
applied:
p { text-indent:30px; }
SUMMARY
Having learned about basic font families, styles,
spacing, alignment, and so on, you will now be
fully proficient in making text display in the best
possible way for the designs you are working to.
In Lesson 7, we’ll continue our exploration by
looking at coloring and positioning text.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
COLORS
Colors can be applied to the foreground and
background of text and objects using the color
and background-color properties (or by simply
using the background property). These colors
can be named colors (such as black or white),
colors created from hexadecimal RGB strings
(such as #ffff00 or #446688), or colors created
with the rgb() and rgba() CSS functions.
The main 16 color names defined by the W3C
(w3.org) standards organization in HTML 4.01
include aqua, black, blue, fuchsia, gray, green,
lime, maroon, navy, olive, purple, red, silver,
teal, white, and yellow.
The following example uses a color name to
set the text color for an object with the ID of
intro to the color navy:
#intro { color:navy; }
#intro { background:teal; }
div { color:#ffff00; }
.blueonyellow { background:yellow;
color:blue; }
FIGURE 7-1 A range of text and background
colors, fills, and gradients
Using rgb()
If you don’t wish to work in hexadecimal (base
16), you can specify your colors using numbers
from 0 to 255 with the rgb() function, as in the
following rule, which changes the background
color of the current document to aqua (maximum
green, plus maximum blue):
Gradients
As well as filling in backgrounds in a solid color,
you can also apply a gradient, which will then
automatically flow smoothly from one color to
another (or more).
For example, here’s a rule to display an
orange gradient on the background of an
element called object:
Linear Gradients
In its simplest form, a linear gradient requires
only a start and end color, which it will then use
to fill the element from top to bottom, graduating
smoothly from the first to the last color, as shown
at the top right of Figure 7-1.
Radial Gradients
In a similar manner to linear gradients, you can
also create them radially, so that they flow out
from a given point. For example, the following
rule creates a green radial gradient, as shown in
the third box down on the left of Figure 7-1:
Repeating Gradients
When you want a gradient effect to repeat, it is
not necessary to provide several elements
alongside each other, each with its own gradient.
Instead you can make a gradient repeat multiple
times in a single element. To do this, you use the
repeating- prefix on either type of gradient
property name, like this (as shown in the top two
segments of the image in Figure 7-2):
FIGURE 7-2 Repeating linear and radial
gradients
An Example Document
The core CSS and HTML used to create Figure 7-
1 are very similar to the following web
document, which is extracted from the
colors.htm file in the accompanying archive.
In this example, you can see various CSS rules
are created in the <style> section of the
document. The only thing that’s new here is
assigning the text color of white to the three
rainbow classes, to make the text stand out from
the backgrounds.
Underneath the style section, the inclusion of
the prefixfree.js file in the <script> section
confers CSS compatibility across all browsers,
obviating the need to list any additional browser-
specific versions of CSS rules (see the section a
little later entitled “The preefixfree.js Utility”).
In the <body> of the document, the CSS rules
are then applied to <td> elements within a
simple <table>, although they could equally
have been applied to spans, divs, or any other
elements that support a background property.
Alpha Transparency
Gradients have one more little trick up their
sleeve, which is the ability to support
transparency, leading to an ever greater (and
more subtle) variety of effects. For example,
suppose you would like to fade a color in over an
image. One way to do this is to fade from a
transparent to a non-transparent color, which is
where the rgba() function (standing for red,
green, blue, and alpha transparency) comes into
its own, as shown here:
An Example Document
In the following example (extracted from
repandtrans.htm in the accompanying archive),
looking at the <style> section you can see that
the value url(′sunset.jpg′) is applied to the
background property of the element with the ID
of table. This sets the background of that
element to the image supplied (more on this in
Lesson 12). This is followed by rules for a linear,
and then a radial gradient (applied to the classes
linear and radial), and a rule for a linear
gradient with alpha transparency (plus the
height of the class is set to 200px), applied to the
class alpha:
As in the previous example document, the
rules are applied to <td> table cell elements, and
in this example the image is being applied to the
background of the entire table.
CSS3 BROWSER
INCOMPATIBILITIES
A few years ago, when CSS3 was still being
rolled out, each browser implemented things
slightly differently, and applied its own vendor-
specific names for many new CSS3 features. For
example, Apple led the development of a lot of
the CSS3 transitions, and so used the prefix -
webkit- for these properties. Then other
browsers implemented their own features, or
copied others, each with their own property
prefixes until, at one point, this meant that for
many rules you had to supply separate CSS for
each of the Opera, Chrome, Firefox, Safari,
Internet Explorer browsers, and more!
However, when Apple chose the Webkit
engine for its browser it became more
compatible with Chrome (which had also chosen
Webkit), and when Opera chose to use Chrome
as the base for its browser there was no longer
any need to consider modern versions of Opera
as different from Chrome.
Over time, many of the proprietary properties
became standardized, as browser developers
implemented the W3C standard names,
operation, and syntax of their versions of CSS,
and so the list of browsers needed to take into
account for any particular rule has, thankfully,
decreased.
Nevertheless differences remain because not
all properties have been standardized yet, even
though many of them haven’t changed for years,
while developers like you and me have had to
continue writing overly inflated web documents
to cover all inconsistencies, as we wait for
finalization of these standards into commonly
used property names.
Therefore, in this book I always make a note
when you should supply alternative rules for
particular browsers, based on the situation as it
stood at the time of publication.
Older Browsers
If you need to allow for browsers older than a
year or two (or thereabouts), then you will have
to supply the following browser-specific prefixes
for many CSS properties:
Let George Do It
When I was learning to program, the first
principle that was drummed into me was to “Let
George do it.” By this my teacher meant that I
should write code in which for every feature or
function I needed, I should assume that some
unknown person named George would write it
for me, so that I could concentrate on
formulating the core of my code. When you
follow this approach while programming, you’re
able to continue building the main framework of
a feature by leaving all the little intricacies and
fiddly bits to George.
Of course, once the code is finished, you must
then take the place of George and actually write
all those missing parts, but you can still leave
anything else you uncover for another George to
do. And so you proceed until as many levels of
Georgeness as are required have been traversed,
and there’s nothing left for George to do—and
thus your code is complete.
This is a good way to write your web
documents too. Whenever you feel the need for
something to be styled, give it a class or ID name
and leave the styling up to George. Later on you
can become George and implement the styling
required. Or you may be lucky enough to work in
a team where a real George will help out.
And you can also hand off parts of your CSS to
George too. You see, when you are assembling
your CSS rules and know that various browsers
may require different prefixes to the rule names,
or use slightly different arguments, you can leave
handling all that to George—just get on with
completing your style sheet first using the
standard property names, and don’t bother
yourself about dealing with all the possible
incompatibilities. That way you’ll finish the style
sheet much more quickly
Then, with your style sheet completed, you
now have a couple of options. You can become
George and work through all the
incompatibilities, adding alternate rules and
property names wherever required (rather a
tiresome task). Or you can take advantage of the
fact that your web surfers will all be using
computers to view your web pages, and let
George (their computer) do the hard work.
The prefixfree.js Utility
Therefore, let’s take a look at the prefixfree.js
utility (see Figure 7-3), which aims to automate
the process of properly prefixing property names
for you. You can find it at tinyurl.com/prefixfree,
where you can click the large circular icon at the
top left of the screen to download a JavaScript
file to be your George.
FIGURE 7-3 With prefixfree.js, you can
forget about CSS3 browser incompatibilities.
<script src=′prefixfree.js′></script>
Absolute Positioning
Therefore, for example, to move an object with
the ID of elem to an absolute location (by
assigning the value absolute to the position
property) that is 200 pixels down from the
document top and 200 pixels in from the left, you
can apply the following rules to it:
As well as pixels, you can specify the location
in any other valid CSS measurement, and values
can be offscreen too, even through the use of
negative values.
Relative Positioning
Instead of moving an object to an absolute
location, you can move it relative to the location
it would normally occupy in the normal
document, by setting the position property to
relative.
Therefore, for example, to move elem 25
pixels down and 25 pixels to the left of its normal
location, you would use the following rules:
Fixed Positioning
There is one further type of position you can use
by assigning the value fixed to the position
property. This moves the element to an absolute
location, but only within the current browser
viewport, and there it stays.
If the document is scrolled, the element will
remain exactly where it was placed, with the
scrolling occurring beneath it. For example, to fix
the object to the top-right corner of the browser
window, you could use the following rules:
An Example Document
The following example (extracted from
positioning.htm in the accompanying archive) is
how the preceding code comes together into an
HTML document. It should all be quite
straightforward, with the exception of the rule
applied to div elements. What is happening here
is that I have assigned a rule to change the
padding property of all <div> elements to 5
pixels, so that there is a small gap between the
edges and the contents.
I also chose not to add these 5 pixels to the
existing dimensions of each <div> (which would
make them bigger), and instead opted to take
those 5 pixels from their inner dimensions by
applying the border-box value to the box-
sizing property (more of which in Lesson 12):
SUMMARY
In this lesson you have learned how to apply
foreground and background colors to elements,
how to use linear and radial gradients instead of
solid colors, the use of repeating gradients and
alpha transparency, and how to precisely position
elements in the browser. In Lesson 8, we’ll move
on to examining pseudo-classes and the
shorthand CSS rules.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
INTRODUCING THE
PSEUDO-SELECTORS
The job of pseudo-selectors is to classify
elements using characteristics other than their
name, attributes, or other content that cannot be
deduced from the document tree. They include
the pseudo-selectors first-line, first-child,
and first-letter.
All pseudo-selectors are added to other
selectors with a : (colon) character, with the
exception of the new CSS3 pseudo-elements, for
which a :: (double colon) is intended to be used
instead.
However, the w3.org website
(tinyurl.com/w3colons) states: “The new CSS3
way of writing pseudo-elements is to use a
double colon to set them apart from pseudo-
classes [but] CSS3 still allows for single colon
pseudo-elements, for the sake of backwards
compatibility, and we would advise that you stick
with this syntax for the time being.”
Therefore, because the W3 recommends
keeping the old styling at the moment, that’s
what I do in this book until they say otherwise,
and so I use only the single colon form
throughout (but you should at least be prepared
that this might change in the future).
There is one exception to this though
(otherwise it wouldn’t be a good rule, would it?),
and that’s the new selection pseudo-element
(see “Miscellaneous Pseudo-selectors” later in
this lesson), which won’t work unless you use the
double colon. So my new rule in this book is to
only use double colons where they are strictly
necessary—there… fixed.
Anyway, for example, to create a class called
cap for emphasizing the size of the first letter of
an element, you would use a rule such as the
following:
When you apply the cap class to an element, the first
letter of any text in the element will be displayed according
to the declarations listed following the selector. In this
instance, an increase of five times text size will occur.
Actually, if you want the remaining text to neatly flow
around it, you also need to make the first letter float to the
left too, as follows:
Link-Related Pseudo-Classes
CSS2 pseudo-classes include hover, link,
active, and visited, all of which are mostly
useful for applying to anchor elements, as in the
following rules, which set the default color of all
links to blue, links that have already been visited
to navy, links that are in the process of being
clicked to red, and links that are being hovered
over by the mouse to cyan:
a:active { color:olive; }
Accessing Elements
Numerically
Now we get to really start having some fun with
CSS by accessing elements by their numerical
position within a parent element using a range of
position-related pseudo-classes, starting with
first-child which, as you would expect, simply
accesses the first child element of a parent,
whatever type of element it may be.
For example, if you are displaying text in such
a way that all paragraphs should be indented
except for the first one, you can achieve this as
follows (which also shows the CSS rules to
format the example text in general):
The first p rule sets all paragraphs to have a
text indent of 30 pixels, and removes all margins
around paragraphs. The second applies the
first-child pseudo-class in which the text-
indent value is set to 0. The result is that all
paragraphs will have a 30-pixel indent applied
except the first one.
Here is the example text that, when the
preceding rules are applied to it, results in
Figure 8-4. As you can see, there is nothing in
the example that indicates this type of formatting
should occur, so we have total separation of
content and styling—always the goal when using
CSS to its best advantages:
FIGURE 8-4 Unlike the rest, the first
paragraph is not indented.
p:first-of-type { text-indent:0; }
ul li:nth-child(5) { color:blue; }
You can also select numerically by type in the
following manner, which will uniquely reference
only the 5th paragraph in a div (and not just the
5th element):
ul li:nth-last-child(5) { color:blue; }
Using Expressions
As well as giving single numeric values to these
types of pseudo-class, you can provide an
expression to give even more interesting control,
by supplying the n part of nth. For example, to
reference all the odd items in a list, you could
use a rule such as this:
ul li:nth-child(2n+1) { color:red; }
ul li:nth-child(2n+2) { color:red; }
ul li:nth-child(2n) { color:red; }
ul li:nth-child(3n) { color:red; }
ul li:nth-child(3n+1) { color:red; }
ul li:nth-child(3n+2) { color:red; }
You can also supply equations to all the other nth rules,
and even a negative value to n (to select only a range of
elements at the start of a list), or you can use negative
values in the second term of the equation too. However, to
avoid overcomplicating matters, I won’t take this any
further, and will leave it up to you to invent any weird and
wonderful selections you may need.
Relational Selectors
A couple of pseudo-classes let you select either
according to whether an element is empty or
whether it doesn’t contain another class or ID.
For example, you can style a <div> with the ID of
mydiv when it is empty like this (which in this
instance makes it half invisible):
#mydiv:empty { opacity:0.5; }
:not(img) { margin-left:20px; }
#myid:not(img) { margin-left:20px; }
div:not(.info) { margin-left:20px; }
Miscellaneous Pseudo-
Selectors
You can also style elements in a range of
miscellaneous ways. For example, there are the
enabled and disabled pseudo-classes that will
reference an element such as an input field
either if it has been enabled, or disabled, like
this:
#latest:target { font-weight:bold; }
Finally, there’s the pseudo-selector out I
mentioned near the start that requires a double
colon in front of it, namely the selection
pseudo-element. Using this selector, you can
modify the color, background, background-
color, and text-shadow properties of text
selected by dragging the mouse, or using SHIFT
with the cursor keys.
For example, because of the * universal
selector, the following rule changes the default
white-on-blue of all selections in a document to
yellow-on-red:
SHORTHAND PROPERTIES
Related CSS properties can be concatenated into
shorthand properties to save your time and make
your CSS easier to follow, but this does come
with a caveat. Whenever you supply a shorthand
property, you are, by implication, assigning
default values for any properties you do not
assign. So if you, for example, set only three of
five possible properties, then the remaining two
properties will have the default values selected,
rather than having undefined values.
As long as you are aware of this caveat,
though, you can use shorthand properties
liberally. Just replace any such shorthand
properties you have problems with by a set of
non-shorthand properties if you encounter
problems.
border:2px dotted;
• border-width
• border-style
• border-color
Background Shorthand
Backgrounds have five main longhand properties
that can be concatenated into a shorthand
property:
• background-color
• background-image
• background-repeat
• background-position
• background-attachment
#example { background:#eee
url(′back.png′) no-repeat 60px 60px
fixed; }
Font Shorthand
There are five longhand font properties that can
be brought together into a shorthand property,
but there are some strict rules you must follow
for this to work successfully. These properties
are as follows:
• font-style
• font-variant
• font-weight
• font-size/line-height
• font-family
• margin-top
• margin-right
• margin-bottom
• margin-left
Or for padding:
• padding-top
• padding-right
• padding-bottom
• padding-left
#example { padding:10px; }
List Shorthand
Finally there’s shorthand available for lists,
which take the following longhand properties:
• list-style-type
• list-style-position
• list-style-image
ul li { list-style:square inside
url(hand.png) }
SUMMARY
Even though many of the properties in this
lesson are not fully explained until later on, it has
given you a good grounding in some very subtle
(yet powerful) ways of using CSS, and has also
shown how you can save time and make your
CSS code easier to follow by using shorthand
properties.
In Lesson 9 we’ll move on to examining the
box model, which is the way CSS looks at HTML
web pages when it decides how to apply the
rules you give it.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
body { margin:0px; }
body { margin:0; }
Setting Margins
The margin is the outermost level of the box
model. It separates elements from each other
and its use is quite smart. For example, assume
you have chosen to give a number of elements a
default margin of 10 pixels around each. When
placed on top of each other, this would create a
gap of 20 pixels due to adding the widths
together.
However, to overcome this potential issue,
when two elements with margins are directly one
above the other, only the larger of the two
margins is used to separate them. If both
margins are the same size, just one of the sizes is
used. This is called margin collapse, and it
results in layout looking more natural than if
there were no collapsing. However, you should
note that the margins of absolutely positioned or
inline elements do not collapse.
The margins of an element can be changed en
masse with the margin shorthand property, or
individually with margin-top, margin-right,
margin-bottom, and margin-left. As described
in Lesson 8, when setting the margin property,
you can supply between one and four arguments,
which have the effects commented in the
following declarations (and which apply in
clockwise order from the top when four values
are supplied):
Using Borders
The border level of the box model is similar to
the margin except that there is no collapsing. It
is the next level as we move from the outside of
the box in toward the content area. The main
properties used to modify borders are border,
border-top, border-right, border-bottom, and
border-left, and each of these can have other
sub-properties added as suffixes, such as -color,
-style, and -width. So, for example, border-
left-width sets only the width of the left border.
The four shorthand ways of accessing
individual property settings used for the margin
property also apply with the border-width
property, so all the following are valid
declarations:
Figure 9-4 (created from the file borders.htm
in the accompanying archive) shows the result of
the following addition to the <style> section of
the previous example, the result being very
similar except that an orange border now takes
the place of the margin:
FIGURE 9-4 The border now occupies the
same space that the margin did previously.
Applying Padding
The deepest of the box model levels (other than
the contents of an element) is the padding, which
is applied inside any borders and/or margins. The
main properties used to modify padding are
padding, padding-top, padding-right,
padding-bottom, and padding-left.
The four ways of accessing individual property
settings used for the margin and border
properties also apply with the padding property,
so all of the following are valid declarations:
Non-image Elements
So far we’ve looked at applying margins,
borders, and padding to images, but what
happens when we do this with a <div> element,
for example? Well, take a look at the following
example (saved as div1.htm in the accompanying
archive), which results in Figure 9-6:
FIGURE 9-6 The <div> has forced new lines
above and below it.
display:inline-block;
img { vertical-align:top; }
SUMMARY
This lesson marks the end of your introduction to
the basics of CSS. You can now style your HTML
documents in an incredible variety of ways, and
have learned how to precisely control colors,
location, dimensions, and much more. Starting
with Lesson 10, we’ll be looking at the more
advanced and exciting features that CSS3 offers,
including transitions, animations, and
transformations.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
CSS3
Introduction to CSS3
ATTRIBUTE SELECTORS
As well as the standard selector types already
covered in this book, there are three
enhancements in CSS3 that enable you to more
easily match elements based on the contents of
their attributes using the three new operators: ^,
$, and *. As you will see in Lesson 11, these let
you select from the start or end of a matching
element, and use wildcards in your matches.
IMPROVED
BACKGROUNDS
New properties have been added to backgrounds
in CSS3, for clipping, changing the origin of a
background image, and specifying how borders
are to be displayed relative to background
images. Additionally CSS3 now supports multiple
background images, as shown in Figure 10-1,
and created with the following HTML (saved as
multiplebackgrounds.htm in the accompanying
archive):
FIGURE 10-1 This certificate employs eight
background images.
BOX SHADOWS
Shadows can now be placed behind elements in
CSS3, as shown in Figure 10-3, in which an
image of a clock is shown without any extra
styling, and then is repeated but with a box
shadow added, using this HTML (saved as box-
shadow.htm in the accompanying archive):
FIGURE 10-3 The second instance of the
image has been given a box shadow.
ELEMENT OVERFLOW
Overflow is where text is too large to fit within
an element, and the way overflowing of element
contents is handled is vastly improved in CSS3
with numerous options now available. For
example, Figure 10-4 shows an element without
any overflow control, and then with the overflow
property set to hidden, using this HTML (saved
as overflow.htm in the accompanying archive):
FIGURE 10-4 The right-hand element has all
overflowing content hidden.
MULTICOLUMN LAYOUT
Compatible browsers supporting the new CSS3
column properties will neatly flow contents
across columns, as shown in Figure 10-5, in
which the first few paragraphs of this lesson
have been flowed across four columns using the
following HTML (saved as multiplecolumns.htm
in the accompanying archive):
FIGURE 10-5 Flowing text across columns
EXTENDED COLOR
HANDLING AND OPACITY
CSS3 brings several new ways of defining colors
using primary colors, or hue, saturation, and
luminance, as well as supporting the use of alpha
transparency, which is fully detailed in Lesson
15. Figure 10-6 shows the range of possible
colors available using these techniques, in the
form of a color circle. Even though monochrome
versions of this book do not do it justice, you can
just make out the light ‘Y’ shape that separates
the three primary colors, and also see how the
luminance gradually increases from the center to
the rim.
FIGURE 10-6 A color wheel displaying both
hue and saturation
ADDITIONAL TEXT
EFFECTS
New text effects are available in CSS3, including
shadows (as shown in Figure 10-7, which uses
the HTML below—text-shadow.htm in the
accompanying archive), plus overflow handling
(as previously described), and more precise
control over word wrapping:
FIGURE 10-7 Adding a shadow to text
BOX SIZING
You saw this new property briefly described in
the previous lesson. It allows you to decide
whether the attributes you apply to an element
such as padding should appear outside the
dimensions of a specified element, or if the
padding or other attribute should be applied
inside, thus reducing the space available for the
content.
WEB FONTS
With CSS3 you can now have your own fonts
display on your web pages or, far more
conveniently, you can use those supplied by a
third party, as with the vast collection of over
600 Google fonts shown in Figure 10-8, and
explained in Lesson 17.
FIGURE 10-8 Accessing the several hundred
freely available Google fonts
TRANSITIONS AND
TRANSFORMATIONS
You can also use CSS3 to resize HTML elements,
rotate them in all three dimensions, and even
animate these transformations into smooth
transitions. Figure 10-9 shows an image of an
iPad in its original form, and then rotated by 75
degrees in three dimensions, using the following
HTML (saved as 3drotation.htm in the
accompanying archive):
FIGURE 10-9 An element is rotated by 75
degrees in 3D space around the X, Y, and Z
axes.
Lessons 18 to 20 cover all aspects of
transitions, transformations, and other 3D
effects.
SUMMARY
CSS3 comprises a huge number of powerful and
flexible additions and improvements to CSS,
bringing features previously not even possible
using JavaScript, but now directly controllable
with just a few lines of CSS. In Lesson 11, I will
begin explaining them in detail.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
SUMMARY OF SELECTOR
TYPES
There are several types of selectors in CSS,
enabling you to approach styling elements from a
variety of different standpoints. Some of these
selectors you have already learned about, and
some you haven’t, but as they are the
cornerstone of CSS, I have summarized them
here.
body * { color:#555; }
Type Selectors
Type selectors refer to types of HTML elements
such as <b> elements, or a <div> or <span>, and
so on, like this:
b { color:red; }
Class Selectors
When you want to apply sets of rules to many
different elements that may or may not be of
different types, you can create a class. The rules
for the class are then applied to any element to
which you add the class.
Class names commence with a period, as in
the following example, which will set the color of
text to blue for all elements that use the class
headlines:
.headlines { color:blue; }
ID Selectors
To uniquely target single elements, you can give
them an ID name and create rules just for that
element, as in the following example, which uses
the # (known as the hash or pound symbol) to
identify the target as being an ID, setting the
font weight of the target with the ID of username
to bold:
#username { font-weight:bold; }
Welcome: <span
id=′username′>FlorenceNightingale123</spa
n>.
Descendant Selectors
Moving on in complexity (or maybe I should say
flexibility), you can select the descendant
elements of a parent element by separating their
names with a space character. These selectors
can include type selectors such as <span>s or
<div>s, classes, IDs, or the universal selector.
In the following example, all <em> elements
that occur anywhere within <span> elements will
have their text color set to green:
span em { color:green; }
Child Selectors
If necessary, you can be more precise with your
descendant selectors by turning them into child
selectors, such that only direct children of the
parent that fit the rule will be styled. In this
example, only direct <em> children of <div>
elements will have their background color set to
lime because of the use of the > (greater than)
symbol between them:
i + b { color:gray; }
p + p {text-indent:1.5em; }
General Sibling Selectors
New to CSS3, you can now generalize your
styling of siblings by allowing them to occur
anywhere within the same parent element (as
long as the second element comes after the first),
and still have them counted as adjacent. To do
this, use the ~ (tilde) symbol to separate the two,
as in the following rule, which styles all <b>
elements that are general siblings of (and
preceded by) <i> elements:
i ~ b { color:olive; }
Attribute Selectors
Using attribute selectors, you can target
elements according to the attributes they have
been given. For example, the following rule will
set any elements whose href attribute value
contains exactly info.htm. I explain attribute
selectors in greater detail following this
summary of selectors, including how to search
within attribute strings too, using CSS3
enhancements:
a[href=′info.htm′] { color:red; }
Pseudo-Classes
With pseudo-classes you can apply dynamic
interactivity to your web pages. They are
selectors that often interact dynamically with
user input, as with the following example, which
sets the hover state of all links to display the link
in bold text:
a:hover { font-weight:bold; }
Pseudo-Elements
Using pseudo-elements, you can even modify
elements to an extent by adding content before
and after them, for example, or, as in the
following case, by changing the size of the first
letter of all <p> elements to 300 percent larger
than the current setting:
p::first-letter { font-size:300%; }
ATTRIBUTE SELECTORS
With CSS3 it has been decided that most of these
selectors work just fine the way they are, but
three enhancements in particular have been
made to attribute selectors so that you can more
easily match elements based on the contents of
their attributes.
For example, as previously explained, in CSS2
you can use a selector such as
a[href=′info.htm′] to match the exact value
info.htm when found in an href attribute, but
there’s no way to match only a portion of a
string, which is where the following
enhancements come into play.
The ^= Operator
CSS3 comes to the rescue with three new
operators: ^=, $=, and *=. Using one of them you
can match the start, end, or any part of a string,
respectively. For example, the following will
match any <a> element that has an href
attribute whose value begins with the string
http://mysite:
a[href^=′http://mysite′]
The $= Operator
Alternatively, to match only at the end of a string,
you can use a selector such as the following,
which will match any <img> element whose src
attribute has a value that ends with .png:
img[src$=′.png′]
The *= Operator
Or, to match any substring, you can use a
selector such as the following to select any <a>
elements that have an href attribute with a value
that contains the string google anywhere within
it:
a[href*=′google′]
For example, the element <a
href=′http://google.com′> will match, while
<a href=′http://gmail.com′> will not.
Figure 11-2 shows these three new operators
in use, with example CSS, HTML, and the results
displayed.
Figure 11-2 The CSS3 attribute selectors
SUMMARY
Now that you are armed with all the tools you
could ever want to select and style elements in
every conceivable way, in the following lessons
we’ll move on to using these selectors to apply
CSS rules, starting with the new background
capabilities of CSS3.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
THE BACKGROUND-CLIP
PROPERTY
This property specifies whether the background
should be ignored (clipped) if it appears within
either the border or padding area of an element.
For example, the following declaration states
that the background may display in all parts of
an element, all the way to the outer edge of the
border:
background-clip:border-box;
To restrict the background from appearing
within the border area of an element, you can
restrict it to only the section of an element inside
the outer edge of its padding area, like this:
background-clip:padding-box;
background-clip:content-box;
background-origin:border-box;
background-origin:padding-box;
background-origin:content-box;
COMPATIBILITY ISSUES
As is often the case in the ever-evolving field of
web development, some browsers handle things
differently. For example, older versions of the
Firefox and Safari browsers have their own
property names.
The standard property names are
background-clip and background-origin, but
on older Firefox (and other Gecko-based
browsers) they are called -moz-background-
clip and -moz-background-origin. While on
older Safari browsers (and others based on the
Webkit rendering engine), their names are -
webkit-background-clip and -webkit-
background-origin.
Therefore, when you use the background-
clip property, if you wish your CSS rules to
apply on as wide a range of browsers as possible,
you may choose to also use the alternate
property names to ensure that they recognize
what you want to achieve. You should also take
into account that older versions of Firefox and
Safari use the nonstandard values of border,
padding, and content for these properties,
instead of border-box, padding-box, and
content-box.
background-size:100px auto;
MULTIPLE BACKGROUNDS
With CSS3 you can now attach multiple
backgrounds to an element, each of which can
use the previously discussed CSS3 background
properties. Figure 12-2 shows an example of a
certificate, in which eight different images have
been assigned to the background, to create the
four corners and four edges of the certificate
border.
FIGURE 12-2 This framed photo comprises a
div with eight different background images.
To display multiple background images in a
single CSS declaration, you separate them with a
comma. For example, here is the CSS that was
used to create the border in Figure 12-2, with
plenty of white space and formatting added to
make it clear what is happening.
SUMMARY
You will now be able to create almost any
background you need for your web pages using
single or multiple images, scale them to your
exact requirements, and support dynamic
resizing of browsers and elements in which the
backgrounds will always look good.
If you ever produce any animations in your
web pages, you can create a great 3D effect by
moving the background(s) about in relation to
the foreground movement, and even enlarging
and reducing their width and height to simulate
zooming in and out.
In Lesson 13 we’ll take a detailed look into the
world of CSS3 borders.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
1. What does the border-box value refer
to?
THE BORDER-COLOR
PROPERTIES
There are two ways you can apply colors to a
border. First, you can assign a single color to the
property, as follows:
border-color:#888;
THE BORDER-STYLE
PROPERTIES
It is possible to apply a wide variety of border
styles, either to the entire border of an element
or to individual edges. To do this, you can assign
values to the border-style property, like this:
border-style:dotted;
border-top-style:double;
THE BORDER-IMAGE
PROPERTIES
On the latest versions of all major browsers, it is
possible to display images for the four edges and
four corners of an element. Using this feature,
you can achieve similar results to those in the
section “Multiple Backgrounds” (see Lesson 12),
but you use only a single image comprising all
eight elements, such as the one in Figure 13-3.
FIGURE 13-3 A compound image (border.png)
used for creating borders
border-radius:20px;
SUMMARY
In Lesson 12 we examined creating backgrounds,
some of which emulated borders, while in this
one we’ve looked at advanced border techniques.
In Lesson 14 we’ll complete our study of styling
behind and around elements, by learning how to
add shadows to them, handle content that may
overflow from them, and even move our focus
back inside HTML elements, to see how to use
CSS3 for flowing text across multiple columns.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
MANAGING ELEMENT
OVERFLOW
In CSS2 it is possible to allow or prevent the
overflow from an element by setting the
overflow property to hidden, visible, scroll,
or auto. But with CSS3 you can now separately
apply these values in the horizontal or vertical
directions, as shown in Figure 14-4 (created with
the file overflow.htm in the accompanying
archive, and listed next), in which a variety of
combinations are displayed for content that
overflows in both directions.
FIGURE 14-4 Some possible combinations of
the various overflow properties
SUMMARY
In this lesson you have learned how to apply a
wide range of box shadowing effects to your
elements, how to control the way content
overflows, scrolls, or is truncated when it is too
large to fully fit within an element, and how to
flow the content across multiple columns, which
is arguably the most useful of the new features in
the world of digital publishing.
In Lesson 15 we’ll examine how to enhance
your use of color and transparency using the
range of new CSS3 features provided to support
these features.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
HSL COLORS
To define a color with the hsl() function, you
must first choose a value for the hue of between
0 and 359 from a color wheel. Any higher color
numbers simply wrap around to the beginning
again, so the value of 0 is red, and so are the
values 360 and 720.
In a color wheel, the primary colors of red,
green, and blue are separated by 120 degrees, so
that green therefore has the value 120, blue 240,
and the numbers between these values represent
shades comprising different proportions of the
primary colors on either side.
Next you need the saturation level, which is a
value between 0 and 100 percent. This specifies
how washed out or vibrant a color will appear.
Figure 15-1 shows a color wheel displaying both
hue and saturation, forming a sort of Y shape,
which you can even make out when viewed in
monochrome. Red is at the top, green is at the 4
o’clock position, and blue is at 8 o’clock, with the
arms and leg of the Y (at 10 o’clock, 2 o’clock,
and 6 o’clock), being the colors magenta, yellow,
and cyan.
FIGURE 15-1 A color wheel displaying both hue
and saturation
You can also use this (and all other CSS color
functions) with any property that accepts a color,
such as background-color, box-shadow, and so
on.
Figure 15-2 shows a selection of colors
displayed using this function, created with the
accompanying example file hslcolors.htm, listed
next.
FIGURE 15-2 Using the hsl() function to
display different colors
HSLA COLORS
To provide even further control over how colors
will appear, you can use the hsla() function,
supplying it with a fourth (or alpha) level for a
color, which is a floating point value between 0
and 1. A value of 0 specifies that the color is
totally transparent, while 1 means it is fully
opaque.
Here’s how you would choose a fully saturated
yellow color with standard brightness and 30
percent opacity:
RGB COLORS
You will probably be more familiar with the RGB
system of selecting a color as it’s similar to using
the #nnnnnn and #nnn color formats. For
example, to apply a yellow color to a property,
you can use either of the following declarations
(the first supporting about 16 million colors, and
the second about 4,000):
You can also use the CSS rgb() function to
achieve the same result, but you use decimal
numbers instead of hexadecimal (where 255
decimal is ff hexadecimal):
RGBA COLORS
As with the hsla() function, the rgba() function
supports a fourth alpha argument, so you can, for
example, apply the previous cyan-like color with
an opacity of 40 percent, by using declarations
such as this:
opacity:0.25;
When opacity is applied (as with all properties), it flows
through the cascade. So, all descendant elements will also
be given this property value. This, of course, means you
don’t have to style descendants separately when you want
to change the opacity of an element containing sub-
elements.
-webkit-opacity:0.25;
-moz-opacity:0.25;
filter:alpha(opacity=′25′);
Most of these cases can be taken care of, however, if you
instead simply include the prefixfree.js utility in the
accompanying archive at 20lessons.com.
SUMMARY
Using the features detailed in this lesson, you
will now be able to manipulate the colors of
HTML elements in the finest of detail and in the
subtlest ways to provide exactly the results you
need. With the alpha value and opacity property,
you can also blend them in precisely with other
elements on a page. And, as you’ll see in Lesson
20, opacity is a great property to use in
conjunction with transitions to fade elements in
and out.
In Lesson 16, though, we’ll look at how you
can fine-tune the display of written content by
using various text effects.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
THE TEXT-SHADOW
PROPERTY
This property is similar to the box-shadow
property (covered in Lesson 14) and takes the
same set of arguments: a horizontal and vertical
offset, an amount for the blur radius, and the
color to use. For example, the following
declaration offsets the shadow by 3 pixels both
horizontally and vertically, and displays the
shadow in dark gray, with a blur radius of 4
pixels:
THE TEXT-OVERFLOW
PROPERTY
When using any of the CSS overflow properties
with a value of hidden, you can also use the
text-overflow property to place an ellipsis
(three dots) just before the cutoff to indicate that
some text has been truncated, like this:
text-overflow:ellipsis;
word-wrap:break-word;
box-sizing:border-box;
box-sizing:content-box;
box-sizing:border-box;
FIGURE 16-6 Setting box-sizing to border-
box
Browser Compatibility
The standard box-sizing name for this property
has only recently become used by browsers other
than Opera and Android. Therefore, for this
feature to work on older web browsers, you’ll
need to use the browser-specific prefixes for this
property (such as -webkit- and -moz-), or
include the prefixfree.js utility in your web pages
that access this property.
SUMMARY
Now that you have a variety of methods for
styling your text in interesting ways and making
its presentation as eye-catching and easy-to-read
as possible, in Lesson 17 we’ll further extend
your repertoire by looking at how to access the
hundreds of free (and premium) quality web
fonts now available.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
Browser Differences
The various main browsers handle web fonts in
slightly different ways. For example, Google
Chrome, Apple Safari, and Opera first render the
rest of a web page and then only render a web
font once it has loaded. On the other hand,
Firefox initially displays web fonts in a native
font and then re-renders the text once the web
font has loaded. This causes the text to flicker
and has been given the name “flash of unstyled
text.”
Internet Explorer, however, is similar in action
to the other non-Firefox browsers but you must
be careful. For example, if the <link> tag to
select the web font is placed after any <script>
elements, the entire page will not display
anything until the font has loaded.
If you wish for consistency across all
browsers, you may want to look into the Web
Font Loader utility that was jointly developed by
Google and Typekit. With it you can ensure all
browsers will behave in a similar manner, for
example, all just like Firefox. You can get more
information on this free program at the following
URL:
developers.google.com/fonts/docs/webfont_loade
r.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
2D TRANSFORMATIONS
There is a wide range of different types of
transformations you can apply to HTML elements
using CSS3, all of which can be achieved using
the information in this lesson (although simpler
functions to make the process even easier are
explained in Lesson 19).
transform:none;
Argument 2
transform:matrix(1, 0.2, 0, 1, 0, 0);
Argument 3
transform:matrix(1, 0, 0.2, 1, 0, 0);
Argument 4
transform:matrix (1, 0, 0, 1.2, 0, 0);
Argument 5
transform:matrix(1, 0, 0, 1, 20, 0);
Argument 6
transform:(1, 0, 0, 1, 0, 20);
filter:progid:DXImageTransform.Microsoft.
Matrix(SizingMethod=′auto expand′,
M11=1, M12=0.2, M21=0, M22=1);
SUMMARY
Armed with the matrix() function, you now have
an amazing variety of tools you can use to
manipulate elements at your disposal. However,
matrix() is not exactly an easy function to fully
understand, so some simpler functions that stick
to specific tasks (such as rotation or skew) have
also been made available, and are detailed in
Lesson 19.
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
THE TRANSLATE(X,Y)
FUNCTION
The translate() function lets you move an
object to a new position by supplying a horizontal
and vertical offset. This is the same as using
arguments 5 and 6 in the matrix() function, as
described in Lesson 18.
For example, the following two declarations
have the same effect as that shown in Figures 18-
6 and 18-7:
transform:translate(20px, 0px);
transform:translate(0px, 20px);
Older Versions of IE
On Internet Explorer browsers prior to IE9, you
can use the following alternative declarations
with the Matrix() function to achieve the same
effect by modifying the M11 and M22 arguments
(but remember that the scaling is from the top-
left corner, not the element’s center).
Older Versions of IE
You can achieve the same effect in Internet
Explorer browser versions prior to IE9, but it
requires using matrix multiplication of sine and
cosine values. However, to get you started with a
few declarations, here are the values to use for
the four diagonal rotations of 45, 135, 225, and
315 degrees:
And here are IE declarations for 90-, 180-, and
270-degree rotations, which are much easier to
set up, because the BasicImage() function can
handle these rotations, with simple values for the
rotation argument of just 1, 2, and 3:
SUMMARY
At this point you have now learned just about
everything you need to know about CSS and
CSS3, up to and including advanced
transformations. But there’s one goody left to
explore, which is taking transformations one step
further, into the world of 3D, which we’ll do in
Lesson 20 (the final lesson).
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
THE PERSPECTIVE
PROPERTY
The perspective property releases an element
from 2D space and creates a third dimension
within which it can move. The property can be
applied to unique elements, which will then have
their own perspective view, but if you wish to
have several elements on a page sharing the
same perspective, then you must apply the
perspective property to a parent object.
The value assigned to the property is any
accepted CSS measurement, but pixels are good
to use. It represents the distance between the
viewer and the object. High values place the
object farther away (but not reduced in size), and
therefore it will show only a subtle 3D effect. A
lower number places the object closer to the
viewer (although still retaining the same size),
and therefore any 3D effect is much more
intense.
To rotate an object through the three
dimensions, you use the rotate3d() function, or
the rotateX(), rotateY(), and rotateZ()
functions (explained later in this lesson).
Similarly, other functions such as skew() and
translate() have their own 3D versions too.
Figure 20-1 was created using the file
perspective.htm from the accompanying archive,
and it is listed next. In it a perspective value of
400 pixels has been assigned to the image’s
parent <div> (named outer), with this rule:
THE TRANSFORM-ORIGIN
PROPERTY
By default, the point of origin for an element (or
group of elements) is its center point. This is like
the vanishing point you see in a photograph or
painting—the location at which all lines converge
to a single point. You can change the location of
this point to anywhere within the 3D space of an
element that has been given perspective, like
this:
THE TRANSLATE3D
(X,Y,Z) FUNCTION
This function moves an element to another
location in its 3D space, while still retaining its
dimensions and rotation. The file translate3d.htm
in the accompanying archive features three
buttons over which you can hover the mouse
(remember to use Google Chrome or Apple
Safari) to move an element by 30 pixels in any of
the three dimensions, as shown in Figure 20-3, in
which an element has been moved toward the
viewer, using this CSS transform declaration:
THE SCALE3D(X,Y,Z)
FUNCTION
You can scale elements up or down in size, in any
or all three dimensions, like the following, which
enlarges in X by 20 percent, keeps Y the same,
and reduces Z by 20 percent:
transform:scale3d(1.2, 1, .8)
Because elements are two-dimensional to begin with,
when you stretch them in the third dimension, they have no
depth to either shrink or grow, so nothing will seem to
happen. However, once an element has been rotated a little
either horizontally or vertically, it will then extend into the
Z dimension, and so scaling and other transformations in
that dimension will have an observable effect.
transform:rotate3d(1, 1, 1, 45deg)
scale3d(1, 1.2, 1);
THE ROTATE3D(X,Y,Z,R)
FUNCTION
Using the rotate3d() function, you can also
rotate an element around any of the X, Y, and Z
axes. To use it, you must provide a value of either
0 or 1 for each of the axes to use, and then a
fourth argument is required, supplying the
number of degrees to rotate the object.
If only one axis is set to 1, the rotation will be
simply around that line (left to right, up to down,
or in to out). But if two or three axes are set to 1,
then a new axis of rotation is created that
combines them.
The file rotate3d.htm in the accompanying
archive (and listed next) provides three buttons
over which you can pass the mouse to rotate
around the X, Y, or Z axis, as shown in Figure 20-
5, in which the element from the previous section
is being rotated around the X axis, with the top
going away from the viewer and the bottom
coming toward the viewer.
FIGURE 20-5 The element has been rotated
around the X axis by 75 degrees.
transform:rotate3d(0, 0, 1, -75deg)
transform:rotate3d(1, 2, 4, 30deg);
backface-visibility:hidden;
backface-visibility:visible;
THE TRANSFORM-STYLE
PROPERTY
Using this property, you can specify whether a
child or nested element inherits the 3D space of
its parent (if it has one) which is the default, or if
it should restrict itself to 2D, for example, to act
as a texture mapped onto the parent. The two
values you can assign to this property are as
follows (where flat is the default uninherited
value):
TRANSITIONS
Also appearing on all the latest versions of the
major browsers is a fantastic new feature called
transitions. Using it, you can specify a type of
animation effect you want to occur when an
element is transformed, and the browser will
automatically take care of all the in-between
frames for you.
There are up to four properties you can use to
set up a transition, as follows:
The transition-property
Property
This is the property to which you assign the
property or properties that are to be animated,
like this:
transition-property:width;
transition-property:width, height,
opacity;
transition-property:all;
The transition-duration
Property
The transition-duration property sets the
length of time during which the animation should
occur, in other words the length of the transition.
The property requires a value of 0 seconds or
greater, like the following, which specifies that
the transition should take 1.25 seconds to
complete:
transition-duration:1.25s;
transition-delay:0.1s;
The transition-timing-
function Property
With the transition-timing-function property,
you can specify the animation timing that should
be used by choosing a function representing the
way the animation speeds up and slows down
during its presentation.
This property requires one of the following
values:
• ease Start slowly, get faster, then end
slowly
• linear Transition at constant speed (the
default)
• ease-in Start slowly, then go quickly until
finished
• ease-out Start quickly, stay fast, then end
slowly
• ease-in-out Start slowly, go fast, then
end slowly
SELF-TEST QUESTIONS
Using these questions, test how much you have
learned in this lesson. If you don’t know an
answer, go back and reread the relevant section
until your knowledge is complete. You can find
the answers in the appendix.
LESSON 1 ANSWERS
1. HTML is based on the Document Object
Model (DOM).
2. The main purpose of style sheets is to
enable the separation of content from
styling in web documents.
LESSON 2 ANSWERS
1. A CSS selector is the element or group
of elements being selected by a rule for
styling. Selectors may include element
types, IDs, or classes.
LESSON 3 ANSWERS
1. In CSS and HTML, IDs are given to
elements so that they can be uniquely
identified by either CSS rules or JavaScript
commands.
LESSON 4 ANSWERS
1. The type selector is where a type of
element such as <div>, <table>, or <img> is
selected—for example: img { border:1px
solid black; }.
LESSON 5 ANSWERS
1. The three types of style sheet are: those
created by a document’s author, those
created by the user, and those created by
the browser.
LESSON 6 ANSWERS
1. To change font family, use the font-
family property—for example: font-
family:Arial;.
LESSON 9 ANSWERS
1. The term CSS Box Model refers to a
method of representing the attributes of
HTML elements affecting their size, spacing,
and borders, in the form of a box model
from the outside to the inside (or vice
versa).
LESSON 10 ANSWERS
1. Features were still being proposed for
CSS3 as recently as 2012.
LESSON 11 ANSWERS
1. The universal selector lets you style any
elements. While this makes it very powerful,
the downside is that it breaks the cascade of
CSS, and therefore, it should be used with
caution.
LESSON 12 ANSWERS
1. The border-box value refers to the
outer edge of an element’s border.
2. The padding-box value refers to the
outer edge of an element’s padding area.
LESSON 13 ANSWERS
1. You can change all the borders of an
element to the same color using the border-
color property—for example: border-
color:red;, as part of a shorthand border
rule—for example: border:1px solid red;,
or uniquely addressing each corner using
the properties border-top-color, border-
left-color, border-right-color, and
border-bottom-color.
LESSON 14 ANSWERS
1. You can add a shadow to an element
using the box-shadow property.
LESSON 15 ANSWERS
1. The difference between the rgb() and
hsl() functions is that the rgb() function
requires values of red, blue, and green to
describe a color, while hsl() needs values
for hue, saturation, and luminance.
LESSON 17 ANSWERS
1. A web font is one that is not stored
locally on the user’s computer, but is
downloaded on demand over the Internet
when a browser requires it.
LESSON 18 ANSWERS
1. CSS can perform transformations such
as skewing, scaling, and rotation on
elements using the matrix() function—for
example: transform:matrix(1.2, 0, 0,
1.2, 0, 0);.
2. The fifth argument to matrix() moves
an element horizontally—for example:
transform:matrix(1, 0, 0, 1, 20, 0);.
LESSON 19 ANSWERS
1. You can move an element to a different
location using the translate() function—
for example: transform:translate(10px,
20px);.
LESSON 20 ANSWERS
1. To release an element from 2D space
and make it available for 3D manipulation,
you can use the perspective property on
the element or its parent—for example:
perspective:2000px;.
, (comma), 35
; (semicolon), 9
: (colon), 87, 88
:: (double colon), 87–88, 99
* (asterisk), 34–35
* (universal selector), 34–35, 133–134
# (number sign), 8
~ (sibling selector symbol), 213
~ (tilde), 135–136
{ } curly braces, 32
*= operator, 138
^= operator, 138
$= operator, 138
* selector, 34–35
- symbol, 212–213
. (period) symbol, 9
# symbol, 9, 99
/p tags, 31
* universal selector, 99
″ (quotation marks), 55–56, 100
2D transformations, 211–223
matrix() function, 213–223
none transformation type, 211–213
overview, 211
3D transformations, 237–256
backface-visibility property, 249–251
dimensions, 241–242
moving elements, 42–43
perspective property, 237–239
point of origin, 239–241
rotate3d() function, 246–249
scale3d( ) function, 244–246
transform-origin property, 239–241
transform-style property, 251
transitions, 251–255
translate3d( ) function, 242–243
x axis, 241, 242
y axis, 241, 242
z axis, 241, 242
A
absolute positioning, 80–81
active class, 90
active pseudo-class, 91
after pseudo-element, 100, 101
alignment
text, 64
vertical, 116
alpha transparency, 75, 168, 191
Android browser, 78
animation, 255
answers, self-test, 257–272
asterisk (*), 34–35
attribute selector, 34
attribute selectors, 122–123, 136, 137–139
attributes, 34
Auto Prefixer, 79
auto value, 147
B
backface-visibility property, 249–251
background color, 67–68, 91, 100
background images, 146
background property, 91
background shorthand, 103–104
background-clip property, 141–146
background-color property, 67, 68, 91
background-origin property, 146
backgrounds, 141–151
browser issues, 146–147
display options, 141–146
hiding, 141–146
multiple, 147–150
position, 146
scaling, 147
size, 147
background-size property, 147
before pseudo-element, 100, 101
blink effects, 61–62
block elements, 115
blockquote elements, 100–101
bold text, 60–61
border property, 111
border-box attribute, 141, 146
border-color properties, 153–154
border-image properties, 156–160
border-radius properties, 160–163
borders, 153–163
box model, 111
boxes, 111
color, 153–154
in CSS3, 123–124
images, 156–160
overview, 153
radius, 160–163
shorthand properties, 102–103
styles, 154–156
border-style properties, 154–156
box model, 107–118
block elements, 115
borders, 111
inline elements, 114, 115
modifying, 117–118
nested levels, 107–108
non-image elements, 112–116
overview, 107–108
padding, 112, 113
setting margins, 107–110
W3C, 194
box shadows, 24–25, 125, 165–169
box sizing, 129
boxes
block elements, 115
borders, 111
inline elements, 114, 115
margins, 107–110
non-image elements, 112–116
padding, 112, 113, 194, 196, 197
resizing, 198–200
size, 118, 128, 194–198
box-shadow property, 165–174
box-sizing property, 194–197
browsers. See web browsers
C
c( ) function, 27
cap class, 88–90
capitalize value, 65
captions, 20–21
cascade, 43–53
cascading, 43
Cascading Style Sheets. See CSS
centimeters, 51
Central Processing Unit (CPU), 227
checkboxes, 99
child elements, 135
child selector, 32–33, 135
Chrome browser, 77, 78, 208, 212
class attribute, 19, 134
class names, 20
class selector, 33–34, 134
classes
accessing with JavaScript, 27
advantages of, 34
applying styles with, 8–9
assigning multiple, 25–26
assigning with JavaScript, 26–29
considerations, 23
names, 12, 25, 134
narrowing scope, 34
rules, 134
using, 23–26
colon (:), 87, 88
color, 67–76
background, 67–68, 91, 100
borders, 153–154
in CSS3, 128
example document, 73–74
fades, 75
gradients, 69–71
HSL, 177–181
HSLA, 182
links, 90, 91
names, 67
opacity, 186–188
RGB, 182–185
RGBA, 186
saturation, 177–178
text shadows, 189–191
color function, 200
color property, 67
color stops, 70, 71–72
color strings, 69
color wheel, 177–178
columns
in CSS3, 127–128
flowing text over, 172–174
multiple, 127–128, 172–174
comma (,), 35
comments, 13–14
compound numbers, 45–46
content
adding with pseudo-elements, 100–101
repurposing, 20
styles and, 16–17
content property, 101
content-box attribute, 141, 146
content-box value, 196
CPU (Central Processing Unit), 227
CSS (Cascading Style Sheets). See also style
sheets; styles
accessing with JavaScript, 27
adding to pages, 6
comments in, 13–14
considerations, 6–7, 19–20
described, 19
DOM and, 6
example, 6–7
importing from within HTML, 7–8
introduction to, 3–10
overview, 3
whitespace, 39
CSS errors, 9
CSS rules, 11–17
assigning, 133–134
classes, 134
importance of, 46–48
multiple assignments, 12–13
selectors, 11–12
CSS2 selectors, 136–137
CSS3
attribute selectors, 122–123
borders in, 123–124
box shadows, 125
box sizing, 129
browser incompatibilities, 76–80
color in, 128
element overflow, 125–126
introduction to, 121–132
multicolumn layout, 127–128
opacity, 128
pseudo-elements, 87–88
resizing/outlines, 129
text effects, 129
transformations, 131–132
transitions, 131–132
CSS3 selectors, 137–139
curly braces { }, 32
D
declarations, 19–29
descendant selectors, 32, 135
div elements, 196
<div> element, 12, 48–50, 115, 116
Document Object Model. See DOM
DOM (Document Object Model)
considerations, 3, 19
CSS and, 6
example, 4
how it works, 3–5
JavaScript and, 26
double colon (::), 87–88, 99
dubic-bezier( ) function, 253–254
E
effects, 61–62, 129
elements. See also objects
accessing numerically, 92–95
block, 115
child, 135
compound, 45–46
inline, 114, 115
inline-block, 115
moving, 42–43, 219–220
overflow, 125–126, 170–172
parent, 135
point of origin, 239–241
positioning, 80–84
rotating, 239–251
scaling, 218, 221, 227–229, 244–246
selecting. See selectors
selecting by numeric location, 95–98
shrinking, 215, 218–219
skewing, 216, 232–234
spacing, 44
stretching, 215, 216, 218–219
transforming. See transformations
ellipses, 71, 191–193
em value, 52
embedded styles, 8, 44
equations, 97
errors, CSS, 9
even/odd elements, 96–97
ex value, 52
example files, 3
Express Prefixr, 79, 80
expressions, 96–97
F
filter property, 222–223
Firefox browser, 77, 78, 208, 212
first-child class, 92–93, 94
first-line pseudo-element, 89–90
first-of-type pseudo-class, 93–95
fixed positioning, 81–83
flipping objects, 219
float property, 24–25
focus pseudo-element, 91
font families, 55–57
font properties, 102
@font-face at-rule, 203, 205
font-family property, 55–57
fonts. See also text
bold, 60–61
browser issues, 56
CSS3, 203–204
generic, 56–57
italics, 58
names, 56–57
OpenType, 204
resources, 209
shorthand values, 104
size, 58–60, 89, 104
styles, 57–58
TrueType, 204
web, 130, 203–210
weight, 60–61
WOFF, 204
fonts.com, 209
font-size property, 55, 58–60
font-style property, 55, 57–58
font-weight property, 55, 60–61
G
getElementById( ) function, 26
Google web fonts, 205–208
GPU (graphical processing unit), 227
gradients, 69–71
alpha transparency, 75
overview, 69–70
repeating, 72–73
web browsers and, 70, 76
graphical processing unit (GPU), 227
groups, selecting by, 35–36
H
hidden attribute, 191, 192
hover class, 90, 91
href property, 4
HSL (hue, saturation, and luminance), 177
HSL color, 177–181
hsl( ) function, 177–181
HSLA (hue, saturation, luminance, and alpha),
177
HSLA color, 182
hsla( ) function, 182, 191
HTML (HyperText Markup Language)
assigning IDs to, 8
importing CSS from, 7–8
nesting, 6
structure, 6
hue, saturation, and luminance. See HSL
hue, saturation, luminance, and alpha (HSLA),
177
hyphenated property names, 27–28
I
<i> tag, 31
ID names, 21, 26, 33
ID rules, 33–34
ID selector, 33, 134
IDs
assigning, 8, 26–29
JavaScript and, 26–29
names, 12, 134
narrowing scope, 33
purpose of, 33
scenario, 20–21
specificity and, 45, 46
using/reusing, 19–23
images
adding box shadows to, 24–25
background, 146
borders, 156–160
<img> element, 32
@import directive (or command), 7
!important keyword, 46–48
importing
CSS from within HTML, 7–8
style sheets, 7
inches, 51
indenting text, 65
inline elements, 114, 115
inline styles, 16
inline-block elements, 115
insertRule( ) function, 41
internal style sheets, 15
Internet Explorer
borders and, 160
CSS3 issues, 76–80, 222–223
CSS3 matrix( ) function and, 222–223
JavaScript and, 212–213
outline issues, 200
rotating objects and, 231
skewing objects and, 234
text shadows and, 191
transformations and, 227
web fonts and, 205, 208
italic text, 58
italicized text, 58
J
JavaScript
accessing classes with, 27
accessing CSS styles, 27
accessing elements, 26
accessing objects, 26–27
applying properties with, 212–213
assigning classes with, 26–29
assigning IDs with, 26–29
considerations, 28–29
DOM and, 26
element resizing and, 198
ID names and, 21, 22, 26
selectors.htm example, 36–41
K
Konqueror web browser, 77
L
last-child pseudo-class, 95
last-of-type pseudo-class, 95
left property, 227
letter spacing, 63–64
line spacing, 62–63
linear gradients, 70–71, 73
line-height rule, 89
link class, 90
<link> tag, 7–8
link-related pseudo-classes, 90–92
links
color, 90, 91
inline styles and, 16
state of, 136
lowercase value, 65
M
margin property, 108–110
margins
boxes, 107–110
padding, 104, 105
matrices, 248
matrix( ) function, 213–223
max-height property, 198
max-width property, 198
measurement, units of, 50–53
millimeters, 51
min-height property, 198
min-width property, 198
multicolumn layout, 127–128
myfonts.com, 209
N
nesting HTML, 6
none transformation type, 211–213
nth rules, 96–97
nth-child( ) pseudo-class, 95
nth-last-childl( ) pseudo-class, 96
nth-last-of type( ) pseudo-class, 96
nth-of-type( ) pseudo-class, 96
O
o( ) function, 26–27
objects. See also elements
animating, 255
changing opacity of, 177, 186–188
considerations, 4
flipping, 219
moving, 219–220, 225–227
positioning, 225–227
resizing, 198–200
rotating, 218, 229–231, 237–251
scaling, 218, 221, 227–229, 244–246
skewing, 216, 232–234
transforming. See transformations
odd/even elements, 96–97
opacity
changing for objects, 177, 186–188
considerations, 186
CSS3, 128
opacity property, 177, 186–188
OpenType fonts, 204
Opera browser, 77, 78, 208, 212
outline property, 200–201
outline-offset property, 200–201
outlines
CSS3, 129
shorthand properties, 102–103
overflow
elements, 125–126, 170–172
text, 191–193
words, 193–194
overflow properties, 170–172
overline text, 61–62
P
<p> tag, 31, 64
padding
boxes, 112, 113, 194, 196, 197
margins, 104
margins and, 105
shorthand values, 104
padding property, 83, 112, 113
padding values, 105
padding-box attribute, 141, 146
parent elements, 135
percentage widths, 52–53
period (.) symbol, 9
perspective property, 237–239
photographs
classes and, 24
styling, 20–21
picas, 51
pixels, 50–51
point of origin, 239–241
points, 51
position property, 80–84
positioning elements, 80–84
precedence calculation, 45–48
prefixfree.js file, 74
prefixfree.js utility, 78–79, 168, 211, 239
print style sheets, 20
programming, 77–78
properties
applying with JavaScript, 212–213
described, 3
font, 102
hyphenated names, 27–28
shorthand, 101–105
text-decoration, 61–62
property names, 78–79
pseudo-class selectors, 136
pseudo-classes
accessing elements numerically, 92–95
in CSS3, 88
enabled/disabled, 99
expressions, 96–97
link-related, 90–92
pseudo-elements
adding content with, 100–101
CSS3, 87–88
overview, 136
pseudo-selectors, 87–101
accesing elements numerically, 92–95
overview, 87–90
relational selectors, 97–98
selecting elements by numeric location,
95–98
Q
quotation marks (″), 55–56, 100
quotations, 100–101
R
radial gradients, 71, 73
radio buttons, 99
radiused corners, 160–163
red, green, and blue (RGB), 177
red, green, blue, and alpha (RGBA), 177
relational selectors, 97–98
relative positioning, 81
resize property, 198–200
RGB (red, green, and blue), 177
RGB color, 182–185
rgb( ) function, 69, 182–185
RGBA (red, green, blue, and alpha), 177
RGBA color, 186
rgba( ) function, 186, 191
rotate( ) function, 229–231
rotate3d( ) function, 237–239, 246–249
rotating items, 218, 229–231, 237–251
rules. See CSS rules
S
s( ) function, 27
Safari browser, 77, 78, 208, 212
saturation, 177–178
scale( ) function, 227–229
scale3d( ) function, 244–246
scaleX( ) function, 229
scaleY( ) function, 229
scaling items, 218, 221, 227–229, 244–246
<script> element, 38
selection pseudo-element, 88
selection text, 100
selections, 31
selectors, 31–42
assigning, 44–45
attribute, 34, 133–137, 136
child, 135
class, 33–34, 134
CSS2, 136–137
CSS3, 137–139
descendent, 135
described, 31
ID, 33, 134
overview, 11–12
pseudo-class, 136
pseudo-elements, 136
relational, 97–98
sibling, 135–136
specificity, 43, 45–48
style sheets, 44–45
type, 134
types of, 133–137
universal, 34–35, 133–134
wildcard, 34–35
selectors.htm example, 36–41
self-test answers, 257–272
semicolon (;), 9
setAttribute( ) function, 28, 213
shadows
alpha transparency, 191
box, 24–25, 125, 165–169
in CSS, 128
text, 189–191
shorthand properties, 101–105
sibling selector symbol (~), 213
sibling selectors, 135–136
skew( ) function, 232–234
skewing elements/objects, 216, 232–234
spacing
box model, 107
columns, 172
elements, 44
letter, 63–64
line, 62–63
word, 63–64
<span> element, 23, 48–50
<span> tag, 40
spans, 48–50
specificity, 43, 45–48
style attribute, 19
style sheets. See also CSS
creation of, 43–44
importing, 7
including with HTML tag, 7–8
methods, 44
order processed, 44
print, 20
selectors, 44–45
types of, 43–44
user, 14–15
style types, 14–16
styles. See also CSS
applying with classes, 8–9
borders, 154–156
captions, 20–21
content and, 16–17
default, 14
embedded, 8, 44
fonts, 57–58
inline, 16
photographs, 20–21
precedence, 43, 45–48
text, 61–65
user, 14–15
T
tags
closing, 6
obsolete, 16
order, 6
reference elements, 45
text. See also fonts
decoration values, 61–62
ellipses, 191–193
flowing over multiple columns, 172–174
indented, 65
italicized, 58
letter spacing, 63–64
line-through, 61–62
lowercase, 65
overflow, 125–126, 191–193
overline, 61–62
selection, 100
shadows, 189–191
size, 58–60
styles, 61–65
truncated, 191–193
underlined, 61–62
uppercase, 65
word spacing, 63–64
text aligning, 64
text decoration, 61–62
text effects, 129
text-decoration property, 61–62
text-indent property, 65
text-overflow property, 191–193
text-shadow property, 189–191
tilde (~), 135–136
<title> element, 4
top property, 227
transformations
2D. See 2D transformations
3D. See 3D transformations
applying, 225–235
browser issues, 212–213
CSS3, 131–132
removing, 211–213
transform-origin property, 239–241
transform-style property, 251
transition property, 214, 251–255
transition-delay property, 252–253
transition-duration property, 252
transition-property property, 252
transitions, 131–132, 251–255
transition-timing-function property, 253
translate( ) function, 225–227
translate property, 227
translate3d( ) function, 242–243
translateX( ) function, 227
translateY( ) function, 227
transparency, 75, 168
TrueType font, 204
type selector, 31–32, 134
typelit.com, 209
U
underlined text, 61–62
units of measurement, 50–53
universal selector (*), 34–35, 133–134
uppercase value, 65
url ( ) function, 204
URLs, pointing to, 99
user style sheets, 14–15
V
Verou, Lea, 79
vertical alignment, 116
vertical-align property, 116
videos, lessons, 3
visited class, 90
W
W3C (World Wide Web Consortium), 204
W3C box model, 194
w3.org website, 88
<wbr> tag, 40
web browsers
Android, 78
background issues, 146–147
box-sizing issues, 197
Chrome, 77, 78, 208, 212
CSS3 and, 76–80
differences between, 208
Firefox, 77, 78, 208, 212
fonts and, 56
gradients and, 70, 76
Internet Explorer. See Internet Explorer
Konqueror, 77
older versions, 77
opacity and, 186
Opera, 77, 78, 208, 212
prefixes and, 239
Safari, 77, 78, 208, 212
style precedence and, 43
text overflow and, 174
transformations and, 212–213
Webkit-based, 212, 239
The Web Developer’s Cookbook, 83
Web Font Loader utility, 208
web fonts, 130, 203–210
Web Open Font Format (WOFF), 204, 205
web pages
considerations, 20
dynamic functionality, 33
web typography, 203
Webkit engine, 77
Webkit-based browsers, 212, 239
websites, pointing to, 99
whitespace, 39
wildcard selector, 34–35
WOFF (Web Open Font Format), 204, 205
word spacing, 63–64
word wrapping, 193–194
word-wrap property, 193–194
World Wide Web Consortium (W3C), 204
X
x axis, 241, 242, 247, 248
X dimension, 239–241
Y
y axis, 241, 242
Y dimension, 239–241, 244
Z
z axis, 241, 242
Z dimension, 241, 244