Skip to content
Codewilds

Lesson 26 of 29 · Polish & Interaction

CSS Pseudo-Classes: :hover, :focus and :nth-child

medium20 XP2 min read

So far your selectors have picked elements by their tag, class or id. Pseudo-classes select elements by their state, like hovered or focused, or by their position, like the first child or every second row, without adding a single class to the HTML. They start with a colon: a:hover, li:first-child.

:hover and :focus

:hover matches an element while the mouse pointer is over it. It's how buttons and links react when you point at them:

.btn {
  background: #f5b301;
}
.btn:hover {
  background: #ffc935; /* a little lighter while hovered */
}

:focus matches the element that has keyboard focus: the input you're typing into, or the link you reached with the Tab key. Many people navigate with a keyboard, so a focused element needs a clearly visible style:

input:focus {
  border-color: #2563eb;
  outline: 3px solid #93c5fd;
}

Never remove the focus outline (outline: none) without adding a replacement, or keyboard users can't tell where they are on the page. A related pseudo-class, :focus-visible, only matches when the browser thinks the focus should be shown, for example after pressing Tab but not after a mouse click.

First, last and nth child

Structural pseudo-classes select an element by its position among its siblings:

  • :first-child is the first child of its parent, and :last-child the last one.
  • :nth-child(3) is the third child. CSS counts from 1, not from 0.
  • :nth-child(odd) and :nth-child(even) select every other child.
  • :nth-child(3n) selects every third child: the 3rd, 6th, 9th and so on.

Zebra stripes make long tables much easier to read:

tbody tr:nth-child(even) {
  background: #fdf0d5;
}

Read li:first-child as "an li that is the first child of its parent", not "the first child inside an li". The pseudo-class describes the element it's attached to.

Pseudo-classes work with any selector. .card:hover h2, for example, styles the heading inside a card while the card is hovered.

Common mistakes

  • A space before the colon. .menu a :hover means "any hovered element inside a link". Write .menu a:hover.
  • Putting :first-child on the parent. .menu:first-child selects a .menu that is the first child of its parent. To select the first link, write .menu a:first-child.
  • Removing focus outlines. outline: none with no replacement makes the page hard to use with a keyboard.
  • Counting from zero. :nth-child(1) is the first element, so :nth-child(2) is the second.

Assignment

The page has a menu, a sign-up form and a leaderboard table. Style them with pseudo-classes, without changing the HTML:

  1. .menu a:hover: hovered menu links get the background colour #f5b301 and the text colour #2b2620.
  2. .signup input:focus: the focused input gets an outline of 3px solid #2563eb.
  3. The first menu link (:first-child) is bold (font-weight: bold).
  4. The last menu link (:last-child) has no right border (border-right: none). The starter gives every link a right border as a divider.
  5. Even rows in the table body (tbody tr:nth-child(even)) get the background colour #fdf0d5.

Point at the menu and click into the input in the Preview to try your hover and focus styles.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+20 XP

Lesson 26 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.