Skip to main content

CSS Pseudo-classes

CSS pseudo-classes are special selectors that allow you to target and style elements based on their state or position in the document tree.

The pseudo-classes start with a colon (:) and are used in conjunction with regular selectors to apply styles to specific elements.

Here are some commonly used CSS pseudo-classes:

The hover

Targets an element when the mouse cursor is hovering over it.

As an example:

Editor

Loading...

The active

Targets an element while it is being clicked or activated.

As an example:

Editor

Loading...

The focus

Targets an element when it has focus (e.g. when it is selected by the user or when the user tabs through the page).

As an example:

Editor

Loading...

The visited

Targets links that have already been visited by the user.

As an example:

Editor

Loading...

The nth-child()

Targets elements that are a specified child of their parent element (e.g. :nth-child(3) targets the third child element).

As an example:

Editor

Loading...

In this example:

  • We have a table with four rows of data.
  • We use the :nth-child pseudo-class to apply a background color to every even row, creating a striped effect.
  • We achieve this by selecting every second row using the even keyword and applying a new background color to the selected rows.

The first-child

Targets the first child element of its parent element.

As an example:

Editor

Loading...

The last-child

Targets the last child element of its parent element.

As an example:

Editor

Loading...

The not()

Targets elements that do not match the specified selector.

As an example:

Editor

Loading...

All Pseudo Classes

Here's a list of all the pseudo-classes:

Pseudo-classDescriptionExample
:activeSelects the active linka:active { color: red }
:checkedSelects the checked elementinput:checked { background-color: yellow }
:disabledSelects the disabled elementinput:disabled { opacity: 0.5 }
:emptySelects elements that have no childrenp:empty { display: none }
:enabledSelects the enabled elementinput:enabled { border-color: green }
:first-childSelects the first child of an elementli:first-child { font-weight: bold }
:focusSelects the element that has focusinput:focus { border-color: blue }
:hoverSelects the element that the user is hovering overa:hover { text-decoration: underline }
:langSelects elements with a specified language attributep:lang(en) { font-style: italic }
:last-childSelects the last child of an elementli:last-child { font-weight: bold }
:notSelects all elements that do not match a selector:not(p) { margin-bottom: 10px }
:nth-childSelects elements based on their position among siblingsli:nth-child(odd) { background-color: lightgray }
:nth-last-childSelects elements based on their position among siblings, counting from the last childli:nth-last-child(even) { background-color: lightgray }
:only-childSelects elements that are the only child of their parentp:only-child { font-size: 24px }
:rootSelects the root element of the document:root { --main-color: red }
:targetSelects the target element of an anchor link#section1:target { background-color: yellow }
:checkedSelects a radio button or checkbox that is checkedinput[type="checkbox"]:checked { background-color: yellow }
:defaultSelects the default button in a formbutton:default { border-color: blue }
:dir()Selects elements with a specified text directionp:dir(ltr) { text-align: left }
:dropSelects elements that are being draggeddiv:drop { border: 2px dashed gray }
:first-of-typeSelects the first element of its type among siblingsp:first-of-type { font-size: 24px }
:fullscreenSelects an element that is in full-screen modevideo:fullscreen { width: 100% }
:in-rangeSelects an input element with a value within a specified rangeinput[type="range"]:in-range { background-color: green }
:indeterminateSelects a checkbox that is in an indeterminate stateinput[type="checkbox"]:indeterminate { opacity: 0.5 }
:invalidSelects an input element with an invalid valueinput:invalid { border-color: red }
:last-of-typeSelects the last element of its type among siblingsp:last-of-type { font-size: 24px }
:linkSelects an unvisited linka:link { color: blue }
:nth-of-typeSelects elements based on their position among siblings, counting only elements of the same typep:nth-of-type(odd) { background-color: lightgray }
:only-of-typeSelects elements that are the only element of their type among siblingsh1:only-of-type { font-size: 36px }
:optionalSelects an input element that is not requiredinput:optional { border-color: gray }
:out-of-rangeSelects an input element with a value outside a specified rangeinput[type="range"]:out-of-range { background-color: red }
:requiredSelects an input element that is requiredinput:required { border-color: green }
:validSelects an input element with a valid valueinput:valid { border-color: green }
:visitedSelects a visited linka:visited { color: purple }