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
.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.
More in this module
Keep going in Web Developer Path
Adding CSS: three ways and three selectors
Colour the `h1` text, give `.intro` a `background-color`, and give `#special` both a `color` and a `background-color`.
Start โ8Backgrounds and gradients
Give `.hero` a `background-image` that uses `linear-gradient`, set its `background-size`, and give `.card` a `background-color`. Use the long-hand properties.
Start โ9Fonts and text styling
Style `h1` with a `font-family`, a `font-size`, `text-align: center` and `text-transform: uppercase`. Style `p` with a `line-height` and `letter-spacing`, and remove the underline from `a` with `text-decoration: none`.
Start โ11The box model
Style `.card`: give it a `width`, some `padding`, a `border`, a `margin` of `12px auto`, and `box-sizing: border-box`.
Start โ12Display and position
Make `.card` `position: relative`, pin `.badge` to its top-right corner with `position: absolute`, `top` and `right`, and hide `.hide-me` with `display: none`.
Start โ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.
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
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.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Python For Loops and range()Change start, stop and step to see which numbers a loop uses.Play