← Teacher guide

Rules and selectors

Students write CSS rules in a style block, find out that a tag selector styles every tag of that kind, and that a rule changes how a part looks, not what it is.

Structure and style a page · Text and style · about 50 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 10 min · Investigate 10 min · Modify 12 min · Mix it up 5 min · Make 13 min

Before class

Read the unit primer "HTML structure in 5 minutes" (the CSS section) first, then do the Make task yourself.

New words: a rule is a selector followed by property lines in braces, p { color: teal; }. The selector (p) names a kind of tag; each line is a property, a colon, a value and a semicolon. Colours here are names such as teal and crimson. The <style> block sits at the top of the code for now; where it goes in a whole page comes later.

Link to the three layers idea from Digital systems: the words are content, the tags are structure, the rules are presentation.

Running the lesson

  1. Predict: some students think p { } styles only the first paragraph, or that it needs to say which one. A rule names a kind of tag and changes every one. Only the two paragraphs turn teal. Ask a pair that said 1 what in the rule picks the first paragraph, and see if they can find it.
  2. Run: changing the selector from p to h2 moves the colour. That is the whole idea of a selector.
  3. Investigate: the trace adds a third paragraph. No CSS changes, and the new line is teal. The second question separates look from meaning: a small <h1> is still a heading.
  4. Modify: four small changes. Each property line ends with a semicolon; a missing one makes the rule do less than expected, with no error.
  5. Mix it up: the crossed tags need fixing first, then one strong rule colours the important words.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Believing a rule picks one element. Ask which tag the selector names, and how many of that tag are on the page, before you say.

When students get stuck

What you will seeA question to askThe fix
A rule changes nothing, and there is no error.Does the selector match a tag in the HTML, and is the rule inside the style block?Write the selector as the tag name without angle brackets, as in p, and put every rule between <style> and </style>.
Only the first property in a rule works.Does every property line end with a semicolon?Add the missing ; after the value, as in color: teal;
The CSS shows on the page as text.Is the CSS inside <style> and </style>?Put every rule between <style> and </style> at the top of the page.

Answers to the checks

  1. Predict: How many lines of text turn teal?
    2: both paragraphs. The rule names the tag p, not one paragraph, so every p changes. The heading is not a p, so it stays black.
  2. Three paragraphs: A third paragraph is added to the page. Fill in the colour of each line of text.
    black, teal, teal
    The rule picks every p, including one added later, so all three paragraphs are teal. The h1 and h2 are different tags, so the rule leaves them alone.
  3. Small but still a heading: The rule h1 { font-size: 14px; } makes the heading smaller than the paragraphs. Is it still a heading?
    Yes: the <h1> tag still says it is the main heading; the rule changed only how it looks
    The tag says what a part is, and the rule says how it looks. Making an <h1> small does not turn it into a paragraph, and screen readers and search engines still treat it as the main heading.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify › Change a colour

<style>
  h1 {
    color: darkgreen;
  }
</style>
<h1>Swimming carnival</h1>
<h2>When</h2>
<p>The carnival is on Thursday.</p>
<h2>What to bring</h2>
<p>Bring a towel and a hat.</p>

Modify › Bigger paragraphs

<style>
  h1 {
    color: darkgreen;
  }
  p {
    font-size: 20px;
  }
</style>
<h1>Swimming carnival</h1>
<h2>When</h2>
<p>The carnival is on Thursday.</p>
<h2>What to bring</h2>
<p>Bring a towel and a hat.</p>

Modify › A rule for the h2

<style>
  h1 {
    color: darkgreen;
  }
  p {
    font-size: 20px;
  }
  h2 {
    background-color: lightyellow;
    text-align: center;
  }
</style>
<h1>Swimming carnival</h1>
<h2>When</h2>
<p>The carnival is on Thursday.</p>
<h2>What to bring</h2>
<p>Bring a towel and a hat.</p>

Modify › Another font

<style>
  h1 {
    color: darkgreen;
  }
  p {
    font-size: 20px;
    font-family: sans-serif;
  }
  h2 {
    background-color: lightyellow;
    text-align: center;
  }
</style>
<h1>Swimming carnival</h1>
<h2>When</h2>
<p>The carnival is on Thursday.</p>
<h2>What to bring</h2>
<p>Bring a towel and a hat.</p>

Make

<style>
  h1 {
    color: purple;
    font-size: 44px;
  }
  h2 {
    color: teal;
    background-color: lightyellow;
  }
  p {
    font-family: sans-serif;
  }
</style>
<h1>Spring concert</h1>
<h2>When and where</h2>
<p>The concert is on Wednesday at 6 pm in the hall.</p>
<h2>Who is playing</h2>
<p>The Year 7 band, the choir and the string group.</p>
<p>Tickets are free. Bring a friend!</p>

Stretch and support

Stretch: Try font-size: 12px on the h1 and 60px on the p. Then ask whether the h1 is still a heading, and how a screen reader would know.

Support: Give students the Modify starter and do the second task together on the board: the selector, the braces, then one property line.

Exit ticket

A rule h2 { color: purple; } is added to a page with four h2 headings. How many turn purple, and is any <h2> changed into something else?

All four turn purple, because the selector names the tag. None changes into something else: the tag is still h2, and the rule only changes how it looks.

What each step is for

Where it leads

Next lesson is lists: a rule for li styles every item at once. Later, classes let you pick only some tags of a kind. Class idea: restyle the same page three ways (calm, loud, sporty) without changing the HTML.