SA-21/06, C-11, Asapur, Varanasi, UP support@kamaldnp.com +91 8318013442
HTML
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10
Try this code live
Lesson 21 of 35

HTML Class Attribute: Style Many Elements at Once

4 min read · Free · No login needed

The class attribute gives an element a name that CSS and JavaScript can use. Unlike id, which must be unique, a class can be shared by many elements — and one element can have several classes at once.

Diagram — see how it fits together
Infographic: class vs ID in HTML

Why classes exist

Imagine a page with twenty course cards that should all look the same. Without classes you would have to style each one separately. With a class, you write the style once — .card { border: 1px solid #ccc; padding: 16px; } — and give every card class="card". Change the CSS once and all twenty cards change together.

Multiple classes

An element can carry several classes, separated by spaces: <div class="card featured large">. In CSS, .card styles all cards, .featured adds the special look, and .card.featured (no space) targets only cards that are also featured. This is how real websites build flexible designs from small reusable pieces.

Class naming

Good class names describe what the thing is, not how it looks: class="course-card" is better than class="orange-box". If the design changes later and the box becomes blue, the name "orange-box" becomes a lie, while "course-card" stays true.

Tip: Class names cannot contain spaces (a space separates two classes) and should not start with a number. Use letters, numbers, hyphens, and underscores.

What a class is

A class is a name you give to elements so you can treat them as a group. The same class can be used on as many elements as you like — ten buttons, fifty cards, all with class="btn". In CSS you write the class with a dot in front, like .btn, and one rule styles every element that carries that name.

Syntax

<p class="note">First note.</p>
<p class="note">Second note.</p>
<p class="note important">Third note — two classes.</p>

<style>
.note { color: grey; }
.important { font-weight: bold; }
</style>

An element can carry more than one class — just separate the names with a space. The third paragraph above gets both the grey colour from .note and the bold text from .important.

Note: Class names are case-sensitive. class="Note" and class="note" are two different classes, and CSS will treat them as different. Pick one style — small letters — and use it everywhere.
Tip: Name classes by what the element is, not how it looks. class="warning" is good; class="red-text" is bad — because the day you change the colour to orange, the name becomes a lie.

Common mistakes

  • Starting a class name with a number, like class="1st" — class names must start with a letter (or a hyphen or underscore).
  • Putting a space inside one class name, like class="my note" — that is two classes, my and note, not one.
  • Writing the class in HTML but forgetting the dot in CSS — note { } styles a <note> tag, which does not exist.
  • Using a class where an id belongs — a class is for groups, an id is for one single element.

Example explained

  • .card { border: 1px solid #ccc; padding: 12px; } — one CSS rule that styles every element with class "card". Write it once, and all three cards get the same border and padding.
  • .featured { background: #fff3e0; } — a second rule that only adds a light orange background. It does not repeat the border or padding — those already come from .card.
  • <div class="card featured"> — this element carries two classes, so it gets both rules: the border and padding from .card plus the background from .featured.
  • The other two divs have only class="card", so they look the same as each other but different from the featured one — which is exactly how one class can style a whole group.

Example

Example
<!DOCTYPE html>
<html>
<head>
<title>Classes</title>
<style>
.card { border: 1px solid #ccc; padding: 12px; }
.featured { background: #fff3e0; }
</style>
</head>
<body>

<div class="card">Normal card</div>
<div class="card featured">Featured card</div>
<div class="card">Another normal card</div>

</body>
</html>

Output

Output — how it looks in the browser
Output of HTML Class Attribute: Style Many Elements at Once

Try it yourself — live editor

Change the code below and press Run to see the result instantly. No file to save, no login needed.

Result

Frequently asked questions

The questions learners ask most about this lesson — read the question, then open it to see the answer.

1. What is a class in HTML?

It is a name given to elements so CSS and JavaScript can treat them as a group; the same class can be shared by many elements.

2. Can one element have several classes?

Yes, separate the names with spaces, and the element gets the styles of every class it carries.

3. How should you name a class?

Name it by what the thing is, not how it looks, so the name stays true even if the design changes later.

Why learn this yourself — and not just ask AI?

AI can write code for you, but it cannot think for you. When you learn HTML yourself, your brain builds the real skill: you understand why a page looks the way it does, you can fix it when it breaks, and you can answer it in an exam or an interview without help. That is the skill that gets you a job — not a copied answer.

Get certified — just ₹10

Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.

Take the test

Prefer watching? Video courses teach the same topics step by step.

Go further with a full course

Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.

Core Python Programming – 3 Month Complete Course
₹700
View
AI & ML with Azure AI, AWS AI & Generative AI
₹10,000
View
Website Design & Development Workshop 2026
₹299
View
Browse all courses
HTML — Lessons
1. HTML Introduction: What is HTML? Learn with Examples 2. Best Free HTML Editors: Notepad to VS Code for Beginners 3. HTML Basic Structure: Doctype, Head, Body Explained 4. HTML Elements Explained: Tags, Content & Empty Elements 5. HTML Attributes List: href, src, alt, width, title & More 6. HTML Headings h1 to h6: Which Heading Tag to Use When 7. HTML Paragraphs: p Tag, br Line Breaks & pre Text 8. HTML Style Attribute: Colour, Font Size & Alignment 9. HTML Text Formatting Tags: bold, italic, mark & More 10. HTML Comments: How to Add Notes & Hide Code in HTML 11. HTML Colors: Names, HEX, RGB & HSL Colour Codes 12. HTML CSS: Inline, Internal & External Stylesheets 13. HTML Links: a Tag, href, New Tab & Email Links 14. HTML Images: img Tag, src, alt, Width & Formats 15. HTML Favicon: Create & Add a Browser Tab Icon 16. HTML Title Tag: How to Write a Good Page Title 17. HTML Tables: table, tr, th, td, colspan & rowspan 18. HTML Lists: ul, ol, dl & Nested Lists Explained 19. HTML Block vs Inline Elements: div, p, span Explained 20. HTML Div Tag: Group Content & Build Page Layouts 21. HTML Class Attribute: Style Many Elements at Once 22. HTML id Attribute: Unique IDs, Links, CSS & JavaScript 23. HTML Iframes: Embed YouTube, Maps & Pages in HTML 24. HTML JavaScript: script Tag, Clicks & First Example 25. HTML File Paths: Relative vs Absolute Paths Explained 26. HTML Head Element: title, meta, charset & viewport 27. HTML Layout: header, nav, main, footer Page Structure 28. HTML Responsive Design: Viewport, Images & Media Queries 29. Semantic HTML Tags: header, nav, main, article & footer 30. HTML Forms: input, label, select, checkbox & Submit 31. HTML Entities: & < > © with Examples 32. HTML Audio & Video Tags: controls, source & Fallback 33. HTML Accessibility: alt Text, Labels, Landmarks & ARIA 34. HTML Meta Tags & SEO: charset, viewport, OG Tags 35. HTML Quotations: blockquote, q, cite, abbr & address
Final Test — Certificate ₹10

🤖 KAMALDNP AI Assistant

Ask about courses, website & more

Hi! I'm KAMALDNP AI Assistant. Ask me about our courses, website features, or anything else! 😊
Please wait 5s...
🤖 KAMALDNP Assistant ✕

Request human callback:

🔐 Login Required
✕

Please log in to download. You will stay on this page after logging in!

New here? Register now →