Pseudo-Classes vs. Pseudo-Elements

March 03, 2014 | 16 Minute Read

For the longest time the only pseudo selectors that we could use were :link, :hover, :visited and :active and they could only be applied to a tags. As long as we were intending to support ie6 that was all we could do. As we've adopted philosophies like progressive enhancement, graceful degradation, or dropped support for ie6, 7, and 8 we have seen the landscape of CSS selectors change dramatically.

As I have began using more pseudo selectors I’ve found myself using the terms pseudo-classes and pseudo-elements interchangeably, which I’ve known to be wrong. In order to correct this I’ve decided to explore the differences between the two.

Pseudo-Classes

A pseudo-class is a keyword that can be added to a selector based on a special state of the selected item. They are unique in that they can be actual elements in the DOM or they can be based on states that are triggered when certain actions occur such as :hover, :visited, :checked, etc.

How They Are Used

li {
    border-top: solid 1px gray;
}

li:first-child {
    border: none;
}

:active

Matches when an element is being activated by the user — Learn More

ie4 and above Chrome 1 and above Fire Fox 1 and above Safari 1 and above

:checked

Represents any radio, checkbox, or option in a select element that is checked or toggled to an on state — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:default

Represents any user interface element that is the default among a group of similar elements — Learn More

Chrome 10 and above Fire Fox 4 and above Safari 5 and above

:dir()

Matches elements based on the directionality of the text contained in it — Learn More

Fire Fox 4 and above

:disabled

Represents any disabled element. An element is disabled if it can’t be activated (e.g. selected, clicked on or accept text input) or accept focus — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:empty

Represents any element that has no children at all. Only element nodes and text (including whitespace) are considered — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:enabled

Represents any enabled element. An element is enabled if it can be activated (e.g. selected, clicked on or accept text input) or accept focus — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:first

Describes the styling of the first page when printing a document — Learn More

ie8 and above

:first-child

Represents any element that is the first child element of its parent — Learn More

ie7 and above Chrome 4 and above Fire Fox 3 and above Safari 4 and above

:first-of-type

Represents the first sibling of its type in the list of children of its parent element — Learn More

ie9 and above Chrome 1 and above Fire Fox 3.5 and above Safari 3.2 and above

:fullscreen

Applies to any element that’s currently being displayed in full-screen mode — Learn More

ie11 and above Chrome 15 and above Fire Fox 9 and above Safari 6 and above

:focus

Applied when a element has received focus, either from the user selecting it with the use of a keyboard or by activating with the mouse (e.g. a form input) — Learn More

ie8 and above Chrome 1 and above Fire Fox 1 and above Safari 1 and above

:hover

Matches when the user designates an element with a pointing device, but does not necessarily activate it — Learn More

ie7 and above Chrome 0.2 and above Fire Fox 1 and above Safari 2.0.4 and above

:indeterminate

Represents any input type=“checkbox” element whose indeterminate DOM property is set to true by JavaScript — Learn More

ie9 and above Chrome 6 and above Fire Fox 3.6 and above Safari 3 and above

:invalid

Represents any input or form element whose content fails to validate according to the input’s type setting — Learn More

ie10 and above Chrome 10 and above Fire Fox 4 and above Safari 5 and above

:lang()

Matches elements based on the language the element is determined to be in — Learn More

ie8 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:last-child

Represents any element that is the last child element of its parent — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.2 and above

:last-of-type

Represents the last sibling of its type in the list of children of its parent element — Learn More

ie9 and above Chrome 1 and above Fire Fox 3.5 and above Safari 3.2 and above

:left

Matches any left page when printing a page. It allows to describe the styling of left-side pages — Learn More

ie8 and above

Lets you select links inside elements — Learn More

Safari 1 and above

:not()

A functional notation taking a simple selector as an argument. It matches an element that is not represented by the argument. Must not contain another negation selector, or any pseudo-elements — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.2 and above

:nth-child

Matches an element that has siblings before it in the document tree, for a given positive or zero value, and has a parent element — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:nth-last-child

Matches elements that have siblings after them in the document tree, for a given positive or zero value, and have a parent element — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 3.1 and above

:nth-last-of-type

Matches the last element that has siblings with the same element name after it in the document tree, for a given positive or zero value, and has a parent element — Learn More

ie9 and above Chrome 4 and above Fire Fox 3.5 and above Safari 3.2 and above

:nth-of-type

Matches an element that has siblings with the same element name before it in the document tree, for a given positive or zero value, and has a parent element — Learn More

ie9 and above Chrome 1 and above Fire Fox 3.5 and above Safari 3.1 and above

:only-child

Represents any element which is the only child of its parent — Learn More

ie9 and above Chrome 2 and above Fire Fox 1.5 and above Safari 3.1 and above

:only-of-type

Represents any element that has no siblings of the given type — Learn More

ie9 and above Chrome 1 and above Fire Fox 3.5 and above Safari 3.2 and above

:optional

Represents any input element that does not have the required attribute set on it — Learn More

ie10 and above Chrome 10 and above Fire Fox 4 and above Safari 5 and above

:read-write

Matches when an element is editable by user like text input element — Learn More

ie9 and above Supported in Chrome Supported in Fire Fox Supported in Safari

:required

Represents any input element that has the required attribute set on it — Learn More

ie10 and above Chrome 10 and above Fire Fox 4 and above Safari 5 and above

Matches any right page when printing a page. It allows to describe the styling of right-side page — Learn More

ie8 and above

:root

Matches the root element of a tree representing the document — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 1 and above

:scope

Matches the elements that are a reference point for selectors to match against — Learn More

Chrome 20 and above Fire Fox 21 and above Safari 7 and above

:target

Represents the unique element, if any, with an id matching the fragment identifier of the URI of the document — Learn More

ie9 and above Chrome 1 and above Fire Fox 1.3 and above Safari 1.3 and above

:valid

Represents any input element whose content validates correctly according to the input’s type setting — Learn More

ie10 and above Chrome 10 and above Fire Fox 4 and above Safari 5 and above

:visited

Lets you select only links that have been visited — Learn More

ie 3.5 and above Chrome 1 and above Fire Fox 1 and above Safari 1 and above

Pseudo-Elements

Like a pseudo-class, a pseudo-element is a keyword that can be added to a selector. Where they differ is that a pseudo-class does not apply to a special state but rather a portion of html that doesn’t exist in the DOM like :before or is not contained within a tag like :first-letter.

Another difference is that as part of the CSS3 spec, in an attempt to differentiate pseudo-classes from pseudo selectors, use of double colons(::) was allowed. Most browsers support both single and double colons for pseudo-elements.

How They Are Used

li::after {
    clear: both;
}

::after

Matches a virtual last child of the selected element. Typically used to add cosmetic content to an element, by using the content CSS property — Learn More

ie8 and above Supported in Chrome Fire Fox 1 and above Safari 4 and above

::before

Creates a pseudo-element that is the first child of the element matched. Typically used to add cosmetic content to an element, by using the content CSS property — Learn More

ie8 and above Supported in Chrome Fire Fox 1 and above Safari 4 and above

::first-letter

Selects the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 1 and above

::first-line

Applies styles only to the first line of an element — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 1 and above

::selection

Applies rules to the portion of a document that has been highlighted (e.g., selected with the mouse or another pointing device) by the user — Learn More

ie9 and above Chrome 1 and above Fire Fox 1 and above Safari 1.1 and above

Sources