06. Basic Selectors, Combinators & Attribute Selectors
🟢 Beginner
1. Definition
A CSS Selector is the part of a CSS rule set that targets specific HTML elements on a webpage so that property declarations can be applied. Basic types include Universal (*), Element (p), Class (.btn), ID (#main), Grouping (h1, h2), Combinators (>, +, ~), and Attribute Selectors ([type="text"]).
2. Why It Is Used
Selectors enable precise targeting control. Without selectors, CSS would have no way of distinguishing between headings, action buttons, paragraphs, or hero banners.
3. How It Works
The browser engine matches selector syntax against DOM nodes. Element selectors match tag names, class selectors match class="..." attributes, ID selectors match id="..." attributes, and combinators match structural parent-child relationships.
4. Syntax
/* Basic selector syntax formats */
* { box-sizing: border-box; } /* Universal */
p { color: #333; } /* Element */
.btn { padding: 10px; } /* Class */
#hero { font-size: 2rem; } /* ID */
[type="text"] { border: 1px solid; } /* Attribute */
5. Basic Selector Types Reference
| Selector Type | Syntax Format | Target Description | Specificity Score |
|---|---|---|---|
| Universal | * | Targets all elements on page | 0, 0, 0, 0 |
| Element | p | Targets elements by HTML tag name | 0, 0, 0, 1 |
| Class | .classname | Targets elements with matching class attribute | 0, 0, 1, 0 |
| ID | #idname | Targets single unique element with matching ID | 0, 1, 0, 0 |
| Direct Child | div > p | Targets paragraphs that are direct children of div | 0, 0, 0, 2 |
| Adjacent Sibling | h1 + p | Targets paragraph immediately following h1 | 0, 0, 0, 2 |
6. Beginner Example
/* Element & class selectors */
h2 {
color: #1e3a8a;
}
.text-muted {
color: #64748b;
}
7. Intermediate Example
/* Combinator selectors */
.card > .card-title {
font-size: 1.25rem;
font-weight: 700;
}
.card-title + .card-subtitle {
margin-top: 4px;
}
8. Advanced Attribute Selector Example
/* Advanced attribute matching */
a[href^="https://"] {
color: #2563eb;
padding-right: 18px;
}
input[type="submit"]:hover {
background-color: #1d4ed8;
}
9. Line-by-Line Code Explanation
.card > .card-title: Direct child combinator matching.card-titleonly if it is a direct child of.card.a[href^="https://"]: Attribute selector matching links whosehrefstarts withhttps://.
10. Expected Terminal / Browser Output
Card component elements and external links formatted precisely based on tag, class, and structural relationship.
11. Common Mistakes & Pitfalls
- Reusing the same ID on multiple elements: IDs must be completely unique per page. Use classes for reusable styles.
12. Important Rules & Limitations
- Class names in CSS start with a dot
.; ID names start with a hash#.
13. Multilingual Explanation: English
Basic selectors target HTML elements by tag, class, ID, or structural parent-child relationships.
14. Multilingual Explanation: Hindi (Roman Script)
Selectors HTML elements ko target karte hain. Class ke liye . aur ID ke liye # ka use karein.
15. Multilingual Explanation: Marathi (Roman Script)
Selectors HTML elements na target kartat. Class sathi .class ani ID sathi #id vapartat.
16. Practice Questions
- What is the difference between a descendant combinator (
div p) and a direct child combinator (div > p)?
17. Practical Coding Challenge
Write CSS selectors targeting all external links starting with https:// and give them a green accent color `#16a34a`.