Pseudo-class selectors affect elements in certain states: for example, a:hover
controls the appearance of links during a momentary touch on a mobile device, or on mouse hover. In contrast, pseudo-element selectors control the appearance of portions of an element’s content, such as the first line or letter of a paragraph.
The table below covers all the current pseudo-class selectors in CSS, with links to appropriate articles on the selectors:
Pseudo-class selector | Purpose | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
:link | Controls the appearance of an unvisited link. Strongly associated with the a tag, to the point at which the a selector used by itself before a style declaration accomplishes the same thing.
| ||||||||||||||||||||
:visited | Controls the appearance of visited links. Quite a few restrictions on styles in terms of what can be applied, due to security risks. | ||||||||||||||||||||
:hover | Controls the appearance of an element while it is being hovered over with a mouse or other pointing device. While it is heavily associated with the a tag, CSS does not explicitly specify this association. In modern browsers, :hover can be applied to any arbitrary element, such as img:hover .
| ||||||||||||||||||||
:active | The appearance of content while the user is clicking on it, i.e. between the mouse button being held down and it being released. Rarely used, since the effect is usually transient. | ||||||||||||||||||||
:first-child | Selects the first child element of a markup sequence. For example, ul li:first-child would specify that the style declaration applies to the first li element in any unordered list.
| ||||||||||||||||||||
:last-child | Selects the last child element in a sequence. | ||||||||||||||||||||
:nth-child(n)
| Selects children in an element sequence, in iterations of (n) , where n is the word odd , even , a number, or a mathematical expression.
| ||||||||||||||||||||
:nth-of-type(n)
| Selects the nth appearance of a particular element. | ||||||||||||||||||||
:focus |
Note that pseudo-selectors can be strung together: a:visited:hover
, for example.
Enjoy this piece? I invite you to follow me at twitter.com/dudleystorey to learn more.