Class selectors, online

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

Class selectors in one minute

What students need to know

Unlike an ID, the same class name can appear on multiple elements, making it ideal for shared styles. A dot starts a class selector. A hash starts an ID selector.

Chaining a type and class selector with no space restricts the match to elements satisfying both conditions. A single class attribute can hold multiple class names separated by spaces.

Mistakes students make

  • The class attribute value must not include the dot. Write class="note" not class=".note". The dot is CSS syntax used only in the selector, not in the HTML attribute.
  • The background declaration is missing a semicolon. It should be background: navy; — every CSS declaration must end with a semicolon.
  • The selector is missing the leading dot. To target elements with class="title" it must be .title not title, which would target h2 elements by type.
  • The border-radius declaration is missing a colon after the property name. It should be border-radius: 8px; — a colon is required between property and value.

Discover other journeys with this module

What students will learn in this module

Every question in Module 36

Quick questions

  1. Which syntax correctly selects all elements with class="menu"?A#menuB.menu
  2. Can the same class be applied to more than one element on a page?ANo, each class can only appear onceBYes, classes can be reused on many elements
  3. What does p.note target?AOnly p elements that also have class="note"BAll p elements AND all elements with class="note"
  4. Is class="intro summary" valid HTML for applying two classes?AYes, space-separated values give an element multiple classesBNo, you must use two separate class attributes
  5. Which is the correct CSS selector for class="btn"?A.btnBbtn

Key terms

Students type the meaning of:

  • .classname

Match the pairs

.warningMatch it to what it does
p.noteMatch it to what it does
Reusable across many elements on the pageMatch it to what it does
Applying multiple classes to one elementMatch it to what it does
class=".highlight"Match it to what it does
highlight { color: red; }Match it to what it does

Fill in the blanks

  1. __________ {
      color: red;
    }

    Hint: Type the class selector for a class named highlight, including the dot

  2. <p class="______">Hello</p>

    Hint: Type the class name that matches the selector .notice

  3. p________ {
      font-size: 14px;
    }

    Hint: Type the class selector that restricts this rule to p elements with class warning

  4. .______ {
      border: 1px solid;
    }

    Hint: Type the class name this selector targets, the part after the dot

  5. <div class="card _______">

    Hint: Type the second class name being applied to this element

Spot the error

Question 1

A<p class=".note">Remember this.</p>B.note {C  color: blue;D}

Question 2

A.banner {B  background: navyC  color: white;D}

Question 3

A<h2 class="title">Welcome</h2>Btitle {C  font-size: 2rem;D}

Question 4

A.card {B  padding: 16px;C  border-radius 8px;D}

Question 5

Ap .highlight {B  color: green;C}