CSS Selectors & Advanced Styling – Class, ID & Grouping Explained

👋 Introduction:
Aapne HTML structure banana seekh liya. Lekin bina design ke website dull lagti hai.
Ab CSS (Cascading Style Sheets) se HTML ko stylish banate hain.
Lekin ek baat important hai:
HTML elements ko target kaise karoge? Iske liye use hota hai – CSS Selectors.
🎯 What is a CSS Selector?
CSS selector browser ko batata hai ki kis element ko style apply karni hai.
h1 {
color: red;
}
Yahan h1
ek selector hai – jo batata hai ki h1
tag ka color red ho.
🧩 Types of CSS Selectors
✅ 1. Element Selector
Direct tag ko target karta hai:
p {
font-size: 18px;
}
Sabhi <p>
tags par yeh style lagega.
✅ 2. Class Selector (.)
Same style multiple elements par apply karne ke liye:
.title {
color: blue;
font-weight: bold;
}
<h2 class="title">Hello</h2>
<p class="title">Paragraph</p>
🔹 Use: Jab multiple elements ko same design deni ho.
✅ 3. ID Selector (#)
Specific element ke liye styling:
#main-header {
font-size: 32px;
color: green;
}
<h1 id="main-header">Main Heading</h1>
🔸 Use: Jab ek unique element ho page me.
✅ 4. Group Selector (,)
Multiple elements ko same style:
h1, h2, p {
margin: 0;
padding: 0;
}
✅ Clean aur DRY (Don’t Repeat Yourself) code ke liye helpful.
✅ 5. Universal Selector (*)
Sabhi elements par style lagata hai:
* {
margin: 0;
padding: 0;
}
🔸 Use: Reset CSS ya global styling ke liye.
✅ 6. Nested / Descendant Selectors
Ek element ke andar ke specific elements ko target karta hai:
div p {
color: purple;
}
<div>
<p>This will be purple</p>
</div>
<style>
.box {
background-color: lightblue;
padding: 10px;
}
#important-text {
color: red;
font-weight: bold;
}
h2, h3 {
text-align: center;
}
</style>
<div class="box">
<h2>CSS Selectors</h2>
<p id="important-text">This is styled with an ID selector.</p>
</div>
⚠️ Class vs ID – Difference
Feature | Class | ID |
---|---|---|
Symbol | . | # |
Use | Multiple elements | One unique element |
Reuse | Yes | No |
Example | .btn | #submit-btn |
🚀 Bonus Tip: Combine Selectors
div.box p {
font-style: italic;
}
👉 Iska matlab: .box
class ke andar jo <p>
tag hai, us par style lagegi.
🏁 Conclusion
Aapne aaj seekha:
✅ CSS selectors kya hote hain
✅ Class, ID, element, group kaise use karte hain
✅ Real-world styling tricks
Ye har frontend project ka foundation hai.
Ab aap kisi bhi HTML element ko precisely target kar sakte ho!
Comments
Post a Comment