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.
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.
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.
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.
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.
<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.
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.class="warning" is good; class="red-text" is bad — because the day you change the colour to orange, the name becomes a lie.class="1st" — class names must start with a letter (or a hyphen or underscore).class="my note" — that is two classes, my and note, not one.note { } styles a <note> tag, which does not exist..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.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.<!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>
Change the code below and press Run to see the result instantly. No file to save, no login needed.
The questions learners ask most about this lesson — read the question, then open it to see the answer.
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.
Yes, separate the names with spaces, and the element gets the styles of every class it carries.
Name it by what the thing is, not how it looks, so the name stays true even if the design changes later.
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.
Finish all 35 lessons, pass the test (80%), pay ₹10 and get your certificate.
Take the testPrefer watching? Video courses teach the same topics step by step.
Free tutorials teach you the basics. Our paid courses add live projects, mentorship and a certificate.
Browse all coursesAsk about courses, website & more
Request human callback:
Please log in to download. You will stay on this page after logging in!
New here? Register now →