ID selectors, online

Six activities your students do on their own devices. See who's finished, and their scores.

ID selectors in one minute

What students need to know

Each id value may appear only once per HTML document; this element correctly uses a unique id. The ID selector #box overrides competing class or element rules due to its higher specificity score.

ID selectors start with # followed by the id value. A dot (.) is used for class selectors. An ID selector outweighs any number of class selectors in the cascade.

Mistakes students make

  • The selector .hero uses a dot (class selector). To target id="hero" it should be #hero { color: red; }.
  • The opening curly brace is missing. It should be #banner { color: gold; }.
  • There must be no space between # and the id name. It should be #title { with no gap.
  • The selector .main uses a dot (class selector). To target id="main" it should be #main { background: grey; }.

Discover other journeys with this module

What students will learn in this module

Every question in Module 37

Quick questions

  1. Which selector correctly targets an element with id="hero"?A#hero { }B.hero { }
  2. Can two elements on the same page share the same id value?ANo — id values must be uniqueBYes — ids can be reused
  3. Which has higher CSS specificity?AClass selector (.box)BID selector (#box)
  4. Is this ID selector written correctly: # title { color: red; }ANo — there should be no space after #BYes — the space is fine
  5. Which HTML attribute does the # selector match against?AclassBid

Key terms

Students type the meaning of:

  • #id

Match the pairs

#intro { color: navy; }Match it to what it does
<h1 id="title">Hello</h1>Match it to what it does
ID values must be unique on a pageMatch it to what it does
ID selectors have higher specificity than class selectorsMatch it to what it does
# hero { color: red; }Match it to what it does
.logo { width: 100px; }Match it to what it does

Fill in the blanks

  1. _______ { color: blue; }

    Hint: Type the ID selector for an element with id="header"

  2. <p id="_____">Hello</p>

    Hint: Type the id attribute value that matches the selector #intro

  3. _____ { background: red; }

    Hint: Type the ID selector for an element with id="main"

  4. #nav { _________: 18px; }

    Hint: Type the CSS property that controls text size

  5. _______ { margin: 0; }

    Hint: Type the ID selector for an element with id="footer"

Spot the error

Question 1

A<style>B.hero { color: red; }C</style>

Question 2

A<p id="banner">Sale!</p>B<style>C#banner color: gold; }

Question 3

A# title {B  font-size: 24px;C}

Question 4

A<div id="main">Nav</div>B<style>C  .main { background: grey; }D</style>

Question 5

A<style>B  #intro { color: navy; }C</style>D<p class="intro">Hello</p>