Loading...

CSS Pseudo-classes

CSS Attribute SelectorsCSS Pseudo-elements

Pseudo-classes let us target different states of an element. For example, when hovered, when focused, or when it is the first element within a parent.

What is a Pseudo-class?

Pseudo-classes are denoted with a colon (:), and they target states, positions, or special cases that cannot be targeted with normal attributes.

Pseudo-classDescription
:hoverApplied when the mouse hovers over the element.
:focusApplied when the element (e.g., input field) is focused.
:nth-child()Selects a child element at a specific position.
:first-childSelects the first child of the parent.
:last-childSelects the last child of the parent.

Interactive Pseudo-classes

These pseudo-classes react to interactions, such as hovering over a link or clicking into an input field.

In the following example, a link turns red on hover, while a text field gets a green border when focused.

Structural Pseudo-classes

Structural pseudo-classes target the position of an element within its parent. This makes it easy to style the first, last, or nth element.

Tips for Using Pseudo-classes

Pseudo-classes simplify state- and structure-based styling. Here are some tips:

  • Use :hover and :focus selectors for better user experience.
  • Structural pseudo-classes help avoid adding unnecessary extra classes in the HTML.
  • Avoid overly complex :nth-child() rules for better code clarity.

✨ Ask Lara

Please sign in to ask Lara about CSS pseudo-classes.

Track Your Progress 🚀

Learn more easily by tracking your progress completely for free.


Top tools

CodeHubBoardly NEWLinksy NEWChromo NEW

Select Language

Set theme

© 2025 ReadyTools. All rights reserved.