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.

Selector

Pattern

Description

Example

Universal selector

*

Matches any element name.

* { text-decoration: none; }

Type selector

C

Matches all elements named C; in this example, all elements h2.

h2 { font-weight: normal; }

Descendant selector (context selector)

C R S

Matches elements named S descended from R, which must itself descend from C.

div#content p em { background- color: yellow; }

Child selector

C > S

Matches elements named S that are direct children of C.

li > ul { list-style-type: circle; }

Adjacent sibling selector

C + S

Selects an element named S directly following an element named C.

div#content+p { text-indent: 0; }

Grouped selectors

C, R, S

Comma-separated selectors can share the same rules.

h1, h2, h3, h4 { color: #0cf; }

Class selector

C.classR

Matches elements named C whose class attribute is classR.

img.content { padding: 4px; border: 1px solid black; }

ID selector

C#idR

Matches the element named C whose attribute id has the value idR.

div#content { color: #333; }

Simple attribute selector

C[Attribut]

Matches elements named C with the specified attribute.

a[link] { text-decoration: none; }

Exact attribute value selector

C[attribute="valueR"]

Matches elements named C whose attribute equals valueR.

input[type="text"] { width: 33%; }

Partial attribute value selector

C[attribute~="valueR"]

Matches elements named C whose attribute is a space-separated list containing valueR.

div.advertisement form[class~="login"] { float: left; margin-left: 7px; }

Language attribute selector

C[attribute|="valueR"]

Mainly for language attributes. Matches elements named C whose attribute is a hyphen-separated list beginning with valueR.

warning[lang="uk"]:after { content: "Blimey!"}

  

Table: CSS 2.1 pseudo-classes.

Pseudo-class

Pattern

Description

Example

:first-child

C:first-child

Matches the first child of elements named C.

divs p:first-child { color: white; background-color: red; }

:link

C:link

Matches elements named C containing an unvisited link.

a:link { text-decoration: none; }

:visited

C:visited

Matches elements named C containing a visited link.

a:visited { font-weight: normal; }

:hover

C:hover

Matches elements named C currently selected, typically under the pointer, but not activated.

a:hover { background-color: orange; }

:active

C:active

Matches activated elements named C, such as a link being clicked.

a:active { color: green; }

:focus

C:focus

Matches the focused element named C, typically a form field.

input:focus { background-color: #F7F7D5; }

:lang

C:lang(R)

Matches elements named C whose lang attribute equals R.

p:lang(en) { font-weight: bold; }

  

Table: CSS 2.1 pseudo-elements.

Pseudo-element

Pattern

Description

Example

:first-line

C:first-line

Matches the first line of the element named C.

h2+p:first-line { color: #727977; }

:first-letter

C:first-letter

Matches the first letter of the element named C.

h1:first-letter { font-size: 66%; text-transform: lowercase; }

:before

C:before

Inserts generated content before the element named C; used with the property content.

ul.tracklisting li:before { content: "Titel des Stücks"; }

:after

C:after

Inserts generated content after the element named C; used with the property content.

div#footer p.copyright:after { content: "Sehr wahr!"; }

  

 

 

 

back to top