← Teacher guide

Classes and themes

Students switch CSS classes with classList.toggle, add and remove, hide and show a tip with a hidden class, fix a classList call written with a dot, and make a themed page of their own.

The DOM and the browser console · Change the page · 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 15 min · Modify 10 min · Make 15 min

Before class

Read the unit primer first. This ties the CSS lessons to JavaScript: the look is in <style>, the script only flips a class. classList.toggle("dark") on document.body applies a rule to the whole page. The classic slip is classList.toggle(".dark"): no error, no change. The bug task is a logic bug, so students must click the button and notice nothing happens. Hiding with a .hidden { display: none } class is the pattern behind menus, tabs and tips; make the link to "display" from the CSS course.

Running the lesson

  1. Predict: ask where the colour of the dark page is written.
  2. Run: ask students to click each button twice and say what toggle does.
  3. Investigate: for the bug, ask "is it the CSS or the script, and how can you tell?"
  4. Modify: ask which element gets the class in each case.
  5. Make: ask each student to list their classes before writing the buttons.

The hard part (on purpose)

The dot: the CSS rule is .dark but the class name is dark. The page does nothing and says nothing.

When students get stuck

What you will seeA question to askThe fix
Clicking does nothing and there is no error.Does the class name in classList have a dot?Remove the dot: toggle("dark").
The class is added but nothing changes.Does the CSS have a rule for exactly that class name?Check the spelling of the rule and the class.
Cannot read properties of null.Does the id in the selector match the HTML?Match the id, with a #.
It changes the wrong thing.Which element did you call classList on?Check the selector, and use document.body for the whole page.

Answers to the checks

  1. Predict: What happens the first time the button is clicked?
    The page goes dark. The CSS already has a .dark rule. toggle adds the class dark to the body, and the rule applies.
  2. Which part does what?: Match each piece with what it does.
    .dark { background: midnightblue; } → C. CSS: what the dark look is; document.body.classList.toggle("dark"); → D. adds dark to the body if missing, removes it if there; classList.add("dark") → A. adds the class, even if it is already there; classList.remove("dark") → B. takes the class away
  3. Why a class?: Why is hidden good for a tip that starts out of sight?
    The CSS rule .hidden { display: none; } hides it, and removing the class shows it
    The class hidden has a CSS rule that hides the element. The tip is always in the page; toggling the class shows or hides it.
  4. Spot the bug: Clicking the button does nothing, and there is no red error. Click the line that is wrong.
    line 11
    classList takes the plain class name: toggle("dark"). With the dot, it toggles a class called ".dark", which no CSS rule matches.
  5. Where this leads: A page needs to show a different message depending on what someone typed. What does the script need to read?
    The value of the input
    An input holds what the person typed in its value. Next lesson.

Solutions

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

Modify › A bigger heading

<style>
  body { font-family: system-ui, sans-serif; padding: 16px; }
  .dark { background: midnightblue; color: white; }
  .hidden { display: none; }
  .big { font-size: 48px; }
</style>
<h1 id="heading">Night mode</h1>
<button id="theme">Switch theme</button>
<button id="tip-button">Show tip</button>
<button id="size">Bigger heading</button>
<p id="tip" class="hidden">Tip: dark pages are easier on the eyes at night.</p>
<script>
  document.querySelector("#theme").addEventListener("click", function () {
    document.body.classList.toggle("dark");
  });
  document.querySelector("#tip-button").addEventListener("click", function () {
    document.querySelector("#tip").classList.toggle("hidden");
  });
  document.querySelector("#size").addEventListener("click", function () {
    document.querySelector("#heading").classList.toggle("big");
  });
</script>

Modify › Two buttons

<style>
  body { font-family: system-ui, sans-serif; padding: 16px; }
  .dark { background: midnightblue; color: white; }
  .hidden { display: none; }
  .big { font-size: 48px; }
</style>
<h1>Night mode</h1>
<button id="light">Light</button>
<button id="night">Dark</button>
<script>
  document.querySelector("#light").addEventListener("click", function () {
    document.body.classList.remove("dark");
  });
  document.querySelector("#night").addEventListener("click", function () {
    document.body.classList.add("dark");
  });
</script>

your own themes

<style>
  body { font-family: system-ui, sans-serif; padding: 16px; background: lightyellow; }
  .ocean { background: navy; color: white; }
  .spaced { letter-spacing: 4px; }
  .card { padding: 12px; border: 2px solid teal; border-radius: 12px; }
  .hidden { display: none; }
</style>
<h1 class="card">My themes</h1>
<button id="theme">Ocean theme</button>
<button id="space">Spaced letters</button>
<button id="more">Show more</button>
<p id="extra" class="hidden">Everything here is switched with classList.</p>
<script>
  document.querySelector("#theme").addEventListener("click", function () {
    document.body.classList.toggle("ocean");
  });
  document.querySelector("#space").addEventListener("click", function () {
    document.querySelector("h1").classList.toggle("spaced");
  });
  document.querySelector("#more").addEventListener("click", function () {
    document.querySelector("#extra").classList.toggle("hidden");
  });
</script>

Stretch and support

Stretch: Remember the theme with a second class for each choice, or make a button that cycles through three themes using a variable.

Support: Give the CSS classes ready-made and ask students to write only the buttons and listeners.

Exit ticket

Why does classList.toggle("dark") have no dot?

classList takes the class name, not a CSS selector: the dot is only for CSS rules and querySelector.

What each step is for

Where it leads

Next lesson: inputs, reading what someone types.