1. About the CSS 2.2 Specification
1.1. CSS 2.2 vs CSS 2
The CSS community has gained significant experience with the CSS2 specification since it became a recommendation in 1998. Errors in the CSS2 specification [CSS20] have subsequently been corrected in the first revised edition [CSS21] in 2011, but new errata were necessary.
While many of the issues will be addressed by the upcoming CSS3 specifications, the current state of affairs hinders the implementation and interoperability of CSS2. The CSS 2.2 specification attempts to address this situation by:
- Maintaining compatibility with those portions of CSS2 that are widely accepted and implemented.
- Incorporating all published CSS2 errata.
- Where implementations overwhelmingly differ from the CSS2 specification, modifying the specification to be in accordance with generally accepted practice.
- Removing CSS2 features which, by virtue of not having been implemented, have been rejected by the CSS community. CSS 2.2 aims to reflect what CSS features are reasonably widely implemented for HTML and XML languages in general (rather than only for a particular XML language, or only for HTML).
- Removing CSS2 features that will be obsoleted by CSS3, thus encouraging adoption of the proposed CSS3 features in their place.
- Adding a (very) small number of new property values, when implementation experience has shown that they are needed for implementing CSS2.
Thus, while it is not the case that a CSS2 style sheet is necessarily forwards-compatible with CSS 2.2, it is the case that a style sheet restricting itself to CSS 2.2 features is more likely to find a compliant user agent today and to preserve forwards compatibility in the future. While breaking forward compatibility is not desirable, we believe the advantages to the revisions in CSS 2.2 are worthwhile.
CSS 2.2 is derived from and is intended to replace CSS 2.1 and CSS2. Some parts of CSS2 are unchanged in CSS 2.2, some parts have been altered, and some parts removed. The removed portions may be used in a future CSS3 specification. Future specs should refer to CSS 2.2 (unless they need features from CSS2 which have been dropped in CSS 2.2, and then they should only reference CSS2 for those features, or preferably reference such feature(s) in the respective CSS3 Module that includes those feature(s)).
1.2. Reading the specification
This section is non-normative.
This specification has been written with two types of readers in mind: CSS authors and CSS implementors. We hope the specification will provide authors with the tools they need to write efficient, attractive, and accessible documents, without overexposing them to CSS’s implementation details. Implementors, however, should find all they need to build conforming user agents.
The specification begins with a general presentation of CSS and becomes more and more technical and specific towards the end. For quick access to information, a general table of contents, specific tables of contents at the beginning of each section, and an index provide easy navigation, in both the electronic and printed versions.
The specification has been written with two modes of presentation in mind: electronic and printed. Although the two presentations will no doubt be similar, readers will find some differences. For example, links will not work in the printed version (obviously), and page numbers will not appear in the electronic version. In case of a discrepancy, the electronic version is considered the authoritative version of the document.
1.3. How the specification is organized
This section is non-normative.
The specification is organized into the following sections:
- Section 2: An introduction to CSS 2
- The introduction includes a brief tutorial on CSS 2 and a discussion of design principles behind CSS 2.
- Sections 3 - 18: CSS 2 reference manual.
- The bulk of the reference manual consists of the CSS 2 language reference. This reference defines what may go into a CSS 2 style sheet (syntax, properties, property values) and how user agents must interpret these style sheets in order to claim conformance.
- Appendixes:
- Appendixes contain information about a sample style sheet for HTML 4, changes from CSS 2.1, the grammar of CSS 2, a list of normative and informative references, and two indexes: one for properties and one general index.
1.4. Conventions
1.4.1. Document language elements and attributes
- CSS property and pseudo-class names are delimited by single quotes.
- CSS values are delimited by single quotes.
- Document language attribute names are in lowercase letters and delimited by double quotes.
1.4.2. CSS property definitions
Each CSS property definition begins with a summary of key information that resembles the following:
Name: | property-name |
---|---|
Value: | legal values & syntax |
Initial: | initial value |
Applies to: | elements this property applies to |
Inherited: | whether the property is inherited |
Percentages: | how percentage values are interpreted |
Computed value: | how to compute the computed value |
Canonical order: | per grammar |
Media: | which media groups the property applies to |
1.4.2.1. Value
This part specifies the set of valid values for the property whose name is property-name. A property value can have one or more components. Component value types are designated in several ways:
- keyword values (e.g., auto, disc, etc.)
- basic data types, which appear between "<" and ">" (e.g., <length>, <percentage>, etc.). In the electronic version of the document, each instance of a basic data type links to its definition.
- types that have the same range of values as a property bearing the same name (e.g., <border-width> <background-attachment>, etc.). In this case, the type name is the property name (complete with quotes) between "<" and ">" (e.g., <border-width>). Such a type does not include the value inherit. In the electronic version of the document, each instance of this type of non-terminal links to the corresponding property definition.
- non-terminals that do not share the same name as a property. In this case, the non-terminal name appears between "<" and ">", as in <border-width>. Notice the distinction between <border-width> and <border-width>; the latter is defined in terms of the former. The definition of a non-terminal is located near its first appearance in the specification. In the electronic version of the document, each instance of this type of value links to the corresponding value definition.
Other words in these definitions are keywords that must appear literally, without quotes (e.g., red). The slash (/) and the comma (,) must also appear literally.
Component values may be arranged into property values as follows:
- Several juxtaposed words mean that all of them must occur, in the given order.
- A bar (|) separates two or more alternatives: exactly one of them must occur.
- A double bar (||) separates two or more options: one or more of them must occur, in any order.
- A double ampersand (&&) separates two or more components, all of which must occur, in any order.
- Brackets ([ ]) are for grouping.
Juxtaposition is stronger than the double ampersand, the double ampersand is stronger than the double bar, and the double bar is stronger than the bar. Thus, the following lines are equivalent:
a b | c || d && e f [ a b ] | [ c || [ d && [ e f ]]]
Every type, keyword, or bracketed group may be followed by one of the following modifiers:
- An asterisk (*) indicates that the preceding type, word, or group occurs zero or more times.
- A plus (+) indicates that the preceding type, word, or group occurs one or more times.
- A question mark (?) indicates that the preceding type, word, or group is optional.
- A pair of numbers in curly braces ({A,B}) indicates that the preceding type, word, or group occurs at least A and at most B times.
The following examples illustrate different value types:
Value: N | NW | NE
Value: [ <length> | thick | thin ]{1,4}
Value: [<family-name> , ]* <family-name>
Value: <uri>? <color> [ / <color> ]?
Value: <uri> || <color>
Value: inset? && [ <length>{2,4} && <color>? ]
Component values are specified in terms of tokens, as described in Appendix G.2. As the grammar allows spaces
between tokens in the components of the expr
production,
spaces may appear between tokens in property values.
Note: In many cases, spaces will in fact be
required between tokens in order to distinguish them from
each other. For example, the value 1em2em would be parsed as a
single DIMEN
token with the number 1 and the identifier
em2em, which is an invalid unit. In this case, a space would be
required before the 2 to get this parsed as the two lengths 1em
and 2em.
1.4.2.2. Initial
This part specifies the property’s initial value. Please consult the section on the cascade for information about the interaction between style sheet-specified, inherited, and initial property values.
1.4.2.3. Applies to
This part lists the elements to which the property applies. All elements are considered to have all properties, but some properties have no rendering effect on some types of elements. For example, the clear property only affects block-level elements.
1.4.2.4. Inherited
This part indicates whether the value of the property is inherited from an ancestor element. Please consult the section on the cascade for information about the interaction between style sheet-specified, inherited, and initial property values.
1.4.2.5. Percentage values
This part indicates how percentages should be interpreted, if they occur in the value of the property. If "N/A" appears here, it means that the property does not accept percentages in its values.
1.4.2.6. Media groups
This part indicates the media groups to which the property applies. Information about media groups is non-normative.
1.4.2.7. Computed value
This part describes the computed value for the property. See the section on computed values for how this definition is used.
1.4.3. Shorthand properties
Some properties are shorthand properties, meaning that they allow authors to specify the values of several properties with a single property.
For instance, the font property is a shorthand property for setting font-style, font-variant, font-weight, font-size, line-height, and font-family all at once.
When values are omitted from a shorthand form, each "missing" property is assigned its initial value (see the section on the cascade).
The multiple style rules of this example:
h1{ font-weight : bold; font-size : 12 pt ; line-height : 14 pt ; font-family : Helvetica; font-variant : normal; font-style : normal; }
may be rewritten with a single shorthand property:
h1{ font : bold12 pt /14 pt Helvetica}
In this example, font-variant, and font-style take their initial values.
1.4.4. Notes and examples
All examples that illustrate illegal usage are clearly marked as "ILLEGAL EXAMPLE".
HTML examples lacking DOCTYPE declarations are SGML Text Entities conforming to the HTML 4.01 Strict DTD [HTML401]. Other HTML examples conform to the DTDs given in the examples.
All notes are informative only.
Examples and notes are marked within the source HTML for the specification and CSS user agents will render them specially.
1.4.5. Images and long descriptions
Most images in the electronic version of this specification are accompanied by "long descriptions" of what they represent. A link to the long description is denoted by a "[D]" after the image.
Images and long descriptions are informative only.
1.5. Acknowledgments
This section is non-normative.
CSS 2.2 is based on CSS2 (1998) and CSS 2.1. See the acknowledgments section of CSS2 and the acknowledgments section of CSS 2.1 for the people that contributed to CSS2 and CSS 2.1.
We would like to thank the following people who, through their input and feedback on the www-style mailing list, have helped us with the creation of this specification: Andrew Clover, Bernd Mielke, C. Bottelier, Christian Roth, Christoph Päper, Claus Färber, Coises, Craig Saila, Darren Ferguson, Dylan Schiemann, Etan Wexler, George Lund, James Craig, Jan Eirik Olufsen, Jan Roland Eriksson, Joris Huizer, Joshua Prowse, Kai Lahmann, Kevin Smith, Lachlan Cannon, Lars Knoll, Lauri Raittila, Mark Gallagher, Michael Day, Peter Sheerin, Rijk van Geijtenbeek, Robin Berjon, Scott Montgomery, Shelby Moore, Stuart Ballard, Tom Gilder, Vadim Plessky, Peter Moulder, Anton Prowse, Gérard Talbot, Ingo Chao, Bruno Fassino, Justin Rogers, Boris Zbarsky, Garrett Smith, Zack Weinberg, Bjoern Hoehrmann, and the Open eBook Publication Structure Working Group Editors. We would also like to thank Gary Schnabl, Glenn Adams and Susan Lesch who helped proofread earlier versions of this document.
In addition, we would like to extend special thanks to Elika J. Etemad, Ada Chan and Boris Zbarsky who have contributed significant time to CSS 2.1, and to Kimberly Blessing for help with the editing.
Many thanks also to the following people for their help with the test suite:
Robert Stam, Aharon Lanin, Alan Gresley, Alan Harder, Alexander Dawson, Arron Eicholz, Bernd Mielke, Bert Bos, Boris Zbarsky, Bruno Fassino, Daniel Schattenkirchner, David Hammond, David Hyatt, Eira Monstad, Elika J. Etemad, Gérard Talbot, Gabriele Romanato, Germain Garand, Hilbrand Edskes, Ian Hickson, James Hopkins, Justin Boss, L. David Baron, Lachlan Hunt, Magne Andersson, Marc Pacheco, Mark McKenzie-Bell, Matt Bradley, Melinda Grant, Michael Turnwall, Ray Kiddy, Richard Ishida, Robert O’Callahan, Simon Montagu, Tom Clancy, Vasil Dinkov, … and all the contributors to the CSS1 test suite.
Working Group members active during the development of this specification:
César Acebal (Universidad de Oviedo), Tab Atkins Jr. (Google, Inc.), L. David Baron (Mozilla Foundation), Bert Bos (W3C/ERCIM), Tantek Çelik (W3C Invited Experts), Cathy Chan (Nokia), Giorgi Chavchanidze (Opera Software), John Daggett (Mozilla Foundation), Beth Dakin (Apple, Inc.), Arron Eicholz (Microsoft Corp.), Elika J. Etemad (W3C Invited Experts), Simon Fraser (Apple, Inc.), Sylvain Galineau (Microsoft Corp.), Daniel Glazman (Disruptive Innovations), Molly Holzschlag (Opera Software), David Hyatt (Apple, Inc.), Richard Ishida (W3C/ERCIM), John Jansen (Microsoft Corp.), Brad Kemper (W3C Invited Experts), Håkon Wium Lie (Opera Software), Chris Lilley (W3C/ERCIM), Peter Linss (HP), Markus Mielke (Microsoft Corp.), Alex Mogilevsky (Microsoft Corp.), David Singer (Apple Inc.), Anne van Kesteren (Opera Software), Steve Zilles (Adobe Systems Inc.), Ian Hickson (Google, Inc.), Melinda Grant (HP), Øyvind Stenhaug (Opera Software), and Paul Nelson (Microsoft Corp.).
2. Introduction to CSS 2
2.1. A brief CSS 2 tutorial for HTML
This section is non-normative.
In this tutorial, we show how easy it can be to design simple style sheets. For this tutorial, you will need to know a little HTML (see [HTML401]) and some basic desktop publishing terminology.
We begin with a small HTML document:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> < HTML > < HEAD > < TITLE > Bach’s home page</ TITLE > </ HEAD > < BODY > < H1 > Bach’s home page</ H1 > < P > Johann Sebastian Bach was a prolific composer.</ BODY > </ HTML >
To set the text color of the H1 elements to red, you can write the following CSS rules:
h1{ color : red}
A CSS rule consists of two main parts: selector (h1) and declaration ('color: red'). In HTML, element names are case-insensitive so h1 works just as well as H1. The declaration has two parts: property name (color) and property value (red). While the example above tries to influence only one of the properties needed for rendering an HTML document, it qualifies as a style sheet on its own. Combined with other style sheets (one fundamental feature of CSS is that style sheets are combined), the rule will determine the final presentation of the document.
The HTML 4 specification defines how style sheet rules may be specified for HTML documents: either within the HTML document, or via an external style sheet. To put the style sheet into the document, use the STYLE element:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> < HTML > < HEAD > < TITLE > Bach’s home page</ TITLE > < STYLE type = "text/css" > h1 { color : red } </ STYLE > </ HEAD > < BODY > < H1 > Bach’s home page</ H1 > < P > Johann Sebastian Bach was a prolific composer.</ BODY > </ HTML >
For maximum flexibility, we recommend that authors specify external style sheets; they may be changed without modifying the source HTML document, and they may be shared among several documents. To link to an external style sheet, you can use the LINK element:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> < HTML > < HEAD > < TITLE > Bach’s home page</ TITLE > < LINK rel = "stylesheet" href = "bach.css" type = "text/css" > </ HEAD > < BODY > < H1 > Bach’s home page</ H1 > < P > Johann Sebastian Bach was a prolific composer.</ BODY > </ HTML >
The LINK element specifies:
- the type of link: to a "stylesheet".
- the location of the style sheet via the "href" attribute.
- the type of style sheet being linked: "text/css".
To show the close relationship between a style sheet and the structured markup, we continue to use the STYLE element in this tutorial. Let’s add more colors:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> < HTML > < HEAD > < TITLE > Bach’s home page</ TITLE > < STYLE type = "text/css" > body { color : black ; background : white } h1 { color : red ; background : white } </ STYLE > </ HEAD > < BODY > < H1 > Bach’s home page</ H1 > < P > Johann Sebastian Bach was a prolific composer.</ BODY > </ HTML >
The style sheet now contains four rules: the first two set the color and background of the BODY element (it’s a good idea to set the text color and background color together), while the last two set the color and the background of the H1 element. Since no color has been specified for the P element, it will inherit the color from its parent element, namely BODY. The H1 element is also a child element of BODY but the second rule overrides the inherited value. In CSS there are often such conflicts between different values, and this specification describes how to resolve them.
CSS 2 has more than 90 properties, including color. Let’s look at some of the others:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML> <HEAD> <TITLE>Bach’s home page</TITLE> <STYLE type="text/css"> body { font-family: "Gill Sans", sans-serif; font-size: 12pt; margin: 3em; } </STYLE> </HEAD> <BODY> <H1>Bach’s home page</H1> <P>Johann Sebastian Bach was a prolific composer. </BODY> </HTML>
The first thing to notice is that several declarations are grouped within a block enclosed by curly braces ({...}), and separated by semicolons, though the last declaration may also be followed by a semicolon.
The first declaration on the BODY element sets the font family to "Gill Sans". If that font is not available, the user agent (often referred to as a "browser") will use the sans-serif font family which is one of five generic font families which all users agents know. Child elements of BODY will inherit the value of the font-family property.
The second declaration sets the font size of the BODY element to 12 points. The "point" unit is commonly used in print-based typography to indicate font sizes and other length values. It’s an example of an absolute unit which does not scale relative to the environment.
The third declaration uses a relative unit which scales with regard to its surroundings. The "em" unit refers to the font size of the element. In this case the result is that the margins around the BODY element are three times wider than the font size.
2.2. A brief CSS 2 tutorial for XML
This section is non-normative.
CSS can be used with any structured document format, for example with applications of the eXtensible Markup Language [XML10]. In fact, XML depends more on style sheets than HTML, since authors can make up their own elements that user agents do not know how to display.
Here is a simple XML fragment:
<ARTICLE> <HEADLINE> Fredrick the Great meets Bach</HEADLINE> <AUTHOR> Johann Nikolaus Forkel</AUTHOR> <PARA> One evening, just as he was getting his<INSTRUMENT> flute</INSTRUMENT> ready and his musicians were assembled, an officer brought him a list of the strangers who had arrived.</PARA> </ARTICLE>
To display this fragment in a document-like fashion, we must first declare which elements are inline-level (i.e., do not cause line breaks) and which are block-level (i.e., cause line breaks).
INSTRUMENT{ display : inline} ARTICLE, HEADLINE, AUTHOR, PARA{ display : block}
The first rule declares INSTRUMENT to be inline and the second rule, with its comma-separated list of selectors, declares all the other elements to be block-level. Element names in XML are case-sensitive, so a selector written in lowercase (e.g., instrument) is different from uppercase (e.g., INSTRUMENT).
One way of linking a style sheet to an XML document is to use a processing instruction:
<?xml-stylesheet type="text/css" href="bach.css"?> <ARTICLE> <HEADLINE> Fredrick the Great meets Bach</HEADLINE> <AUTHOR> Johann Nikolaus Forkel</AUTHOR> <PARA> One evening, just as he was getting his<INSTRUMENT> flute</INSTRUMENT> ready and his musicians were assembled, an officer brought him a list of the strangers who had arrived.</PARA> </ARTICLE>
A visual user agent could format the above example as:
Notice that the word "flute" remains within the paragraph since it is the content of the inline element INSTRUMENT.
Still, the text is not formatted the way you would expect. For example, the headline font size should be larger than then the rest of the text, and you may want to display the author’s name in italic:
INSTRUMENT{ display : inline} ARTICLE, HEADLINE, AUTHOR, PARA{ display : block} HEADLINE{ font-size : 1.3 em } AUTHOR{ font-style : italic} ARTICLE, HEADLINE, AUTHOR, PARA{ margin : 0.5 em }
A visual user agent could format the above example as:
Adding more rules to the style sheet will allow you to further describe the presentation of the document.
2.3. The CSS 2 processing model
This section up to but not including its subsections is non-normative.
This section presents one possible model of how user agents that support CSS work. This is only a conceptual model; real implementations may vary.
In this model, a user agent processes a source by going through the following steps:
- Parse the source document and create a document tree.
- Identify the target media type.
- Retrieve all style sheets associated with the document that are specified for the target media type.
-
Annotate every element of the document tree by assigning a single
value to every property that is
applicable to the target media type.
Properties are assigned values according to the mechanisms described
in the section on cascading and
inheritance.
Part of the calculation of values depends on the formatting algorithm appropriate for the target media type. For example, if the target medium is the screen, user agents apply the visual formatting model.
-
From the annotated document tree, generate a
formatting
structure. Often, the formatting structure closely
resembles the document tree, but it may also differ significantly,
notably when authors make use of pseudo-elements and generated content.
First, the formatting structure need not be "tree-shaped" at all -- the
nature of the structure depends on the implementation. Second, the
formatting structure may contain more or less information than the
document tree. For instance, if an element in the document tree has a
value of none for the display property, that element will
generate nothing in the formatting structure. A list element, on the
other hand, may generate more information in the formatting structure:
the list element’s content and list style information (e.g., a bullet
image).
Note that the CSS user agent does not alter the document tree during this phase. In particular, content generated due to style sheets is not fed back to the document language processor (e.g., for reparsing).
- Transfer the formatting structure to the target medium (e.g., print the results, display them on the screen, render them as speech, etc.).
2.3.1. The canvas
For all media, the term canvas describes "the space where the formatting structure is rendered." The canvas is infinite for each dimension of the space, but rendering generally occurs within a finite region of the canvas, established by the user agent according to the target medium. For instance, user agents rendering to a screen generally impose a minimum width and choose an initial width based on the dimensions of the viewport. User agents rendering to a page generally impose width and height constraints. Aural user agents may impose limits in audio space, but not in time.
2.3.2. CSS 2 addressing model
CSS 2