XML technologies / CSS / CSS Cookbook / CSS 2.1: selectors, pseudo-classes and pseudo-elements
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Besides knowing the CSS properties themselves, knowing how to assign styles to the right elements is just as important. These three reference tables show how.
The first table lists CSS 2.1 selectors. Selectors help browsers apply your declarations to elements.
Note:
C, R and S in the “Pattern” column represent individual selectors.
The second table lists pseudo-classes. These access elements or parts that cannot be specifically marked up, such as the different states of links (visited, hover etc.).
The third table lists pseudo-elements. Unlike pseudo-classes, these act like invisible tags around an element, enabling styling without extra markup. Their structure resembles an element more than a class, hence the name.
Table: CSS 2.1 selectors.
Pattern | Description | Example | |
|---|---|---|---|
Universal selector | * | Matches any element name. |
|
Type selector | C | Matches all elements named C; in this example, all elements |
|
Descendant selector (context selector) | C R S | Matches elements named S descended from R, which must itself descend from C. |
|
Child selector | C > S | Matches elements named S that are direct children of C. |
|
Adjacent sibling selector | C + S | Selects an element named S directly following an element named C. |
|
Grouped selectors | C, R, S | Comma-separated selectors can share the same rules. |
|
Class selector | C.classR | Matches elements named C whose class attribute is classR. |
|
ID selector | C#idR | Matches the element named C whose attribute |
|
Simple attribute selector | C[Attribut] | Matches elements named C with the specified attribute. |
|
Exact attribute value selector | C[attribute="valueR"] | Matches elements named C whose attribute equals valueR. |
|
Partial attribute value selector | C[attribute~="valueR"] | Matches elements named C whose attribute is a space-separated list containing valueR. |
|
Language attribute selector | C[attribute|="valueR"] | Mainly for language attributes. Matches elements named C whose attribute is a hyphen-separated list beginning with valueR. |
|
Table: CSS 2.1 pseudo-classes.
Pseudo-class | Pattern | Description | Example |
|---|---|---|---|
| C:first-child | Matches the first child of elements named C. |
|
| C:link | Matches elements named C containing an unvisited link. |
|
| C:visited | Matches elements named C containing a visited link. |
|
| C:hover | Matches elements named C currently selected, typically under the pointer, but not activated. |
|
| C:active | Matches activated elements named C, such as a link being clicked. |
|
| C:focus | Matches the focused element named C, typically a form field. |
|
| C:lang(R) | Matches elements named C whose lang attribute equals R. |
|
Table: CSS 2.1 pseudo-elements.
Pseudo-element | Pattern | Description | Example |
|---|---|---|---|
| C:first-line | Matches the first line of the element named C. |
|
| C:first-letter | Matches the first letter of the element named C. |
|
| C:before | Inserts generated content before the element named C; used with the property |
|
| C:after | Inserts generated content after the element named C; used with the property |
|
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln
back to top