Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Switch Styles With CSS Classes

In this video, you switched the long class off and on in the Elements panel and watched Priya’s page follow, without changing the stylesheet.

Key Points

  • One rule, #games li.long, styles whichever game in the games list carries the class long. It never names a game.
  • When you type the class, write the bare name, class="long". The dot belongs only to the selector, .long, where it says that long is a class.
  • To switch the class off in the Elements panel, double-click its value in the tree, delete it, and press Enter. Catan turns gray and loses its bold: the #games li rule still matches it, and the #games li.long rule drops out of the Styles pane.
  • To switch it back on, right-click Catan’s row in the Elements panel, choose Add attribute, type class="long", and press Enter. Any game can carry the class: do the same on the Codenames row, and Codenames turns the accent color and bold too.
  • A class used like this is a state class: it names a state an element can be in, and the stylesheet says once how that state looks. To change how one element looks as its state changes, switch its class. The stylesheet already says how each state looks.
  • Edits in the Elements panel only last in that tab until it reloads. The browser then reads your file again. Later on, a script will do the switching: each time the page loads, it reads the game data and puts long on every game over an hour.

The Final Code

Nothing in the files changed in this video. The games list in scratch/index.html:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Priya’s page now has a stylesheet. Catan, a long game, shows in the accent color, in bold. Codenames, a short one, is gray. One rule does that. It’s the rule for a list item with the class long, inside the games list.

00:17 It never names Catan. It styles whichever game carries that class.

00:23 Next month’s games will be different, and a script will build the list. So the stylesheet can’t know which games are long, and it doesn’t need to. In this video, you’ll switch that class off and on by hand and watch the page follow.

00:38 Follow along on Priya’s page, and stop the video whenever you need time. Open DevTools, go to the Elements panel, and select Catan’s list item. Its class attribute holds one word, long.

00:52 The Styles pane lists both games rules. The long rule sets the bold and the color. The gray it beats is crossed out.

01:01 Now switch long off. Double-click the word long in the tree, and it turns into a text box. Delete the word, but don’t press Enter yet. Your turn: pause the video and predict how Catan looks once you press Enter.

01:17 Press Enter, and Catan turns gray and loses its bold. It isn’t black, because the games list rule still matches it. Only the long rule stopped matching, so it’s gone from the Styles pane.

01:32 Now switch long back on. In the Elements panel, right-click Catan’s row and choose Add attribute. Type the class attribute with the value long, as shown. Watch out: no dot here.

01:46 In the selector, the dot in front of long is what says it’s a class. In the HTML, you type the bare name. Press Enter, and Catan is back in the accent color, in bold.

02:00 Any game can carry the class. In the Elements panel, right-click the Codenames row, choose Add attribute, and type the same thing. Press Enter, and Codenames turns the accent color and bold too.

02:14 You didn’t touch the stylesheet once. The rule stayed the same, and the class decided which games it applied to.

02:22 A class used like this is called a state class. It names a state an element can be in, like long. The stylesheet says once how that state looks. So here’s the rule.

02:35 To change how one element looks as its state changes, switch its class. The stylesheet already says how each state looks.

02:44 These edits only live in this tab. Reload, and Catan has the long class again and Codenames has none, because the browser reads your file afresh. Later on, a script will do this switching for you.

02:57 Each time the page loads, it’ll read the game data and put long on every game over an hour. That’s all you need about that for now.

03:05 You’ve switched one class and watched one rule follow it. Next comes Exercise: Mark Up the Game Night Page, where you’ll write Priya’s page and its stylesheet yourself.

Become a Member to join the conversation.