Skip to content

CSS Pseudo Classes

Learn the basics of CSS Pseudo Classes

Pseudo classes are predefined keywords that are used to select an element based on its state, or to target a specific child.

They start with a single colon :.

They can be used as part of a selector, and they are very useful to style active or visited links for example, change the style on hover, focus, or target the first child, or odd rows. Very handy in many cases.

These are the most popular pseudo classes you will likely use:

Pseudo classTargets
:activean element being activated by the user (e.g. clicked). Mostly used on links or buttons
:checkeda checkbox, option or radio input types that are enabled
:defaultthe default in a set of choices (like, option in a select or radio buttons)
:disabledan element disabled
:emptyan element with no children
:enabledan element that’s enabled (opposite to :disabled)
:first-childthe first child of a group of siblings
:focusthe element with focus
:hoveran element hovered with the mouse
:last-childthe last child of a group of siblings
:linka link that’s not been visited
:not()any element not matching the selector passed. E.g. :not(span)
:nth-child()an element matching the specified position
:nth-last-child()an element matching the specific position, starting from the end
:only-childan element without any siblings
:requireda form element with the required attribute set
:rootrepresents the html element. It’s like targeting html, but it’s more specific. Useful in CSS Variables.
:targetthe element matching the current URL fragment (for inner navigation in the page)
:validform elements that validated client-side successfully
:visiteda link that’s been visited

Let’s do an example. A common one, actually. You want to style a link, so you create a CSS rule to target the a element:

a {
  color: yellow;
}

Things seem to work fine, until you click one link. The link goes back to the predefined color (blue) when you click it. Then when you open the link and go back to the page, now the link is blue.

Why does that happen?

Because the link when clicked changes state, and goes in the :active state. And when it’s been visited, it is in the :visited state. Forever, until the user clears the browsing history.

So, to correctly make the link yellow across all states, you need to write

a,
a:visited,
a:active {
  color: yellow;
}

:nth-child() deserves a special mention. It can be used to target odd or even children with :nth-child(odd) and :nth-child(even).

It is commonly used in lists to color odd lines differently from even lines:

ul:nth-child(odd) {
  color: white;
	background-color: black;
}

You can also use it to target the first 3 children of an element with :nth-child(-n+3). Or you can style 1 in every 5 elements with :nth-child(5n).

Some pseudo classes are just used for printing, like :first, :left, :right, so you can target the first page, all the left pages, and all the right pages, which are usually styled slightly differently.


→ Here's my latest YouTube video

→ Get my CSS Handbook

→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter

JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list