What is the purpose of the class attribute in HTML?
The <code>class</code> attribute assigns one or more class names to an HTML element. These class names can be used to apply CSS styles or select elements with JavaScript.
Click to reveal answer
beginner
How do you assign multiple classes to a single HTML element?
You list multiple class names separated by spaces inside the <code>class</code> attribute, like <code>class="class1 class2"</code>.
Click to reveal answer
beginner
True or False: The class attribute can be used to group elements for styling and scripting.
True. The class attribute groups elements so CSS and JavaScript can target them easily.
Click to reveal answer
beginner
Example: What classes are assigned in this tag? <div class="box highlight"></div>
The element has two classes: box and highlight. Both can be used to style or select this element.
Click to reveal answer
intermediate
Why should you avoid using the same class name for unrelated elements?
Using the same class name for unrelated elements can cause unexpected styling or scripting effects because all elements with that class share the same rules.
Click to reveal answer
What does the class attribute do in HTML?
ASpecifies the element's type
BDefines the element's ID
CSets the element's text content
DAssigns a name to an element for styling and scripting
✗ Incorrect
The class attribute assigns one or more class names to an element for CSS and JavaScript to use.
How do you add two classes named 'red' and 'bold' to a paragraph?
A<code><p class="red,bold"></code>
B<code><p class="red bold"></code>
C<code><p class="red;bold"></code>
D<code><p class="red+bold"></code>
✗ Incorrect
Multiple classes are separated by spaces inside the class attribute.
Can multiple elements share the same class name?
AOnly if they are inside the same parent
BNo, class names must be unique
CYes, many elements can share the same class
DOnly if they are the same element type
✗ Incorrect
Class names are meant to group elements, so many elements can share the same class.
Which is the correct way to select all elements with class 'menu' in CSS?
A.menu { }
B#menu { }
Cmenu { }
D*menu { }
✗ Incorrect
In CSS, a dot (.) before a name selects elements by class.
What happens if you use the same class name for different types of elements?
AThey all get the same styles and scripts targeting that class
BOnly the first element gets styled
CThe browser ignores the class
DIt causes an error
✗ Incorrect
All elements with the same class share the same CSS and JavaScript rules.
Explain how the class attribute helps in styling and scripting HTML elements.
Think about how you can group elements to style them all at once.
You got /4 concepts.
Describe the correct way to add multiple classes to an HTML element and why it is useful.
Imagine you want one element to look like a box and also be highlighted.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the class attribute in HTML?
easy
A. To group elements for styling or scripting
B. To add inline styles directly to an element
C. To define the element's unique ID
D. To specify the element's text content
Solution
Step 1: Understand the role of the class attribute
The class attribute is used to assign one or more class names to an HTML element.
Step 2: Identify the purpose of class names
Class names help group elements so CSS or JavaScript can target them easily for styling or behavior.
Final Answer:
To group elements for styling or scripting -> Option A
Quick Check:
class attribute groups elements [OK]
Hint: Class groups elements for CSS/JS use [OK]
Common Mistakes:
Confusing class with id attribute
Thinking class adds inline styles
Believing class sets element content
2. Which of the following is the correct way to assign multiple classes to an element?
easy
A. <div class="red,bold"></div>
B. <div class="red bold"></div>
C. <div class="red;bold"></div>
D. <div class="red.bold"></div>
Solution
Step 1: Recall how to separate multiple classes
Multiple classes must be separated by spaces inside the class attribute.
Step 2: Check each option's syntax
<div class="red bold"></div> uses a space between "red" and "bold", which is correct. Others use commas, semicolons, or dots which are invalid.
Final Answer:
<div class="red bold"></div> -> Option B
Quick Check:
Multiple classes separated by spaces [OK]
Hint: Separate multiple classes with spaces [OK]
Common Mistakes:
Using commas or semicolons instead of spaces
Trying to join classes with dots
Forgetting to separate class names
3. What will be the color of the text inside this HTML snippet?
Step 1: Identify classes assigned to the paragraph
The paragraph has two classes: "red" and "bold".
Step 2: Check CSS rules for these classes
"red" sets text color to red, "bold" sets font weight to bold. Both styles apply together.
Final Answer:
Red and bold text -> Option D
Quick Check:
Multiple classes combine styles [OK]
Hint: Multiple classes combine all styles [OK]
Common Mistakes:
Thinking only one class style applies
Ignoring combined effects of multiple classes
Confusing class names with IDs
4. Find the error in this HTML snippet:
<div class=red bold>Content</div>
medium
A. Class attribute cannot have multiple classes
B. Class names should be separated by commas
C. Missing quotes around class attribute value
D. The tag <div> cannot have a class attribute
Solution
Step 1: Check syntax for class attribute
The value of the class attribute must be enclosed in quotes if it contains spaces.
Step 2: Identify the error in the snippet
Here, class=red bold lacks quotes, so the browser misinterprets it.
Final Answer:
Missing quotes around class attribute value -> Option C
Quick Check:
Class attribute values with spaces need quotes [OK]
Hint: Always quote class values with spaces [OK]
Common Mistakes:
Omitting quotes for multiple classes
Separating classes with commas
Thinking class attribute is invalid on div
5. You want to style all buttons with a green background and white text, but only those with class primary should also be bold. Which HTML and CSS setup achieves this?
All buttons get green background and white text via class "btn". Only "primary" buttons are bold.
Step 2: Check HTML and CSS correctness
<button class="btn primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } correctly assigns two classes separated by space and styles them separately. <button class="btn,primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } uses comma in class attribute which is invalid. <button class="btn primary">Click</button>\n.btn.primary { background: green; color: white; font-weight: bold; } combines styles but makes all primary buttons green and bold, missing the separate "btn" style. <button class="primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } misses "btn" class on button, so green background won't apply.