A selector is the "who" of a CSS rule. Good selectors let you style exactly the right elements, such as all buttons, one particular card, or only the links inside the footer, without changing the HTML.
Selectors are worth learning well because they appear everywhere. JavaScript uses the same syntax to find elements on a page, and browser testing tools use it too. Time spent here pays off long after you finish the CSS course.
The basic CSS selectors
| Selector | Example | Selects |
|---|---|---|
| Element | p | Every <p> |
| Class | .card | Every element with class="card" |
| ID | #logo | The element with id="logo" |
| Universal | * | Every element |
| Group | h1, h2 | Every h1 and every h2 |
Use classes for almost all your styling. They're reusable, they say what an element is for, and they're easy to override. Element selectors are best for broad defaults, such as setting the font for every paragraph.
Combinators: selecting by family relationship
| Selector | Example | Selects |
|---|---|---|
| Descendant (space) | nav a | Every a anywhere inside nav |
Child (>) | ul > li | li elements that are direct children of ul |
Next sibling (+) | h2 + p | The first p straight after an h2 |
Later siblings (~) | h2 ~ p | Every p after an h2 with the same parent |
Combinators let you style elements based on where they sit in the page. A link in the navigation can look different from a link in an article, with no extra classes needed.
Combining selectors
p.introselects a<p>that also has the classintro(no space)..btn.primaryselects elements that have both classes..card pselects paragraphs inside an element with classcard(with a space).
That one space changes everything: p.intro and p .intro are completely different selectors. Reading selectors from right to left helps: .card p means "a paragraph, inside a card".
Attribute selectors
| Selector | Selects |
|---|---|
[disabled] | Elements that have the attribute |
[type="email"] | Attribute equals a value |
a[href^="https"] | Value starts with |
a[href$=".pdf"] | Value ends with |
img[alt*="dog"] | Value contains |
Attribute selectors are useful for form fields and links. For example, a[href$=".pdf"] can add a small PDF icon to every link that downloads a PDF.
Keep selectors short and simple
It's tempting to write long selectors like body div.main ul li a, but they're fragile. If the HTML structure changes slightly, the rule stops working. Long selectors are also harder to override. A single, well-named class such as .menu-link is easier to read and more reliable. A good rule of thumb is to keep most selectors to one or two parts.
CSS Selectors Syntax
Selector cheat sheet
p { } /* element */
.note { } /* class */
#header { } /* id */
nav a { } /* descendant */
ul > li { } /* child */
a[target] { } /* attribute */CSS Selectors Summary
- Selectors choose which elements a rule applies to.
- Element, class, id, universal and grouped selectors cover most needs.
- Combinators select by relationship: descendant, child and sibling.
p.intro(no space) andp .intro(with a space) mean different things.- Prefer short, class-based selectors that are easy to read and override.
CSS Selectors Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Class, ID and element
<h1 id="title">Plant Shop</h1>
<p>Our bestsellers:</p>
<p class="tag">Monstera</p>
<p class="tag sale">Snake Plant</p>p {
font-family: sans-serif;
}
#title {
color: #17a865;
}
.tag {
display: inline-block;
padding: 4px 10px;
border-radius: 99px;
background: #e1e6ff;
}
.tag.sale {
background: #ffe3d7;
}Line-by-line explanation
- Lines 1 to 3Every paragraph gets a sans-serif font.
- Lines 5 to 7Only the element with
id="title". - Lines 9 to 14Both elements with the
tagclass become pills. - Lines 16 to 18Only elements with both
tagandsale.
Descendant vs child
Both lists are inside .menu, but only the top-level items are direct children.
<ul class="menu">
<li>Home</li>
<li>Shop
<ul>
<li>Shoes</li>
<li>Hats</li>
</ul>
</li>
</ul>.menu li {
color: gray;
}
.menu > li {
font-weight: bold;
color: #15161b;
}Line-by-line explanation
- Lines 1 to 3Descendant: every
liinside.menu, at any depth, turns gray. - Lines 5 to 8Child: only
lidirectly inside.menuare bold and dark. Shoes and Hats stay gray.
Attribute selectors for links
<p><a href="https://example.com">External site</a></p>
<p><a href="/about">Internal page</a></p>
<p><a href="/menu.pdf">Menu (PDF)</a></p>a[href^="https"] {
color: #7c3aed;
}
a[href$=".pdf"] {
color: #e5484d;
font-weight: bold;
}Line-by-line explanation
- Lines 1 to 3Links whose
hrefstarts with "https" turn purple. - Lines 5 to 8Links ending in ".pdf" turn red and bold.
Common CSS Selectors Mistakes
Forgetting the dot or hash
card { padding: 20px; }
logo { width: 120px; }.card { padding: 20px; }
#logo { width: 120px; }Without . or #, the browser looks for <card> and <logo> elements, which don't exist.
A stray space
p .highlight { background: yellow; }
/* looks for .highlight INSIDE a p */p.highlight { background: yellow; }
/* a p that HAS the class highlight */A space means "inside". No space means "and also".
CSS Selectors in Real Projects
Selectors beyond CSS
JavaScript uses exactly the same selectors with document.querySelector(".card"). See DOM selecting. Testing tools, browser extensions and web scrapers all use selectors too, so learning them well pays off everywhere.
CSS Selectors Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Select the element with id="hero" and all elements with class="btn".
hero { height: 300px; }
btn { padding: 10px; }
# for ids, . for classes.What does article > p select?
> is the child combinator, so it matches direct children only.Which selector targets an element with both classes btn and large?
Which items turn red?
<h2>Title</h2>
<p>A</p>
<p>B</p>
<style> h2 + p { color: red; } </style>
+ selects only the element immediately after the h2.The CSS should make only the links inside the footer white, and the .price paragraph bold. Fix the selectors.
<a href="#">Header link</a>
<footer><a href="#">Footer link</a></footer>
<p class="price">£20</p>
<style>footer { background: #15161b; padding: 8px; }</style>a footer {
color: white;
}
p .price {
font-weight: bold;
}- Footer link is white
- Header link is not white
- Price is bold
CSS Selectors Quiz
4 questions to check what you learned.
Which selector matches every element?
*all.all#all
What does
a[href$=".pdf"]select?- Links containing "pdf"
- Links starting with ".pdf"
- All PDF files on the page
- Links whose href ends with ".pdf"
Which should you use most for styling?
- IDs
- Inline styles
- Classes
- The universal selector
How many elements does
.menu listyle in a menu with 3 top items and 2 nested sub-items?- 3
- 2
- 1
- 5
CSS Selectors Challenge
Style a to-do list with selectors
Without changing the HTML:
- Make every
liinside.todohavepadding: 8px - Make the
liwith classdonehavetext-decoration: line-through - Make the checkbox inputs (
input[type="checkbox"]) havemargin-right: 8px
<ul class="todo">
<li><input type="checkbox"> Water plants</li>
<li class="done"><input type="checkbox" checked> Learn selectors</li>
<li><input type="checkbox"> Call grandma</li>
</ul>- Every li has 8px padding
- The done item is crossed out
- Other items aren't crossed out
- Checkboxes have 8px right margin