The simplest selector is the name of an element from the object language, called a type selector. Type selectors match all instances of the element in the document.
H1 { font-family: Helvetica }
When element selectors share the same declarations, they may be grouped into comma-separated lists.
H1 { font-family: Helvetica }
H2 { font-family: Helvetica }
H3 { font-family: Helvetica }
is equivalent to:
H1, H2, H3 { font-family: Helvetica }
Furthermore, multiple declarations for the same selector may be organized into semi-colons separated groups.
Thus, the following rules:
H1 { font-weight: bold }
H1 { font-size: 12pt }
H1 { line-height: 14pt }
H1 { font-family: Helvetica }
H1 { font-variant: normal }
H1 { font-style: normal }
are equivalent to:
H1 {
font-weight: bold;
font-size: 12pt;
line-height: 14pt;
font-family: Helvetica;
font-variant: normal;
font-style: normal;
}
In addition, some properties are shorthand rules that 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.
The multiple style rules of the previous example:
H1 {
font-weight: bold;
font-size: 12pt;
line-height: 14pt;
font-family: Helvetica;
font-variant: normal;
font-style: normal;
}
may be rewritten with a single property:
H1 { font: bold 12pt/14pt Helvetica }
Note that since 'font-variant' and 'font-style' take their default values of 'normal' in this example, these values have been omitted from the shorthand form.
At times, authors may want selectors to match elements that appear in a certain context, such as "only those EM elements that are within an H1 element". In these cases, contextual selector add specificity.
A contextual selector is made up of two or more type selectors separated by white space. A contextual selector may also contain attribute selectors, but with only one attribute per simple selector.
Context is determined by the ancestors of an element in the document tree (and not its siblings or descendents).
For example, consider the following rules:
H1 { color: red }
EM { color: red }
Although the intention of these rules is to add emphasis to text by changing its color, the effect will be lost in a case such as:
<H1>This headline is <EM>very</EM> important</H1>
We address this special case by adding a contextual rule to the previous two that sets the text color to blue whenever an EM occurs immediately within an H1:
H1 EM { color: blue }
The following rules vary the appearance of nested list items:
OL OL { list-style: upper-alpha }
OL OL OL { list-style: lower-alpha }
The first rule matches all OL elements one level below another OL element. The second rule matches all OL elements with two OL ancestors.
Contextual selectors may be grouped according to the rules for grouping listed above.
In CSS2, style is normally attached to an element based on its position in the document tree. This simple model is sufficient for many cases, but some common publishing scenarios (such as changing the font size of the first letter of a paragraph) may be independent of the document tree. For instance, in [HTML40], no element refers to the first line of a paragraph, and therefore no simple CSS selector may refer to it.
CSS introduces the concepts of pseudo-elements and pseudo-classes to extend the addressing model and permit formatting based on information that lies outside the document tree. Pseudo-elements refer to sub-parts of an element's content (e.g., the first letter or first line of a paragraph, etc.). Pseudo-classes refer to elements that are grouped dynamically (e.g., all links that have been visited, all left-hand pages, etc.)
Although pseudo-elements and pseudo-classes do not exist in the document tree, their behavior is defined as if they did. Each pseudo-element and pseudo-class may be modeled by a fictional tag sequence, a fragment of document source that includes imaginary elements from the object language.
For instance, suppose we want to specify style information for the first line of a paragraph only. CSS defines a pseudo-element named ":first-line" which may be used as part of a selector:
P:first-line { font-style: small-caps }
The above rule means "change the font style of the first line of every paragraph to small-caps". However, the selector "P:first-line" does not match any real HTML element. It does match a pseudo-element that conforming user agents will insert at the beginning of every paragraph.
Note that the length of the first line depends on a number of factors, including the width of the page, the font size, etc. Suppose for this example that the paragraph is broken into the lines indicated in the example. Thus, an ordinary HTML paragraph such as:
<P>This is a somewhat long HTML paragraph that will be broken into several lines. The first line will be identified by a fictional tag sequence. The other lines will be treated as ordinary lines in the paragraph.</P>
will be "rewritten" by user agents to include the fictional tag sequence for :first-line.
<P> <P:first-line>This is a somewhat long HTML paragraph that will</P:first-line> be broken into several lines. The first line will be identified by a fictional tag sequence. The other lines will be treated as ordinary lines in the paragraph.</P>
If a pseudo-element breaks up a real element, the necessary extra tags must be regenerated in the fictional tag sequence. Thus, if we mark up the previous paragraph with a SPAN element:
<P><SPAN class="test">This is a somewhat long HTML paragraph that will be broken into several lines.</SPAN> The first line will be identified by a fictional tag sequence. The other lines will be treated as ordinary lines in the paragraph.</P>
The user agent must generate the appropriate start and end tags for SPAN when inserting the fictional tag sequence for :first-line.
<P><P:first-line><SPAN class="test">This is a somewhat long HTML paragraph that will</SPAN></P:first-line> <SPAN>be broken into several lines.</SPAN> The first line will be identified by a fictional tag sequence. The other lines will be treated as ordinary lines in the paragraph.</P>
Pseudo-element and pseudo-class names are case-insensitive.
Note. In CSS2, only one pseudo-element can be specified per selector. This may change in future versions of CSS.
Several pseudo element rules may refer to the same content.
In the following example, the first letter of each P element will be green with a font size of 24pt. The rest of the first line (as formatted on the canvas) will be blue while the rest of the paragraph will be red.
P { color: red; font-size: 12pt }
P:first-letter { color: green; font-size: 200% }
P:first-line { color: blue }
<P>Some text that ends up on two lines</P>
Assuming that a line break will occur before the word "ends", the fictional tag sequence for this fragment is:
<P> <P:first-line> <P:first-letter> S </P:first-letter>ome text that </P:first-line> ends up on two lines </P>
Note that the :first-letter element is inside the :first-line element. Properties set on :first-line will be inherited by :first-letter, but are overridden if the same property is set on :first-letter.
In a contextual selector, pseudo-elements are only allowed at the end of the selector.
The following example illustrates this with the :first-letter pseudo element.
BODY P:first-letter { color: purple }
Pseudo-classes may also be used in contextual selectors.
The following example sets the border color to blue of all images that descend from A elements that have not yet been visited:
A:link IMG { border: solid blue }
Some common typographical effects are associated not with structural elements but rather with typographical items as formatted on the canvas. In CSS2, two such typographical items can be addressed through pseudo-elements: the first line of an element, and the first letter.
Conforming UAs may ignore all rules with :first-line or :first-letter in the selector, or, alternatively, may only support a subset of the properties on these pseudo-elements. See the section on conformance for further information.
The :first-line pseudo-element is used to apply special styles to the first line as formatted on the canvas.
For example, on a text-based UA, this text:
<STYLE type="text/css">
P:first-line { font-style: small-caps }
</STYLE>
<P>The first line of an article in Newsweek.
could be formatted as:
THE FIRST LINE OF AN article in Newsweek.
The fictional tag sequence in the above example is:
<P> <P:first-line> The first line of an </P:first-line> article in Newsweek. </P>
The :first-line end tag is inserted at the end of the first line as formatted on the canvas.
The :first-line pseudo-element can only be attached to a block-level element.
The :first-line pseudo-element is similar to an inline element, but with certain restrictions. Only the following properties apply to a :first-line element: font properties, color properties, background properties, 'word-spacing', 'letter-spacing', 'text-decoration', 'vertical-align', 'text-transform', 'line-height', and 'clear',
The :first-letter pseudo-element is used for "initial caps" and "drop caps", which are common typographical effects. It is similar to an inline element if its 'float' property is 'none', otherwise it is similar to a floating element.
These are the properties that apply to :first-letter pseudo-elements: font properties, color properties, background properties, 'text-decoration', 'vertical-align' (only if 'float' is 'none'), 'text-transform', 'line-height', margin properties, padding properties, border properties, 'float', and 'clear'.
The following CSS2 will make a dropcap initial letter span two lines:
<HTML>
<HEAD>
<TITLE>Title</TITLE>
<STYLE type="text/css">
P { font-size: 12pt; line-height: 12pt }
P:first-letter { font-size: 200%; float: left }
SPAN { text-transform: uppercase }
</STYLE>
</HEAD>
<BODY>
<P><SPAN>The first</SPAN> few words of an article in The Economist.</P>
</BODY>
</HTML>
If a text-based UA supports the :first-letter pseudo-element the above could be formatted as:
___ | HE FIRST few | words of an article in the Economist.
The fictional tag sequence is:
<P> <SPAN> <P:first-letter> T </P:first-letter>he first </SPAN> few words of an article in the Economist. </P>
Note that the :first-letter pseudo-element tags abut the content (i.e., the initial character), while the :first-line pseudo-element start tag is inserted right after the start tag of the element to which it is attached.
The UA defines what characters are inside the :first-letter element. Quotes that precede the first letter should be included, as in:
|| /\ bird in
/ \ the hand
/----\ is worth
/ \ two in
the bush," says an
old proverb.
When the paragraph starts with other punctuation (e.g., parenthesis and ellipsis points) or other characters that are normally not considered letters (e.g., digits and mathematical symbols), :first-letter pseudo-elements are usually ignored.
The :first-letter pseudo-element can only be attached to a block-level element.
Note. Some languages may have specific rules about how to treat certain letter combinations. In Dutch, for example, if the letter combination "ij" appears at the beginning of a word, they should both be considered within the :first-letter pseudo-element.
user agents commonly display unvisited HTML links differently from previously visited ones. In CSS2, authors may refer to three categories of links: visited, unvisited, and a currently selected link (e.g., by the mouse). Each category of link has a corresponding pseudo-class:
A:link { color: red } /* unvisited link */
A:visited { color: blue } /* visited links */
A:active { color: lime } /* active links */
user agents are not required to reformat a currently displayed document due to anchor pseudo-class transitions. For instance, a style sheet may legally specify that the 'font-size' of an 'active' link should be larger that a 'visited' link, but the UA is not required to dynamically reformat the document when the reader selects the 'visited' link.
In CSS2, anchor pseudo-classes have no effect on elements other than A. Therefore, the element type can be omitted from the selector.
Note. user agents may choose to move an element from 'visited' to 'link' after a certain time.
The following two CSS2 declarations are equivalent and select the same HTML elements:
A:link { color: red }
:link { color: red }
[HTML40] defines two attributes -- "class" and "id" -- that may be used as CSS2 selectors in an HTML document. These are called attribute selectors.
The HTML "class" attribute allows authors to group elements together and specify style information to the entire group.
To add class information to a selector, the selector must be followed by a period (".") and then the class name, with no intervening white space. The selector may be empty, which means "any element".
For example, we can assign style information to all elements belonging to the class 'pastoral':
.pastoral { color: green } /* all elements with class=pastoral */
or to certain types of elements belonging to the class 'pastoral' (here, H1):
H1.pastoral { color: green } /* H1 elements with class=pastoral */
Given these rules, the first H1 instance below would not have green text, while the second would:
<H1>Not green</H1> <H1 class="pastoral">Very green</H1>
The normal inheritance rules apply to classed elements; they inherit values from their parent in the document structure.
Only one class can be specified per selector.
ILLEGAL EXAMPLE:
The following selector is illegal in CSS2 since it contains two classes.
P.pastoral.marine { color: green }
Note. CSS gives so much power to the "class" attribute, that in many cases it doesn't even matter what HTML element the class is set on -- you can make any element emulate almost any other. Relying on this power is not recommended, since it removes the level of structure that has a universal meaning (HTML elements). A structure based on "class" is only useful within a restricted domain, where the meaning of a class has been mutually agreed upon.
The HTML "id" attribute allows authors to uniquely identify an element and specify style information for that element only.
To add id information to a selector, the selector must be followed by a period ("#") and then the id name, with no intervening white space. The selector may be empty, which means "any element".
<HEAD>
<STYLE>
#z98y { letter-spacing: 0.3em }
H1#z98y { letter-spacing: 0.5em }
</STYLE>
</HEAD>
<BODY>
...body...
<P id=z98y>Wide text</P>
...body...
</BODY>
In the above example, the first selector matches the P element due to the "id" attribute value. The second selector specifies both an element type (H1) and an "id" value, and will therefore not match the P element.
Note. While style sheets have been designed to augment document structure, this feature will allow authors to create documents that present well on the canvas without taking advantage of the structural elements of HTML. This use of style sheets is discouraged.
Pseudo-classes can be combined with normal classes. In this case, the class name precedes the pseudo-class name in the selector.
In the following example, if the link in the above example has been visited, it will be rendered in blue.
A.external:visited { color: blue }
<A class="external" href="http://out.side/">external link</A>
Pseudo-elements can also be combined with attribute selectors:
P.initial:first-letter { color: red }
<P class="initial">First paragraph</A>
The above example would make the first letter of all P elements with "class=initial" red.
When combined with classes or pseudo-classes, pseudo-elements must be specified at the end of the selector.
CSS syntax allows the following rules to co-exist:
A:link { color: red } /* The :link pseudo-class */
A.link { color: green } /* In HTML, class=link */
A#link { color: blue } /* In HTML, id=link */
Since a link may have class="link", id="link", and belong to the pseudo-class :link simultaneously (i.e., be unvisited), user agents must resolve the colliding rules. User agents must do so according to the cascading order defined in the section on the cascade.