Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
In the previous lesson, you saw that an Element Selector (like p { color: blue; }) styles every single paragraph on your page.
But what if you have 10 paragraphs on a page, and you want:
Element selectors cannot do this! This is where Class Selectors and ID Selectors become your best friends. 🚀
To never get confused between a Class and an ID, remember this simple mental model:
+-------------------------------------------------------------------------+ | CLASS vs ID: THE SCHOOL ANALOGY | +-------------------------------------------------------------------------+ | CLASS SELECTOR (.dot) --> THE SCHOOL HOUSE BADGE | | (Red House / Shivaji House) | | • Many different students can wear the same Red House badge. | | • One student can wear multiple badges (e.g., Red House + Sports Cap). | | • Reusable across many elements on the page. | | | | ID SELECTOR (#hash) --> THE STUDENT ROLL NUMBER / AADHAAR CARD | | (Roll No: 1042 / Aadhaar ID) | | • Strictly UNIQUE to one single person in the entire school. | | • No two elements on the same webpage should ever have the same ID. | +-------------------------------------------------------------------------+
.classname)A Class Selector targets one or more HTML elements that share a specific class attribute.
class attribute:. ):.highlight.
In HTML, you DO NOT write a dot: class="highlight" (never class=".highlight").You can apply the exact same class to completely different HTML tags!
All three elements will automatically be centered!
An HTML element can have multiple classes at the same time. Separate each class name with a simple space:
This multi-class pattern is used by top frameworks like Bootstrap and Tailwind CSS!
#idname)An ID Selector targets one unique HTML element on the page that has a matching id attribute.
id attribute:#):id="submit-btn" is invalid HTML!#1header is invalid; use #header-1 instead).| Feature | Class Selector | ID Selector |
|---|---|---|
| CSS Symbol | Period / Dot (.card) | Hash / Pound (#header) |
| HTML Attribute | class="card" | id="header" |
| How many elements can have it? | Unlimited elements on the page | Only 1 unique element on the page |
| Can one element have multiple? | Yes (class="btn btn-large btn-blue") | No (Only 1 ID per element) |
| Specificity (Priority) | Medium priority | High priority (Overrides classes) |
| Best Used For | Reusable components (cards, buttons, alerts) | Unique landmarks (header, footer, modal container) |
What happens if an element has both an ID and a Class rule that specify conflicting text colors?
color: red;) 🏆Because an ID is unique to a single element, the browser considers it much more specific than a class. An ID rule will always beat a class rule, no matter what order they are written in your stylesheet!
| What Beginners Do (Don't ❌) | What You Should Do Instead (Do ✅) | Why? |
|---|---|---|
<div class=".card"> | <div class="card"> | The dot (.) belongs in the CSS file, never inside the HTML attribute. |
Using the same id="nav" on 3 different menus. | Use class="nav" on all 3 menus. | IDs must be strictly unique per webpage. |
Giving an element multiple IDs: id="main top". | Use multiple classes: class="main top". | HTML only allows a single value inside id="...". |
.1st-box { ... } (Starts with a number) | .first-box or .box-1 { ... } | CSS class and ID names must not start with a digit. |
.) in CSS and is reusable on multiple elements.#) in CSS and must be unique to one element on the page.class="card", not class=".card").class="badge badge-success").Test your understanding with these multiple-choice questions:
<div id="profile-card">?class="text-blue" (setting blue text) and id="title" (setting red text), what color will the text display?school-cards.html..card styling..red-house, .blue-house, .green-house).#head-girl ID! 🎯In the next lesson, we will continue your web styling journey with Grouping and Combinator Selectors (CSS Syntax & Selectors Basics).
👉 Continue to Next Lesson: Grouping and Combinator Selectors →
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Universal and Element Selectors | Grouping and Combinator Selectors |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.