Skip to content
BENIBIX Learn

CSS Pseudo-Class

Development🌐 Web DevelopmentBrowser Code

Also known as Pseudo-Class, :hover, :focus, Pseudo Class Selector

A bit of a CSS rule that picks a thing by its state. :hover means the mouse is on it.

Think of it like

It is like a light that comes on when someone stands at the door.

Example

Lucy wants her buttons to turn dark when the mouse is on them. She adds one rule with button:hover, and no JavaScript.

Why it matters

It lets a page answer the reader with no script at all. Clear focus styles also help people who use a keyboard.

Definition

A CSS pseudo-class picks an element by its state or place, not by its name. It starts with one colon. :hover matches while the mouse is over the element, and :focus matches the box being typed in. :first-child matches the first item in a list. A pseudo-class counts like a class when the browser scores which rule wins.

Example code

css
button:hover { background: navy; }
input:focus { outline: 2px solid teal; }
The button goes dark under the mouse. The box being typed in gets a teal ring.
a:hover scores like one class plus one tag, 0-1-1.

Common mistake

Mixing it up with a pseudo-element, like ::before. A pseudo-class has one colon and picks a state; two colons make a new part.

Key takeaways

  1. 1
    One colon. :hover, :focus, :first-child.
  2. 2
    State or place. It picks by what is happening.
  3. 3
    Scores as a class. It adds 0-1-0 to the score.

Learn it properly