← Teacher guide

Style the links in the menu

Students pick only the links inside a nav with a descendant selector, and find out that a colour on the nav does not reach the links, and that a space means inside.

Layout and components · Rules that combine · about 40 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 12 min · Modify 8 min · Make 10 min

Before class

Read the unit primer "Layout and components" first, then do the Make task yourself in the playground (about 5 minutes).

A descendant selector is two selectors with a space between: nav a picks every a anywhere inside a nav. A link keeps its own colour until a rule names it, which is why nav { color: white; } leaves the menu blue. :hover is shown as a line to copy for now, not taught: it means "while the pointer is over it".

Running the lesson

  1. Predict and Run: most students say white. Blue on navy is hard to read, which is the reason menus need their own rule. Add the nav a rule and let them see the link in the paragraph stay blue. Ask someone who chose white why the header's colour would reach the links. The idea to land: a link has its own colour.
  2. Investigate: the trace has four links and one rule. The question about the comma is last lesson's group selector, read against the new one: comma means and, space means inside.
  3. Modify and Make: the same shape for the footer. The Make check ticks on a header rule, a background colour and the nav a rule. Look at the page for the paragraph link staying blue.

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)

Reading nav a as both. Ask which of the two words names the element that changes, and what the first word does.

When students get stuck

What you will seeA question to askThe fix
The menu links do not change.Does the rule name the links, or only the nav?Write nav a { color: white; } with a space between nav and a. A rule on the nav alone leaves the links blue.
Every link on the page changes.Is there a comma between nav and a, or only the a?Use a space, not a comma, and keep the nav in front of the a.
The menu is dark text on the dark header.Which rule sets the link colour?Add color: white; to the nav a rule, or pick a light colour such as lightyellow.

Answers to the checks

  1. Predict: The header is navy and its text colour is white. What colour are the two menu links?
    Blue. The white reaches the heading, but a link keeps its own colour until a rule names the link. Blue on navy is hard to read.
  2. Which links change?: Fill in the colour of each link. The browser's usual link colour is blue.
    crimson, crimson, blue, blue
    nav a picks only the links inside the nav. The paragraph and the footer are not in the nav, so their links keep the usual blue. The space means inside, not and.
  3. The selector for the menu: Which rule styles only the links inside the nav?
    nav a { }
    A space means inside, so nav a is the links within a nav. A comma, as in nav, a, means and: it styles the nav and every link on the page. a nav is the other way round: a nav inside a link.
  4. White links that stay blue: Sam wants white menu links on the navy header. The links are still blue, and there is no error. Click the line that causes it.
    line 5
    Line 5 picks the nav, not its links. The colour reaches the nav's own words, but each link keeps its blue. The selector needs to name the links: nav a.

Solutions

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

Modify › No underline in the menu

<style>
  header {
    background-color: navy;
    color: white;
  }
  nav a {
    color: white;
    text-decoration: none;
  }
</style>
<header>
  <h1>Owls club</h1>
  <nav>
    <a href="https://example.com/home">Home</a>
    <a href="https://example.com/results">Results</a>
  </nav>
</header>
<p>Read the <a href="https://example.com/results">results</a> from last week.</p>
<footer>
  <a href="https://example.com/contact">Contact us</a>
</footer>

Modify › Footer links

<style>
  header {
    background-color: navy;
    color: white;
  }
  nav a {
    color: white;
    text-decoration: none;
  }
  footer a {
    color: gray;
  }
</style>
<header>
  <h1>Owls club</h1>
  <nav>
    <a href="https://example.com/home">Home</a>
    <a href="https://example.com/results">Results</a>
  </nav>
</header>
<p>Read the <a href="https://example.com/results">results</a> from last week.</p>
<footer>
  <a href="https://example.com/contact">Contact us</a>
</footer>

Make

<style>
  header {
    background-color: navy;
    color: white;
  }
  nav a {
    color: lightyellow;
    text-decoration: none;
  }
</style>
<header>
  <h1>Owls club</h1>
  <nav>
    <a href="https://example.com/home">Home</a>
    <a href="https://example.com/results">Results</a>
    <a href="https://example.com/join">Join</a>
  </nav>
</header>
<p>Read the <a href="https://example.com/results">results</a> from last week.</p>

Stretch and support

Stretch: Use nav a:hover with a background-color to make a link light up when the pointer is over it. Copy the line from the Run table and change it.

Support: Give students the Modify starter and do the first task together on the board: the part first, then a space, then the tag.

Exit ticket

A page has a link in the nav and a link in a paragraph. Which does nav a { color: white; } change?

Only the link in the nav. The space means inside, so the link in the paragraph is not picked.

What each step is for

Where it leads

Next lesson looks at why some elements start a new line and others sit in a line, which a menu needs to run across the page. Class idea: find three menus on websites you use and say what colour their links are.