Book a free demo

Web Developer Path ยท lesson 10 of 18

Combinators, pseudo-classes and pseudo-elements

Pick exactly the elements you want, style them when someone points at them, and add decoration with no extra HTML.

What you will learn

  • combinators: descendant (space), child (>) and adjacent sibling (+)
  • attribute selectors such as input[type="text"]
  • pseudo-classes: :hover, :focus, :active, :first-child
  • pseudo-elements: ::before, ::after, ::first-letter

Understand the idea

Combinators join selectors. A space is the descendant combinator: .menu a is every link anywhere inside .menu. > is the child combinator: .menu > li is only the direct children. + picks the element right after another: h2 + p is the paragraph just after an h2.

An attribute selector matches an element by an attribute: input[type="text"] is only text boxes and a[href^="https"] is links whose address starts with https.

A pseudo-class is a state, written with one colon. :hover is when the pointer is over the element, :focus is when it has the keyboard, :active is while it is being pressed, and :first-child is an element that comes first inside its parent. A pseudo-element is a part of an element, written with two colons. ::before and ::after insert generated content before and after an element (they need a content property), and ::first-letter is its first letter. These tricks add decoration without adding any HTML.

Example

Selectors that need no extra classes
.menu a:hover {
  color: crimson;
}

.menu li:first-child {
  font-weight: bold;
}

h2::before {
  content: "โ˜… ";
  color: gold;
}

input[type="text"] {
  border: 2px solid teal;
}

Point at a menu link in the preview to see :hover work. The star is added by CSS, not by HTML.

Your turn: code to type and practise

Write four rules with exactly these selectors: .menu a:hover (set a color), .menu li:first-child (set font-weight), h2::before (set content) and input[type="text"] (set a border).

Stretch challenge

Use h2 + p to make the paragraph after the heading bigger, and .menu > li:last-child to remove its border.

โ† Fonts and text stylingThe box model โ†’

CSS

More in this module

Keep going in Web Developer Path

Practise the idea

๐Ÿž Play Bug Hunter

A Python program prints the wrong answer. Find the one line with the bug, then see the fix and why it was wrong.

Play now

FAQ

Combinators, pseudo-classes and pseudo-elements: common questions

What is the difference between a pseudo-class and a pseudo-element?

A pseudo-class (one colon, like :hover) selects an element in a certain state. A pseudo-element (two colons, like ::before) selects or creates a part of an element.

Why does ::before need a content property?

The generated box only appears if it has content. Use content: "" for an empty decorative box.

What does the child combinator > do?

It selects only direct children, not deeper descendants. .menu > li matches the list items directly inside .menu.

I am stuck. What can I do?

Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.

Does my work get saved?

Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.

Start a conversation

Enjoying coding? Meet a teacher.

Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try