Find out that an element takes every rule that matches it, and that a class beats a tag. A comma list shares one rule, as a handy extra.
A real stylesheet has hundreds of rules, and the browser settles every clash between them the same way you are about to learn.
A page can have many rules, and one element can match more than one of them. You already know that p { } styles every paragraph and .note { } styles the ones marked note. What happens to a paragraph that is both?
Make your prediction first. Help opens once you've locked it in.
<style>
.note {
color: crimson;
font-size: 24px;
}
p {
color: teal;
}
</style>
<h1>Sports day</h1>
<p>Sports day is on Friday.</p>
<p class="note">Bring a hat.</p>
Read the code. Bring a hat. is a p and it has class="note", so both rules match it. What will it look like?
Compare the page with your prediction. Then move the .note rule below the p rule: nothing changes. Last, change p { to h1, p { and watch the heading turn teal too.
| Code | What it does | Why |
|---|---|---|
.note { color: crimson; font-size: 24px; } |
Styles every element marked note |
The colour and the size both apply to Bring a hat. |
p { color: teal; } |
Styles every paragraph | The same paragraph matches this rule too, so it takes both rules. Nothing is thrown away |
color on Bring a hat. |
Crimson, not teal | Both rules set the colour, and a class is a more exact pick than a tag, so the class wins, whichever rule is written first |
font-size on Bring a hat. |
24px | Only .note sets the size, so nothing competes with it |
| two rules equally exact | The one written later wins | If two tag rules both set color on p, the browser takes the lower one |
h1, p { color: teal; } |
One rule for the heading and the paragraphs | The comma means "and". It is the same as two rules, written once |
<style>
.note {
color: crimson;
font-size: 24px;
}
p {
color: teal;
}
</style>
<h1>Sports day</h1>
<p>Sports day is on Friday.</p>
<p class="note">Bring a hat.</p>
<style>
h1, p {
color: navy;
font-size: 18px;
}
.note {
color: crimson;
}
.big {
font-size: 30px;
}
</style>
<h1>Sports day</h1>
<p>Bring a hat.</p>
<p class="note">Bring water.</p>
<p class="note big">Races start at 9 am.</p>
Each line may match several rules.
Both rules are equally exact.
The class rule should win here, but the page stays teal.
Fill in the colour and the size of each line. Read every rule that matches it.
| line | colour | size (px) |
|---|---|---|
| the heading | ||
| Bring a hat. | ||
| Bring water. | ||
| Races start at 9 am. |
Bring a hat. should be crimson, but it is teal, and there is no error. Click the line that causes it.
The heading and the paragraphs have the same rule, written twice.
The two rules are the same, so they can share one.
List both selectors with a comma, then delete the other rule: h1, p {

Mark the second paragraph with a class and write its rule above the group rule. Is it crimson or navy?
A class needs two parts that match: a name on the paragraph, and a rule for that name.
In the HTML: <p class="alert">. In the CSS: .alert { color: crimson; }.
Write the .alert rule above the group rule, on purpose.

Join the h1 and p rules into one rule.
This ticks when your page has all of the above.
Give the second paragraph the class alert, with a crimson rule above the group rule.
This ticks when your page has all of the above.
Restyle the club notice. Write one group rule such as h1, h2, p that gives the heading and the paragraphs a colour and a font. Give the last paragraph the class signup and a .signup rule with a different colour and a bigger size.
Start with one group rule for the headings and paragraphs: colour and font in one place.
Mark the sign-up line <p class="signup"> and write a .signup rule that sets a different colour.
The .signup rule wins wherever it is written. Put it above the group rule to see it still does.

Style the club notice with one group rule and a signup class that overrides it.
This ticks when your page has all of the above.
Students find out that an element takes every rule that matches it, that a class beats a tag and a later rule beats an equal one, and that a comma shares one rule between selectors. About 40 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer "Layout and components" first, then do the Make task yourself in the playground (about 5 minutes).
Students have written rules and classes in Course A, but always one at a time. The new idea is that an element can match several rules at once. The browser keeps every property from every matching rule. Only where two rules set the same property does it pick: a class beats a tag, and between rules equally exact the later one wins. A group selector is a comma list, h1, p { }, one rule for several kinds of element.
Believing only one rule applies to an element. Ask the student to list every rule that matches the line before they say what it looks like.
Stretch: Add a second class, `big`, that sets only the font size, and put both classes on one paragraph: class="signup big". Which properties come from which class?
Support: Give students the Modify starter and ask them only to join the two rules with a comma, then mark one paragraph with a class.
Next lesson picks only the links inside a menu, with a space between two selectors. Class idea: look at the stylesheet of a real school page and find two rules that must both apply to one heading.