Styling Web Pages With CSS-2: John W. Shipman
Styling Web Pages With CSS-2: John W. Shipman
John W. Shipman
2013-07-07 12:45
Abstract
Reference guide for Cascading Style Sheets 2.0, a language for describing the style of Web pages.
1 2
This publication is available in Web form and also as a PDF document . Please forward any
comments to tcc-doc@nmt.edu.
Table of Contents
1. What are Cascading Style Sheets and why should you use them? ................................................ 3
2. Connecting your page to a style sheet ........................................................................................ 3
3. Overall structure of a style sheet ............................................................................................... 4
4. At-rules ................................................................................................................................... 4
5. How to write CSS rules ............................................................................................................ 5
6. Common value types in CSS ..................................................................................................... 5
6.1. Dimensions ................................................................................................................... 5
6.2. Specifying colors ........................................................................................................... 6
6.3. String constants ............................................................................................................. 6
6.4. Universal resource identifiers (URIs) .............................................................................. 7
6.5. Counters ....................................................................................................................... 7
6.6. Specifying angles ........................................................................................................... 7
6.7. Times ........................................................................................................................... 7
6.8. Frequencies ................................................................................................................... 8
7. Selectors .................................................................................................................................. 8
7.1. Element type selectors ................................................................................................... 8
7.2. Selecting elements by class ............................................................................................. 8
7.3. Selecting elements by their context ................................................................................. 9
7.4. Child selection .............................................................................................................. 9
7.5. Adjacent element selection ............................................................................................. 9
7.6. Selecting by attribute values ......................................................................................... 10
7.7. Selecting specific single elements by ID ......................................................................... 10
7.8. Pseudo-classes ............................................................................................................. 10
7.9. Pseudo-elements .......................................................................................................... 11
7.10. The universal selector ................................................................................................. 12
8. When rules collide .................................................................................................................. 12
8.1. Cascading ................................................................................................................... 12
8.2. Specificity: Which selector applies? ............................................................................... 13
8.3. Inheritance .................................................................................................................. 13
9. Declarations ........................................................................................................................... 13
1
2
http://www.nmt.edu/tcc/help/pubs/css/
http://www.nmt.edu/tcc/help/pubs/css/css.pdf
New Mexico Tech Computer Center Styling Web pages with CSS-2 1
10. Font properties ..................................................................................................................... 14
10.1. The font-family property ....................................................................................... 14
10.2. The font-style property ......................................................................................... 15
10.3. The font-variant property ..................................................................................... 15
10.4. The font-weight property ....................................................................................... 15
10.5. The font-size property ........................................................................................... 15
10.6. The font property ..................................................................................................... 15
11. The display property: What kind of box is this? ................................................................... 16
12. Other text properties ............................................................................................................. 17
12.1. The line-height property ....................................................................................... 17
12.2. The text-indent property ....................................................................................... 17
12.3. The text-align property ......................................................................................... 17
12.4. The text-decoration property ............................................................................... 17
12.5. The text-transform property ................................................................................. 18
12.6. The white-space property ....................................................................................... 18
12.7. The letter-spacing property ................................................................................. 18
12.8. The word-spacing property ..................................................................................... 18
12.9. The vertical-align property: Shifting the baseline ................................................. 18
12.10. The quotes property: Specifying quote characters ..................................................... 19
13. The color property ............................................................................................................. 19
14. The background properties ................................................................................................... 20
15. Designing with box elements ................................................................................................. 21
15.1. Side lists .................................................................................................................... 22
15.2. The height and width properties ............................................................................. 23
15.3. The clear property ................................................................................................... 23
15.4. The float property ................................................................................................... 23
15.5. The padding properties ............................................................................................. 23
15.6. The border properties ............................................................................................... 24
15.7. The margin properties ............................................................................................... 25
15.8. The overflow property: What if it doesn't fit? ............................................................ 26
15.9. The clip property: Specify a clipping rectangle .......................................................... 26
15.10. The visibility property: Can we see the content? .................................................. 27
15.11. The position property: Positioning boxes ............................................................... 27
15.12. The box offset properties: top, bottom, left, and right ......................................... 27
15.13. The z-index property: Stacking order ...................................................................... 28
16. The content property: Specifying content in pseudo-elements ............................................... 29
16.1. The counter-reset property ................................................................................... 30
16.2. The counter-increment property ........................................................................... 30
17. The list properties ................................................................................................................. 31
17.1. The list-style-type property ............................................................................... 31
17.2. The list-style-image property ............................................................................. 31
17.3. The list-style-position property ....................................................................... 31
17.4. The list-style property ......................................................................................... 32
17.5. The marker-offset property ................................................................................... 32
18. Tables .................................................................................................................................. 32
18.1. Table column properties ............................................................................................. 33
18.2. How table size is computed ........................................................................................ 34
18.3. Table border properties .............................................................................................. 34
18.4. The speak-header property: Aural rendering of tables .............................................. 35
19. User interface options ........................................................................................................... 35
19.1. The cursor property ................................................................................................. 36
19.2. Selecting colors to match UI components ..................................................................... 36
2 Styling Web pages with CSS-2 New Mexico Tech Computer Center
19.3. Dynamic outlines ....................................................................................................... 37
20. Aural stylesheets .................................................................................................................. 38
20.1. Spatial presentation: the azimuth property ................................................................. 38
20.2. Voice properties ......................................................................................................... 39
20.3. The volume property ................................................................................................. 39
20.4. The speak, speak-punctuation , and speak-numeral properties: spelling it
out .................................................................................................................................... 40
20.5. General voice qualities: voice-family, pitch, pitch-range, stress, and rich-
ness ................................................................................................................................ 41
20.6. Timing properties: speech-rate, pause-before, pause-after, and pause ........... 41
20.7. Element cues: cue-before, cue-after, and cue ...................................................... 42
20.8. Audio mixing: play-during ..................................................................................... 42
21. The @import rule: Importing another stylesheet .................................................................... 43
22. The @media rule: Tuning for different rendering platforms ..................................................... 44
22.1. Media types ............................................................................................................... 44
23. The @page rule: Paged media ............................................................................................... 45
23.1. The size property for paged media ............................................................................ 46
23.2. Controlling page breaks ............................................................................................. 46
23.3. Orphan control .......................................................................................................... 47
23.4. Widow control ........................................................................................................... 47
23.5. Crop marks and alignment targets: the marks property ............................................... 47
23.6. The page attribute: Selecting a page type .................................................................... 48
1. What are Cascading Style Sheets and why should you use them?
HTML (HyperText Markup Language), the language of Web pages, describes the function of each element
of your page, but the browser determines how each element will actually look. This has frustrated page
designers who want more creative control of appearance.
The Cascading Style Sheet (CSS) standard gives you this creative control, assuming that the reader has
a relatively recent Web browser. Most modern browsers support most or all of CSS level 2.1.
CSS is most commonly used to mark up HTML web pages. However, it can also be used to display
XML documents.
Useful online resources:
The CSS standard is defined by the 3W3 Consortium, the umbrella organization for Web-related
standards. See the W3 CSS homepage for a variety of resources: tutorials, standards, and books.
4
This document is based on Cascading Style Sheets, level 2 revision 1: CSS 2.1 Specification . Some of the
rules governing page makeup are extremely tricky; refer to this document for all the fine points.
3
4
http://www.w3.org/Style/CSS/
http://www.w3.org/TR/CSS21/
New Mexico Tech Computer Center Styling Web pages with CSS-2 3
For example, you could write your CSS rules into a file called mystyle.css and then place this tag
somewhere inside the head element of each of your HTML pages:
The href='' attribute will accept any URI, just like the href attribute of the HTML <a> tag.
Here's a small, complete Web page with a stylesheet link:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Small example page</title>
<link rel="stylesheet" href="mystyle.css" type="text/css">
</head>
<body>
<h1>Title</h1>
<p>
Sample paragraph.
</p>
</body>
</html>
Note
Because HTML element names are case-insensitive, CSS stylesheets used to mark up HTML pages are
also case-insensitive, so that for example element names "DIV" and "div" are considered equivalent.
However, because XML element names are case-sensitive, names in a CSS stylesheet associated with
an XML file are also case-sensitive. So, for example, XML element names "castle" and "CASTLE"
are not the same.
4. At-rules
Besides regular style rules, your CSS can include at-rules. These directives all start with an at-sign
(@).
Section 21, The @import rule: Importing another stylesheet (p. 43).
Section 22, The @media rule: Tuning for different rendering platforms (p. 44).
Section 23, The @page rule: Paged media (p. 45).
4 Styling Web pages with CSS-2 New Mexico Tech Computer Center
5. How to write CSS rules
Most of the statements in a CSS stylesheet are rules that specify how certain elements of your page
should appear.
Here is an example of a typical rule:
This rule says that all h1 headings should be rendered using blue letters and centered on the page.
In general, a CSS rule has this structure:
where the selector describes which kinds of elements of your Web page are affected, and each de-
claration describes how those elements should appear. If there are multiple declarations, separate
them with semicolons (;).
The declaration part has this format:
property: value
where the property is a keyword that specifies what aspect of the element you are changing, and the
value says what you are changing it to. In the example rule above, the selector is h1; color and text-
align are properties; and blue and center are values.
You can add comments by enclosing them between /* and */ characters. For example:
6.1. Dimensions
Wherever a property can be set to a value representing a physical dimension, such as a height or a
width, there are several ways to specify them.
These units are used in property definitions:
Unit Description
mm Millimeters
New Mexico Tech Computer Center Styling Web pages with CSS-2 5
Unit Description
cm Centimeters
in Inches
pt Printer's points, about 1/72"
pc Printer's pica, about 1/6"
em An em is the current font size. For example, if the font size is 14pt, two ems is 28pt.
px One pixel on the display.
You can also specify dimensions as a percentage. For example, specifying the left margin as 10% would
mean the left margin would be a tenth of the total screen width.
Note
Whenever possible, prefer relative units (ems and percentages) over fixed units. Consider the plight of
readers whose eyes are aging and not as sharp as they were as a youngster. They can read your page
if they increase the font size. Percentage units can help your layout adjust to different screen sizes.
where the first number is red, the second green, and the third blue.
The rgb() notation also allows numbers in the range 0255. For example, rgb(255, 255, 0) is
pure yellow (pure red plus pure green).
You can also define the colors using a string of the form #rgb or #rrggbb, using hexadecimal digits
to specify the relative amount of red, green and blue. For example, color #ff5500 has full red, a little
green, and no blue.
Any color in the #rgb form is the same as if each of the three digits were doubled. For example, #f50
is the same as #ff5500.
@import "basic.css";
6 Styling Web pages with CSS-2 New Mexico Tech Computer Center
To include a quote symbol inside a string, precede it with a backslash, e.g., 'don\'t' is the same string
as "don't".
You can include any character value inside a string by encoding it as \hh, where hh is the character's
code value in hexadecimal. For example, since the single-quote character has code hex 27, string "don't"
could also be represented as 'don\27t'.
url("URI")
You can, of course, use a full URI to refer to any image on the Web.
6.5. Counters
If you need to assign serial numbers to some sequence of elements, such as bullets in a bullet list, sections
in a chapter, or similar applications, CSS allows you to declare counters that hold such numbers.
Counters are created by using the counter-reset property. For details on the creation and application
of counters, see Section 16, The content property: Specifying content in pseudo-elements (p. 29).
For example, audio playback defines an azimuth property that specifies the apparent source of a sound
in the stereo image. Azimuth zero is straight ahead; 90 is purely in the right ear; -90 is in the left ear;
and so on. So, to specify that the azimuth of a spoken h1 heading is halfway between straight ahead
and the left ear, you would use this rule:
h1 { azimuth: -45deg; }
6.7. Times
CSS time intervals are specified as a number followed immediately by either ms for milliseconds or s
for seconds.
For example, if you want the speech synthesizer to insert a fifty-millisecond delay before speaking an
h3 heading, use this rule:
New Mexico Tech Computer Center Styling Web pages with CSS-2 7
h3 { pause-before: 50ms; }
6.8. Frequencies
Frequencies in CSS are specified as a number followed immediately by suffix Hz for Hertz, or kHz for
kiloHertz. (Note that CSS is case-insensitive, so you don't have to capitalize them as carefully as they
are shown here.)
For example, to specify that a speech synthesizer should produce a voice with a frequency around 180
Hertz for title elements, you would use this rule:
7. Selectors
The selector part of a CSS rule can take several forms.
p { text-indent: 0.5in; }
You can also make the rule apply to multiple element types by giving a list of tag names separated by
commas, for example:
This rule would render all three heading levels as red: h1, h2, and h3.
element.C
For example, suppose you want to designate certain paragraphs as key paragraphs. You might tag
all those paragraphs using:
If you want key paragraphs set in purple type, add this rule:
8 Styling Web pages with CSS-2 New Mexico Tech Computer Center
You can use the HTML div and span elements to attach class names to sections of your documents.
These tags don't affect the HTML; they exist only as places to attach class names. Use div for block-
level elements, meaning that this element always starts a new line on the page and ends with a new
line. Use the span element to mark part of a line or paragraph.
e1 e2
where e1 is the containing element and e2 is the contained element. The associated rule then applies
only to cases where element e1 is an ancestor of e2that is, either e2 is a child of e1, or a child of a
child, et cetera.
For example, to use maroon letters for emphasized text (the em tag) anywhere within a level 1 heading
(the h1 tag), you would use a CSS rule like this:
h1 em { color: maroon; }
You can stack any number of element names in a rule like this. For example, the selector ul ul ul
would apply to third-level bullet lists (that is, bullet lists within bullet lists within bullet lists).
e1 > e2
where e1 and e2 are element names. In this form, the rule applies only to e2 elements that are the direct
children of e1 elements.
For example, the selector h1 > em would apply to an em element whose parent element is h1, but
it would not apply to an em element inside a citetitle element inside an h1 element.
e1 + e2
where e1 and e2 are element names. In this form, the rule applies only to any e2 elements that imme-
diately follows an e1 element.
For example, the selector div.q+div.a would apply to any <div class="a"></div> element
that immediately follows a <div class="q"></div> element.
New Mexico Tech Computer Center Styling Web pages with CSS-2 9
7.6. Selecting by attribute values
You can write rules that apply only to elements with certain attributes. There are several forms of this
rule.
[att]
Applies only to elements that have an attribute named att.
For example, the selector
h2[rating]
p[role="panic"]
would apply only to paragraphs starting with a tag like <p role="panic">.
[att~=value]
Applies to elements that have an attribute named att whose value is a space-separated list of values
including the given value.
For example, a paragraph that started with tag <p phobias='acro claustro arachno
agora'> would be affected by a selector p[phobias~="arachno"].
[att|=value]
Applies to elements that have an attribute att whose value starts with value, optionally followed
by a hyphen and other characters.
This particular selector was intended for style markup that applies only to specific languages. For
example, this rule
*[lang|="en"]
would apply to any element that has a lang attribute that starts with "en". Thus it would apply
to the various English variants such as lang="en-us" (U. S. English), lang="en-uk" (British
English), and so forth.
element#I
7.8. Pseudo-classes
You can use a pseudo-class in a selector. Unlike the selectors we've already discussed, pseudo-classes
are used to select parts of the content by their state or position or other qualities that are not related to
what elements or tags they are part of.
10 Styling Web pages with CSS-2 New Mexico Tech Computer Center
Pseudo-class names are always preceded by a colon (:). Pseudo-classes include:
:active
Affects an element during the time the user is clicking on it.
:focus
Affects the element that has focus, that is, the element that would currently receive any keyboard
input.
:hover
Affects the appearance of an element while the mouse is on top of it, but not clicked.
:lang(code)
Affects only elements with a language that matches the given code. For example, this rule would
display any French elements in blue:
:link
Affects the appearance of links that have not been visited. For example, this rule would make un-
visited links appear in purple:
:visited
Affects the appearance of links that have been visited.
7.9. Pseudo-elements
A pseudo-element is a selector that refers to a specific area of the page. There are two types of pseudo-
elements:
The :first-letter and :first-line pseudo-elements refer to specific parts of a paragraph.
There is no other way to refer to the first line of a paragraph, since we only know what is in that line
when the page is actually rendered.
The :before and :after pseudo-elements allow you to add content before and after some other
element.
Here are the pseudo-elements in CSS2.
:after
Displays specific content after an element. Use the content property of the rule to specify what
text is displayed after an element.
For example, this rule would display two closing square brackets after any level-2 heading:
:before
Like :after, but this selector displays specific content before an element. Use the content property
of the rule to specify what text is displayed after an element.
For example, this rule would display two opening square brackets before any level-2 heading:
One common use of this selector is to insert generated content, such as section numbers. See Sec-
tion 16, The content property: Specifying content in pseudo-elements (p. 29).
New Mexico Tech Computer Center Styling Web pages with CSS-2 11
:first-letter
Affects the appearance of the first letter of an element. For example, this rule:
would display the first letter of each paragraph in a large size. In combination with other properties,
you can use this pseudo-class to get a drop cap effect (starting a paragraph with one large letter).
:first-line
Affects the appearance of the first line of a block element (such as a text paragraph). For example,
this rule
would display the first line of every paragraph all in capital letters.
* { color: green; }
This selector is the least specific selector, so any more specific selector will override it. See Section 8.2,
Specificity: Which selector applies? (p. 13).
8.1. Cascading
Up to three style sheets may apply to a page.
The user can provide their own stylesheet.
The author of the page may attach a style sheet to the page.
The browser may have a built-in style sheet that applies if neither the user nor the author has provided
one.
Normally, the page author's style sheet has the highest priority, followed by the user's, with the browser's
style last.
However, a rule can override rules from other, higher-priority style sheets by including the special
keyword !important after the property value. For example, suppose this rule appears in the user's
style sheet:
12 Styling Web pages with CSS-2 New Mexico Tech Computer Center
Paragraphs with class='admon' would be set in italics, even if the author's style sheet specifies a
different font-style value.
Here is the ranking of all possible sources with and without the !important keyword, from highest
to lowest priority:
User stylesheet with !important.
Author stylesheet with !important. (This has changed since the CSS-1 specification, which gave
author rules precedence over user rules.)
Browser stylesheet with !important.
Author stylesheet without !important.
User stylesheet without !important.
Browser stylesheet without !important.
8.3. Inheritance
Most properties inherit from the properties of parent elements. That is, a property that applies to an
element generally applies to all of the elements inside it.
For example, the HTML body element is the parent element for the content of a web page. So a rule
such as
would apply to the entire page unless overruled by a more specific rule at a lower level.
Some properties, however, do not inherit. For example, the background property determines what
appears behind an element. The default value of this property is transparent, so if there is no specific
rule applying to the background of an element, its background is invisible and you see the background
of the parent element behind it.
9. Declarations
The declaration part of a CSS rule is one of the parts inside the curly braces { } following the selector.
Each declaration has the form
property: value
where the property is some aspect of page rendering, and the value specifies how that property
should be rendered.
Following sections discuss the various properties and the values they take.
New Mexico Tech Computer Center Styling Web pages with CSS-2 13
Section 10, Font properties (p. 14).
Section 11, The display property: What kind of box is this? (p. 16).
Section 12, Other text properties (p. 17).
Section 13, The color property (p. 19).
Section 14, The background properties (p. 20).
Section 15, Designing with box elements (p. 21).
Section 16, The content property: Specifying content in pseudo-elements (p. 29).
Section 17, The list properties (p. 31).
Section 18, Tables (p. 32).
Section 19, User interface options (p. 35).
Section 20, Aural stylesheets (p. 38).
Note that family names containing spaces must be enclosed in quotes. This rule says to use Garamond
if available; use Times if Garamond is not available; and so on, using the generic family serif of none
of the named fonts are available.
14 Styling Web pages with CSS-2 New Mexico Tech Computer Center
10.2. The font-style property
Allowable values are normal for vertical text (the default), italic for italics, or oblique for fonts
that look like regular text, only slanted.
In the example above, an h2 element inside a body element would be displayed in a larger font than
medium, and a p element would use a smaller font.
By naming a specific font size. The units allowed are discussed elsewhere; see Section 6.1, Dimen-
sions (p. 5). Here is an example:
This rule would set in 14-point type all elements of class special inside pre elements.
As a percentage of the parent font size. Example:
em { font-size: 120%; }
This rule would set text inside an em element 20% larger than the parent font.
New Mexico Tech Computer Center Styling Web pages with CSS-2 15
p { font: italic bold normal 12pt bodoni, bembo, serif; }
16 Styling Web pages with CSS-2 New Mexico Tech Computer Center
12. Other text properties
In addition to font selection, CSS has a number of other properties for controlling the presentation of
text.
New Mexico Tech Computer Center Styling Web pages with CSS-2 17
12.5. The text-transform property
These properties can change the case of text:
The capitalize value capitalizes the first letter of each word.
The uppercase value changes all letters to capitals.
The lowercase value changes all letters to lowercase.
would add one point of horizontal space between the letters of any heading element <h1
class='shout'><h1>.
18 Styling Web pages with CSS-2 New Mexico Tech Computer Center
sub
Shift this text down as if it were a subscript. Note that this does not change the size of the text.
super
Shift this text up as if it were a superscript. This property does not change the size of the text.
text-top
The top of this text will be vertically aligned with the top of text in the containing block.
text-bottom
The bottom of this text will be vertically aligned with the bottom of text in the containing block.
int%
Raise this text an amount specified as the percentage of the current line-height. For example, if
the current line-height is 12 points, this declaration
vertical-align: 25%;
vertical-align: -2mm;
would lower the text by two millimeters. See Section 6.1, Dimensions (p. 5).
The quote symbols you set up with this declaration are invoked when the content property contains
open-quote and close-quote values. For more on this property, see Section 16, The content
property: Specifying content in pseudo-elements (p. 29).
h3 { color: maroon;
}
New Mexico Tech Computer Center Styling Web pages with CSS-2 19
would display all level h3 headings with maroon letters.
20 Styling Web pages with CSS-2 New Mexico Tech Computer Center
For example, this rule
would center the image horizontally, and move it down half an inch from the top edge.
background
You can specify all of the above properties in a single rule with the background property. The list
of values for the property can include any of the values shown above for background-color,
background-image, background-repeat, background-attachment, or background-po-
sition.
For example:
margin
border
padding
block
content
The background of the block's content may be a color or an image set either by the background
property of the block, or inherited from some containing block.
Padding separates the box's content from what is outside. It also shows the background color or image,
if any. The default padding size is zero.
An optional border can be drawn around the outside of the padding area. This can be a solid, dashed,
or dotted line of any color or thickness. The default is to have no border.
New Mexico Tech Computer Center Styling Web pages with CSS-2 21
The margin is an area that is supposed to be kept clear. The margin area is always transparent, so
whatever is behind it will show through.
When two such boxes are stacked vertically adjacent, their margin areas may overlap, but never their
borders, padding, or content.
block A
block B
In this diagram, distance d is either the bottom margin of box A, or the top margin of box B, whichever
is larger. (In some cases, margins may not overlap. Refer to the standard for pages of mind-numbing
detail.)
For each box property, you can specify different values for top, bottom, left, and right. For example, the
same box can have a thick black solid top border, a thin red dotted bottom border, an inch-thick dotted
left border, and no right border at all.
Box properties fall into several groups:
Section 15.5, The padding properties (p. 23).
Section 15.6, The border properties (p. 24).
Section 15.7, The margin properties (p. 25).
Shape properties describe the size of the box, if necessary. The default size of a box is the full width
of the browser window with just enough height to accommodate the contents. These properties include
height and width. See Section 15.2, The height and width properties (p. 23).
Positioning properties specify where the browser is allowed to place the box relative to other elements.
They include clear and float. See Section 15.3, The clear property (p. 23) and Section 15.4,
The float property (p. 23).
22 Styling Web pages with CSS-2 New Mexico Tech Computer Center
Side list Top Right Bottom Left
ab a b a b
abc a b c b
abcd a b c d
New Mexico Tech Computer Center Styling Web pages with CSS-2 23
As a dimension (see Section 6.1, Dimensions (p. 5)). For example, padding-right: 2pc would
add two picas (about a third of an inch) of space between the contents of the box and its right side.
As a number followed by a percent sign (%). The number is interpreted as a percentage of the width
of the browser window, even for padding-top and padding-bottom.
The usual way of specifying padding is to specify the amount of padding on all four sides in one rule
like this:
padding: side-list;
where the side-list is as described in Section 15.1, Side lists (p. 22).
For example, to specify an extra 1.5 picas of padding:
padding: 1.5pc;
To specify one em at the top and bottom, and two ems of padding on the sides:
You can also specify the padding for only one side with these properties:
padding-top
padding-left
padding-bottom
padding-right
24 Styling Web pages with CSS-2 New Mexico Tech Computer Center
none: This is the default styleno visible border.
solid: The border is a single solid line.
double: A double solid line.
dashed: Displays a dashed border.
dotted: A border of dots.
inset: A 3-d border that makes the contents look like they're set more deeply into the page.
outset: A 3-d border that makes the contents look raised against the background.
groove: A 3-d effect that makes the border look inset.
ridge: A 3-d effect that makes the border look outset.
You can also set the styles of the four sides of the borders individually using these properties:
border-top-style
border-left-style
border-bottom-style
border-right-style
border-color
By default, borders are black. To specify a different border color, use the border-color property.
The value of this property is a side list (see Section 15.1, Side lists (p. 22). See Section 6.2, Spe-
cifying colors (p. 6)) for the various ways of specifying colors.
You can specify the color of each side of the border individually using these properties:
border-top-color
border-left-color
border-bottom-color
border-right-color
border
You can specify multiple properties for any side of the border using these property names:
border-top
border-right
border-bottom
border-left
You can set each of the above properties to a list containing a border thickness (as thin, medium,
thick, or a dimension), border style (using any of the values legal for border-style), or border
color (as in border-color).
You can specify all of the border properties of a box element by using the border property. Set the
value of this property to a list containing any of the values allowed for border-style, border-
color, or border-width.
For example, this rule
would display <div class='orchid'></div> elements with a one-pixel thick, maroon, dashed
border.
New Mexico Tech Computer Center Styling Web pages with CSS-2 25
A dimension (see Section 6.1, Dimensions (p. 5)). For example, margin-left: 10mm would
add an extra 10 millimeters of margin outside the left side of the box.
A number followed by a percent sign (%), interpreted as a percentage of the browser window width
(even for the top and bottom margin values).
Most commonly, all four margins are set at once with the margin property. The value of this property
is a side list (see Section 15.1, Side lists (p. 22)).
For example, this rule specifies a one-em margin around all div elements:
To specify the margin on a specific side of the box, use one of:
margin-top
margin-right
margin-bottom
margin-left
26 Styling Web pages with CSS-2 New Mexico Tech Computer Center
sions (p. 5)). You can use negative dimensions to allow the clipping rectangle to extend outside
the containing box.
Here's an example. This declaration would clip half an inch inside the top and bottom of the box,
but allow text to extent an inch outside the left and right sides:
15.12. The box offset properties: top, bottom, left, and right
When a box's position property is not static (see Section 15.11, The position property: Positioning
boxes (p. 27)), these four properties specify where the box's edges are relative to some other box:
top
bottom
left
right
Each property takes one of these values:
dimension
Shift the box's edge inward by the value of this dimension; see Section 6.1, Dimensions (p. 5).
A negative value shifts the box's edge outwards.
For example, these declarations would shift a box 5mm to the right:
New Mexico Tech Computer Center Styling Web pages with CSS-2 27
position: relative;
right: -5mm;
int%
Shift the box's edge as a percentage of some value. For relatively positioned boxes, this is a percentage
of the box to be rendered. For absolutely positioned boxes, it is a percentage of the size of the con-
taining box. For example, these declarations move a box to the right 10% of its width:
position: relative;
left: 10%;
auto
The default value: do not shift this box edge.
<div id="d1">
<img src="i1.jpg">
</div>
<div id="d2">
<img src="i2.jpg">
<div id="d3">
<img src="i3.jpg">
</div>
</div>
div
{ position: absolute;
top: 0in;
left: 0in;
28 Styling Web pages with CSS-2 New Mexico Tech Computer Center
}
div#d1 { z-index: 5; }
div#d2 { z-index: 3; }
div#d3 { z-index: 15; }
The first rule says that all div elements are absolutely positioned at the top left corner of the page. The
next three rules assign z-index values to the div elements in the HTML.
There are two different stacking contexts here. Block d1 and block d2 are in the outermost (global)
stacking contexts, so block d1, with a z-index value of 5, is in front of d2 with a z-index of 3.
Block d3, however, is inside block d2's local stacking context, so block d2 and everything inside it are
stacked together. Block d3's z-index value of 15 only influences how it stacks relative to any other
elements inside of block d2.
Therefore, image i3.jpg, with a z-index of 15 inside that local stacking context, stacks in front of
image i2.jpg, which has the base z-index of 0 inside that local context.
However, on the final page, image i1.jpg still appears in front of i3.jpg, because it has a higher z-
index in the global stacking context.
New Mexico Tech Computer Center Styling Web pages with CSS-2 29
Use of this value increments the quote-nesting level.
close-quote
Adds the closing quote character for the current level of nesting. Use of this value decrements the
quote-nesting level.
no-open-quote
Like open-quote, this value increments the quote-nesting level, but it does not generate any content.
no-close-quote
Like close-quote, this value decrements the quote-nesting level, but generates no content.
attr(aname)
Generates the value of the aname attribute of the element to which this rule applies.
For example, suppose your document's div elements of class dog have an attribute dogname. This
rule would insert the value of that attribute, in parentheses, as content after the rendering of the
div:
div.dog
{ :after
{ " (" attr(dogname) ")"
}
}
counter-reset: chapter-no;
counter-reset: section-no 10;
The first example sets the counter named chapter-no to zero. The second example sets counter sec-
tion-no to ten.
30 Styling Web pages with CSS-2 New Mexico Tech Computer Center
name
The named counter is incremented by one.
name int
The given integer is added to the named counter.
Examples:
counter-increment: chapter-no;
counter-increment: section-no 10;
The first example adds one to the counter named chapter-no. The second example adds ten to the
counter named section-no.
5
http://www.w3.org/Style/CSS/
New Mexico Tech Computer Center Styling Web pages with CSS-2 31
This behavior is selected by setting list-style-position: outside.
However, if you set list-style-position: inside, the second and following lines will start lined
up with the left edge of the bullet (or section number); that is, the bullet or section number will be run
into the text.
would display any list tagged as ul class='pterry' using disc-shaped bullets run in to the text.
18. Tables
CSS allows you to format elements as a table. A table consists of an optional caption and one or more
rows and columns. Each item in the grid is called a cell. Additionally, you can combine multiple rows
into row groups that share style features, and you can combine multiple columns can into column groups.
You can designate one or more rows at the top of the table as a table header group. You can also designate
one or more rows at the bottom as a table footer group. When a table has a table header group or table
footer group, and the table is rendered on paged media, the header group and footer group will be re-
peated at the top and bottom of each page, so the reader can see what the columns mean.
To present some element of your document act as one of these parts of a table, set the display property
for that element to one of these values:
table
Renders the element as a block containing a table.
inline-table
Renders the element as a table, but encloses the entire table in an inline element, so it can be placed
on a line with other inlines.
table-row
The element acts as one row of a table.
table-row-group
The element is presented as a row group.
table-header-group
The element is a group of header lines.
32 Styling Web pages with CSS-2 New Mexico Tech Computer Center
table-footer-group
The element is a group of footer lines.
table-column
A rule for a table-column does not actually render any content. Such a rule can, however, carry
properties that can be inherited by the cells in that column.
table-column-group
Like table-column, a rule with this display value does not render content, but can carry prop-
erties to be inherited by columns in the group.
table-cell
The item is rendered as one cell of a table.
table-caption
The item is presented as a table caption.
Note
A minimal table consists of a table or inline-table containing at least one table-row, which in
turn contains at least one table-cell.
You don't have to supply all these parts to create a table. Any missing parts will be added automatically.
For example, if you render a series of objects as table cells, an anomymous table row and table will be
created to contain them. Similarly, if you render some objects as table rows, but they aren't inside a table
element, an anonymous table will be created to contain the rows.
The various parts of a table render as if they were stacked in this order (from front to back): cells, rows,
row groups, columns, column groups, and the table itself. This means, for example, that if the background
of a cell is transparent, but its row has a background color, that color will show as the background color
of the cell.
New Mexico Tech Computer Center Styling Web pages with CSS-2 33
18.2. How table size is computed
The first property that affects table size and layout is the caption-side property. This property can
take any of these values:
top is the default caption position. The caption appears above the table.
bottom places the caption below the table.
left or right places the caption to the left or right of the table.
The width of a table depends on the table-layout property. Select one of these two values:
fixed
This layout method starts rendering the table as soon as the first row is complete, so it is a good
choice if you have a huge table and you don't want the reader to have to wait for the entire thing
to be rendered.
The agent looks first at each column to see if it has a fixed width attribute, and uses that width if
it is specified. Otherwise it looks at the first row's cell in that column to see if that cell has a fixed
width. Failing that, it distributes the table's width over the cells that don't declare a fixed width.
The final table's width is the value of the table's width property, or the sum of the column widths,
whichever is greater.
In the second and succeeding rows, any cells that are wider than the computed column width will
not cause the column to get any wider. The overflow property of the cell determines what happens
to the extra content.
auto
Rendering a table with table-layout set to auto is more flexible, but a table can't be rendered
completely until the agent looks at all the rows.
In this method, the agent computes the minimum and maximum width of the contents of each cell
in a column. For example, if a cell contains text, the minimum width is the width of the longest line
if the content were broken into the maximum number of lines, and the maximum width is how
wide that cell would be if it were rendered all on one line.
The final width of the table depends on the table's width property (which is auto by default.
If the table's width is auto, and the total of the maximum column sizes is less than the width of
the block containing the table, that total is used. Otherwise, the table's width is the sum of the
minimum column sizes, or the width of the containing block, whichever is larger.
If the table's width is a specific value, the rendered width is either that value, or the sum of the
minimum column sizes, whichever is larger.
The height of a given row in the table is the height of the tallest cell in the row, or the height property
of the row, whichever is larger.
The height of a given cell is the height of its content, or the value of its height property, whichever is
larger.
34 Styling Web pages with CSS-2 New Mexico Tech Computer Center
collapse
This is the default. A cell may have border properties set on itself, its row, its row group, its column,
its column group, or the table itself.
The actual border rendered at any given spot follows these rules:
1. If any border-style property that affects the cell has the value hidden, there will be no border.
2. If any of the relevant border-style properties are none but others are not none, the none
property will be ignored.
3. Wider borders have precedence over narrower borders.
4. If all the relevant border properties have the same width, the border with the higher-priority
style wins. From highest to lowest priority, the styles are double, solid, dashed, dotted,
ridge, outset, groove, and inset.
5. If relevant border properties differ only by their color, closer elements win over more distant
elements. From closer to further, the elements are cell, row, row group, column, column group,
and the table itself.
separate
With this option, every cell has its own border. Any border properties on rows, row groups, columns,
or column groups are ignored.
Two additional properties control the appearance with the separate option:
border-spacing
Set this property to one or two dimensions (see Section 6.1, Dimensions (p. 5)). If you supply
one value, it will be used for both vertical and horizontal spacing between cells. If you supply
two values, the first gives the horizontal spacing and the second the vertical spacing.
empty-cells
This property controls the appearance of cells that don't have any content. (You can use the
non-breaking space character, , to force the appearance of content when there is none.)
The default value of this property is show, which forces empty cells to have borders. Set this
property to hide if you want empty cells to be displayed as if there were no cell there. If you
select hide and all the cells in the row are empty, the entire row will disappear.
New Mexico Tech Computer Center Styling Web pages with CSS-2 35
Section 19.3, Dynamic outlines (p. 37).
The cursor urging the reader to be patient, typically a watch or hourglass image such as:
36 Styling Web pages with CSS-2 New Mexico Tech Computer Center
ButtonShadow The color on the sides of 3-d elements that are facing away from the apparent
light source.
CaptionText The color of the text in captions and scrollbar arrow boxes.
GrayText The color of text on a disabled control.
Highlight The color of selected items in a control.
InactiveBorder The border color of an inactive window.
InactiveCaption The caption color of an inactive window.
InactiveCaptionText The text caption color of an inactive window.
InfoBackground The background color for tooltip controls.
InfoText The text color for tooltip controls.
Menu The background color of menus.
MenuText The text color of menus.
Scrollbar The color of the trough area of a scrollbar, not occupied by a scroll thumb.
ThreeDDarkShadow The darker shadow color of a three-dimensional element, on sides facing
away from the apparent light source.
ThreeDFace The foreground color of a three-dimensional element.
ThreeDHighlight The highlight color of a three-dimensional element.
ThreeDLightShadow The lighter shadow color of a three-dimensional element, on sides facing
toward the apparent light source.
ThreeDShadow Same as ThreeDDarkShadow.
Window The window background color.
WindowFrame The color of window frames.
WindowText The color of text in windows.
New Mexico Tech Computer Center Styling Web pages with CSS-2 37
outline
This is a combination property that allows you to specify any of the color, width, and style values
at once. For example, this rule sets up a wide dashed red border around the div element whose id
is n23:
38 Styling Web pages with CSS-2 New Mexico Tech Computer Center
center
centerleft centerright
0
340 20
left right
320
40
farleft 300 farright
60
240 120
farleft behind farright behind
220 140
This figure shows the various azimuth keywords and the equivalent angles.
If the rendering agent has speakers above and below the listener, we can also specify the apparent ver-
tical position of the source.
Set the elevation property to one of:
angle
The angle of the source relative to the horizontal; see Section 6.6, Specifying angles (p. 7). For
example, the declaration elevation: -20deg; would place the source twenty degrees below the
horizon.
above
The source comes from the zenith, straight up.
below
The source comes from the nadir, straight down.
higher
The source is placed 10 higher than the elevation property of the parent element.
lower
The source is placed 10 lower than the elevation property of the parent element.
New Mexico Tech Computer Center Styling Web pages with CSS-2 39
the user agent, not CSS, to determine what these numbers represent, taking into account the back-
ground noise level and the dynamic range of the audio system.
silent
Turn the sound off altogether.
x-soft
Extra-soft.
soft
A relatively soft volume.
medium
A medium volume. This is the default value.
loud
A moderately loud volume.
x-loud
Extra-loud volume.
40 Styling Web pages with CSS-2 New Mexico Tech Computer Center
20.5. General voice qualities: voice-family, pitch, pitch-range, stress,
and richness
The voice-family property selects a general voice type or specific personality. Values may include:
female
A generic female voice.
male
A generic male voice.
child
A generic child's voice.
personality
A given voice synthesizer may have any number of specific named voices, somewhat like type font
families. Examples: gary owens, talullah bankhead, bullwinkle, lounge singer.
You can provide a comma-separated list of voice names as the value of the voice-family property,
and the agent will try them in the given order until it finds one that is actually available. Just as you
can't assume any specific font will be available to any reader's browser, it's safest to provide one of the
generic voices as a fail-safe at the end of the list. Example:
The pitch property controls whether a voice is low or high. You may specify the value as a frequency
(see Section 6.8, Frequencies (p. 8)), or any of the values x-low (extra-low), low, medium (the default),
high, or x-high for extra-high.
Use the pitch-range property to control how much the synthesized voice varies in pitch. The value
of this property is a number from 0 to 100, the default being 50. A declaration pitch-range: 0
would render the voice as a flat, uninflected monotone. Values above 50 give you higher amounts of
pitch range.
The stress property controls how much the voice varies in stress. The value is a number from 0 to
100. A zero value would suppress all stress variations. The default value is 50. Higher values add more
variation in stress, as if the speaker were agitated.
Finally, the richness property is also a number from 0 to 100, defaulitng to 50. Lower values produce
a smoother voice; higher values produce a voice that carries better in a larger room.
New Mexico Tech Computer Center Styling Web pages with CSS-2 41
fast
About 300 wpm.
x-fast
About 500 wpm.
faster
About 40 wpm faster than the speech-rate of the parent element.
slower
About 40 wpm less than the speech-rate of the parent element.
To add a bit of extra silence before an element, set its pause-before property; to add some after it,
set pause-after. Values allowed:
time
The syntax for specifying times is discussed in Section 6.7, Times (p. 7).
percentage
A number followed by % specifies the duration as a percentage of an average word length, as
specified by the speech-rate property (see Section 20.2, Voice properties (p. 39)).
For example, if the current speech rate is 180 words per minute, the average time per word is 1/3
of a second, so a pause-before value of 200% would add a gap of about 2/3 of a second.
There is also a combination pause property that sets both the pause-before and pause-after
properties. If pause is followed by one value, both properties are set to that value; if there are two
values, pause-before gets the first value and pause-after the second.
For example, this rule inserts a 150-millisecond pause both before and after any h2 element:
h2 { pause: 150ms; }
This rule would play reveille.wav before each <p class='warning'></p> element, and play
retreat.wav after it.
42 Styling Web pages with CSS-2 New Mexico Tech Computer Center
uri
Retrieve the recording from the given URI. The value may be followed by either of two keywords:
mix
If this keyword is given, and the parent element has also specified a recording with the play-
during property, mix the parent's recording with this element's recording. Without this key-
board, the child element's recording would replace that of the parent.
repeat
If given, this keyword tells the agent to repeat the recording as many times as necessary to be
heard behind the rendering of the element. The default is to play it only once. If the recording
is longer than the rendering of the element, it is cut off when the element is finished rendering.
auto
If the parent element has a play-during recording, continue playing it back as this element is
rendered.
none
Do not play a recording during this element.
inherit
If the parent element has a play-during recording, start it over again for the current element.
@import url(string);
@import string;
Warning
If you use an @import, it must precede all other rules in the same stylesheet.
The effect of importing another stylesheet is just as if the other stylesheet were copied into yours at that
locationwith one important difference. Imported rules have a lower priority than your other rules.
For example, suppose your stylesheet contains this at-rule:
@import url("basic.css");
Further suppose that your stylesheet has a rule for paragraphs, with selector p, and the basic.css
stylesheet also has a rule for paragraphs, with the same selector. In that case, your rule will be used.
You may also specify a comma-separated list of media types after the URL part of the rule. In this case,
the importation of the other stylesheet would happen only if the content were being rendered in one of
the selected media types.
For example, this rule:
would apply only for content rendered by a speech synthesizer (aural) or television (tv).
For more information, see Section 22.1, Media types (p. 44).
New Mexico Tech Computer Center Styling Web pages with CSS-2 43
22. The @media rule: Tuning for different rendering platforms
If you want certain rules to be applied only for certain media types, wrap them in a @media at-rule
with this format:
where the @media keyword is followed by a comma-separated list of media types, and the rules inside
the curly braces are used only when the content is rendered on one of those media types. See Section 22.1,
Media types (p. 44).
For example, these lines would specify 14-point fonts on a screen, 12-point fonts on paper, and brown
type on a yellow background in both cases:
@media screen
{ body
{ font-size: 14pt;
}
}
@media print
{ body
{ font-size: 12pt;
}
}
@media print, screen
{ body
{ color: brown;
background-color: yellow;
}
}
44 Styling Web pages with CSS-2 New Mexico Tech Computer Center
Grid
Can this device render individual pixels, or is it restricted to a grid of characters? Example: ancient
CRT displays that show only 24 rows of 80 characters. Values are:
grid for devices restricted to a character grid.
bitmap for devices that can render individual pixels.
Interactive
Can this medium change in response to user actions? A screen can, but paper can't. Values are:
interactive
static (non-interactive)
Here is a table showing the current set of media types and the attributes of the various media groups.
@page { }
@page name { }
See Section 23.6, The page attribute: Selecting a page type (p. 48) for more information on named
pages.
If you want some rules to apply only to odd pages, even pages, or the first page of the set, use this option
to the @page at-rule:
@page page-selector { }
New Mexico Tech Computer Center Styling Web pages with CSS-2 45
where the page-selector is one of:
:right for right-hand pages.
:left for left-hand pages.
:first for the first page.
You can use margin properties inside the @page rule to define the margins of the page. For example,
this rule would set up a page with one-inch margins all around:
You may also use the individual margin properties such as margin-left to set different values for
different margins. See Section 15.7, The margin properties (p. 25).
The declarations described below may appear only inside an @page rule. They apply only to presentation
of the content on fixed-size pages.
Section 23.1, The size property for paged media (p. 46).
Section 23.2, Controlling page breaks (p. 46).
Section 23.3, Orphan control (p. 47).
Section 23.4, Widow control (p. 47).
Section 23.5, Crop marks and alignment targets: the marks property (p. 47).
Section 23.6, The page attribute: Selecting a page type (p. 48).
size: auto;
Sets the page size to the sheet size.
size: portrait;
Selects portrait orientation, with the longer sides vertical.
size: landscape;
Selects landscape orientation, with the longer sides horizontal.
46 Styling Web pages with CSS-2 New Mexico Tech Computer Center
page-break-after
Specifies whether page breaks are allowed after some element. Values are the same as for page-
break-before: always means always start a new page after the element; avoid prevents a page
break after the element; right forces the following element to start on a new right-hand page; and
left forces the following element to a new left-hand page.
page-break-inside
To prevent page breaks inside a given element, use this declaration:
page-break-inside: avoid;
orphans: n;
For example, if you want to make sure that a p (paragraph) element has no orphans of three lines or
fewer:
@page
{ p { orphans: 3; }
}
widows: n;
@page
{ li { widows: 1; }
}
New Mexico Tech Computer Center Styling Web pages with CSS-2 47
marks: crop cross;
page: page-name
where the page-name is specified just after the @page in the at-rule.
For example, suppose you want all table elements to be displayed on a page with landscape orientation.
First you define a new page-name that specifies a landscape page:
Then you connect the table element with that page name using this rule:
48 Styling Web pages with CSS-2 New Mexico Tech Computer Center