The class attribute helps group HTML elements so you can style or control them together easily.
Class attribute in HTML
<tag class="class-name">Content</tag>
class="class1 class2".<p class="highlight">This is important text.</p>
<div class="box container">Content inside a box and container.</div>
<button class="btn primary">Click me</button>
This example shows how the class attribute is used to style a paragraph and a button. The paragraph has a yellow background and some padding. The button has blue background, white text, and changes color when hovered.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Class Attribute Example</title> <style> .highlight { background-color: #ffeb3b; padding: 0.5rem; border-radius: 0.25rem; } .btn { background-color: #2196f3; color: white; border: none; padding: 0.75rem 1.5rem; font-size: 1rem; border-radius: 0.3rem; cursor: pointer; transition: background-color 0.3s ease; } .btn:hover { background-color: #1976d2; } </style> </head> <body> <p class="highlight">This paragraph is highlighted with a yellow background.</p> <button class="btn">Click me</button> </body> </html>
Class names are case-sensitive, so Highlight and highlight are different.
Using classes is better than using inline styles because it keeps your HTML clean and styles reusable.
You can use the browser's developer tools (right-click element -> Inspect) to see which classes an element has and how styles apply.
The class attribute groups elements for styling or scripting.
You can assign multiple classes separated by spaces.
Use meaningful class names to keep your code clear and easy to maintain.
Practice
class attribute in HTML?Solution
Step 1: Understand the role of the class attribute
Theclassattribute 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 AQuick Check:
class attribute groups elements [OK]
- Confusing class with id attribute
- Thinking class adds inline styles
- Believing class sets element content
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 BQuick Check:
Multiple classes separated by spaces [OK]
- Using commas or semicolons instead of spaces
- Trying to join classes with dots
- Forgetting to separate class names
<style>
.red { color: red; }
.bold { font-weight: bold; }
</style>
<p class="red bold">Hello World</p>Solution
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 DQuick Check:
Multiple classes combine styles [OK]
- Thinking only one class style applies
- Ignoring combined effects of multiple classes
- Confusing class names with IDs
<div class=red bold>Content</div>
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 boldlacks quotes, so the browser misinterprets it.Final Answer:
Missing quotes around class attribute value -> Option CQuick Check:
Class attribute values with spaces need quotes [OK]
- Omitting quotes for multiple classes
- Separating classes with commas
- Thinking class attribute is invalid on div
primary should also be bold. Which HTML and CSS setup achieves this?Solution
Step 1: Understand the requirement for styling
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.Final Answer:
<button class="btn primary">Click</button>\n.btn { background: green; color: white; }\n.primary { font-weight: bold; } -> Option AQuick Check:
Multiple classes separated by spaces with separate CSS rules [OK]
- Using commas instead of spaces in class attribute
- Combining unrelated styles in one CSS selector
- Forgetting to assign all needed classes in HTML
Ready to prove it?
Pass the test and this pattern moves unknown
