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.
What students will learn in this module
Every question in Module 36
Quick questions
- Which syntax correctly selects all elements with class="menu"?A#menuB.menu
- 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
- What does p.note target?AOnly p elements that also have class="note"BAll p elements AND all elements with class="note"
- 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
- 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 doesp.noteMatch it to what it doesReusable across many elements on the pageMatch it to what it doesApplying multiple classes to one elementMatch it to what it doesclass=".highlight"Match it to what it doeshighlight { color: red; }Match it to what it doesFill in the blanks
__________ { color: red; }Hint: Type the class selector for a class named highlight, including the dot
<p class="______">Hello</p>
Hint: Type the class name that matches the selector .notice
p________ { font-size: 14px; }Hint: Type the class selector that restricts this rule to p elements with class warning
.______ { border: 1px solid; }Hint: Type the class name this selector targets, the part after the dot
<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}
