On this page10 sections
Lecture 002: Selectors & Combinators
| Category | 1: CSS Basics |
| Lecture | 002 of 057 |
| Estimated video length | 14 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
A selector answers one question: which elements should this rule style? Pick all paragraphs, only the ones with a class, one special element by id, or links inside the navigation. Selectors are how you aim your styles.
Learning Objectives
- Select by element, class, id and the universal selector
- Group selectors with commas
- Combine them: descendant, child, and element-with-class
- Select by attribute
- Explain specificity: why some selectors win
1. Element, Class and ID Selectors
| Selector | Matches | Example |
|---|---|---|
p | Every <p> | p { … } |
.price | Every element with class="price" | .price { … } |
#logo | The one element with id="logo" | #logo { … } |
* | Every element | * { … } |
Example
<style>
p { color: #444; }
.price { color: crimson; font-weight: bold; }
#site-title { color: teal; }
</style>
<h1 id="site-title">Shoe Shop</h1>
<p>Running shoes</p>
<p class="price">Rs 6,500</p>
<p>Leather sandals</p>
<p class="price">Rs 3,200</p>An element can have several classes, separated by spaces: class="price sale".
2. Grouping With Commas
Give several selectors the same styles:
Example
<style>
h1, h2, h3 { font-family: Georgia, serif; color: #2b5797; }
</style>
<h1>Main title</h1>
<h2>Section</h2>
<h3>Sub-section</h3>3. Combining Selectors
| Selector | Matches |
|---|---|
nav a | Every <a> anywhere inside a <nav> (descendant) |
ul > li | <li> that are direct children of a <ul> |
p.note | <p> elements that have the class note |
h2 + p | The <p> straight after an <h2> |
Example
<style>
nav a { color: white; background: #4c6ef5; padding: 4px 10px; text-decoration: none; }
p.note { background: #fff3bf; padding: 6px; }
h2 + p { font-weight: bold; }
</style>
<nav><a href="#">Home</a> <a href="#">Shop</a></nav>
<a href="#">This link is outside the nav, so it stays plain.</a>
<h2>Delivery</h2>
<p>This first paragraph after the heading is bold.</p>
<p>This one is normal.</p>
<p class="note">A paragraph with the note class.</p>4. Attribute Selectors
Select by an attribute and its value:
Example
<style>
input[type="email"] { border: 2px solid #4c6ef5; }
a[target="_blank"]::after { content: " ↗"; }
</style>
<input type="text" placeholder="Name">
<input type="email" placeholder="Email (blue border)">
<p><a href="https://example.com" target="_blank">Opens in a new tab</a></p>5. Specificity: Which Selector Wins
When two rules conflict, the more specific one wins, wherever it is in the file:
id (#title) strongest
class (.title) ↓
element (h1) weakestExample
<style>
#headline { color: green; }
.big { color: orange; }
h1 { color: blue; }
</style>
<h1 id="headline" class="big">I am green: the id wins.</h1>
<h1 class="big">I am orange: the class beats the element.</h1>
<h1>I am blue.</h1>Use classes for almost everything. They're reusable and easy to override. Keep ids for
links (#section) and JavaScript.
Common Beginner Mistakes
1. Forgetting the dot or hash. price { } looks for a <price> element. Classes need
.price and ids need #price.
2. Spaces that change meaning. p.note = a paragraph with the class. p .note = an
element with the class inside a paragraph.
3. Styling with ids everywhere. Their high specificity makes later changes hard.
4. Class names with spaces. class="sale price" is two classes, sale and price.
Practice Questions
- Write a selector for every element with the class
card. - Write a selector for the element with the id
menu. - How do you give
h1andh2the same rule in one go? - What's the difference between
nav aandnav > a? - An element has both
id="x"andclass="y".#xsays red and.ysays blue. Which colour wins? - Write a rule that gives every
inputof typepassworda red border.
Practice Answers
.card#menuh1, h2 { … }nav amatches links anywhere inside the nav;nav > aonly matches links that are direct children.- Red. An id is more specific than a class.
input[type="password"] { border: 2px solid red; }
Key Takeaways
p(element),.class,#id,*(everything) are the basic selectors.- Commas group selectors; spaces,
>and+combine them. [attribute="value"]selects by attribute.- Specificity: id > class > element. Prefer classes.