CSS Pseudo-classes

A pseudo-class is used to define a special state of an element.

For example, it can be used to:

  • Style an element when a user mouses over it

  • Style visited and unvisited links differently

  • Style an element when it gets focus

The syntax of pseudo-classes:

selector:pseudo-class {
  property: value;
}

CSS Pseudo Classes

Selector
Example
Example description

a:active

Selects the active link

input:checked

Selects every checked <input> element

input:disabled

Selects every disabled <input> element

p:empty

Selects every <p> element that has no children

input:enabled

Selects every enabled <input> element

p:first-child

Selects every <p> elements that is the first child of its parent

p:first-of-type

Selects every <p> element that is the first <p> element of its parent

input:focus

Selects the <input> element that has focus

a:hover

Selects links on mouse over

input:in-range

Selects <input> elements with a value within a specified range

input:invalid

Selects all <input> elements with an invalid value

p:lang(it)

Selects every <p> element with a lang attribute value starting with "it"

p:last-child

Selects every <p> elements that is the last child of its parent

p:last-of-type

Selects every <p> element that is the last <p> element of its parent

a:link

Selects all unvisited links

:not(p)

Selects every element that is not a <p> element

p:nth-child(2)

Selects every <p> element that is the second child of its parent

p:nth-last-child(2)

Selects every <p> element that is the second child of its parent, counting from the last child

p:nth-last-of-type(2)

Selects every <p> element that is the second <p> element of its parent, counting from the last child

p:nth-of-type(2)

Selects every <p> element that is the second <p> element of its parent

p:only-of-type

Selects every <p> element that is the only <p> element of its parent

p:only-child

Selects every <p> element that is the only child of its parent

input:optional

Selects <input> elements with no "required" attribute

input:out-of-range

Selects <input> elements with a value outside a specified range

input:read-only

Selects <input> elements with a "readonly" attribute specified

input:read-write

Selects <input> elements with no "readonly" attribute

input:required

Selects <input> elements with a "required" attribute specified

root

Selects the document's root element

#news:target

Selects the current active #news element (clicked on a URL containing that anchor name)

input:valid

Selects all <input> elements with a valid value

a:visited

Selects all visited links

Last updated