The class attribute, online

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

The class attribute in one minute

What students need to know

Both paragraphs share class="lead". CSS targeting .lead { } will style both paragraphs identically. class="btn primary" applies two classes. CSS can target .btn or .primary independently or together.

Unlike id, class values can be reused across many elements - that is the whole point of class-based styling. Class values must be wrapped in double quotes. class=btn without quotes is invalid HTML.

Mistakes students make

  • Attribute names must be lowercase in HTML. CLASS should be written as class.
  • Class values must be enclosed in double quotes: class="intro", not class=intro without quotes.
  • The class attribute value is missing its closing double quote. It should be class="note">.
  • There must be no space between class and =. The correct form is class="logo" with no space before the equals sign.
PREVIOUS · MODULE 14The id attribute
YOU ARE HERE · MODULE 15The class attribute
NEXT · MODULE 16Links (a and href)

Discover other journeys with this module

What students will learn in this module

Every question in Module 15

Quick questions

  1. Can two elements on the same page share the same class value?ANoBYes
  2. Which of these is the correct class attribute syntax?Aclass="btn"Bclass=btn
  3. True or false: class="my class" with a space applies one class named 'my class'.AFalseBTrue
  4. How do you apply two separate classes to one element?Aclass="btn primary"Bclass="btn" class="primary"
  5. True or false: CLASS with capital letters is valid HTML.ATrueBFalse

Key terms

Students type the meaning of:

  • class

Match the pairs

<p class="note">Remember this.</p>Match it to what it does
<li class="item">One</li>
<li class="item">Two</li>
Match it to what it does
class values can be reused across many elements for shared CSS stylingMatch it to what it does
Multiple classes are applied with space-separated values inside one class attributeMatch it to what it does
<p CLASS="note">Text.</p>Match it to what it does
<p class="my note">Text.</p>Match it to what it does

Fill in the blanks

  1. <p ___="intro">Hello</p>

    Hint: Type the attribute name used to assign a CSS class to an element

  2. <h1 ___="title">Welcome</h1>

    Hint: Type the attribute name that assigns a reusable CSS class

  3. <div ___="card">Content here.</div>

    Hint: Type the attribute name used to group elements for CSS styling

  4. <span ___="highlight">key term</span>

    Hint: Type the attribute name that assigns a CSS class to this span

  5. <button ___="btn">Click me</button>

    Hint: Type the attribute name used to style this button with a CSS class

Spot the error

Question 1

A<p CLASS="intro">Hello</p>B<p class="body">More text.</p>C<p class="end">Last.</p>

Question 2

A<h1>Title</h1>B<p class=intro>First paragraph.</p>C<p>More text.</p>

Question 3

A<div class="box">B  <p>Content here.</p>C  <p class="note"Text.</p>D</div>

Question 4

A<header>B  <h1 class="title">Site</h1>C  <nav class="menu">Links</nav>D  <p class="slogan">Hello.</p>E  <div class ="logo">Logo</div>F</header>

Question 5

A<header>B  <h1 class="title">Site</h1>C  <nav class="menu">Links</nav>D  <p class="tag line">Slogan.</p>E</header>