CSS Selectors
A selector tells the browser which elements a rule should style. Choosing the right selector is half the skill of writing CSS: too broad and you style things by accident, too narrow and your styles become hard to reuse.
This lesson covers the five basic selectors. Combinators, pseudo-classes, pseudo-elements and attribute selectors each get their own lesson later.
| Selector | Example | Selects |
|---|---|---|
| Element (type) | p |
Every <p> element |
| Class | .warning |
Every element with class="warning" |
| ID | #header |
The one element with id="header" |
| Universal | * |
Every element |
| Grouping | h1, h2 |
Every <h1> and every <h2> |
Element selector
Use the tag name. It targets every element of that type on the page.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
li { color: teal; font-size: 20px; }
em { background: yellow; }
</style>
</head>
<body>
<ul>
<li>Apples</li>
<li>Bananas <em>(ripe)</em></li>
<li>Cherries</li>
</ul>
</body>
</html>Class selector
A class selector starts with a dot. Classes are the workhorse of CSS: you can put the same class on as many elements as you like, and an element can have several classes separated by spaces.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
.tag {
display: inline-block;
padding: 4px 12px;
border-radius: 999px;
background: #dde4ff;
color: #264de4;
margin: 4px;
}
.hot { background: #ffd6d6; color: #c0392b; }
p.note { border-left: 4px solid gold; padding-left: 8px; }
</style>
</head>
<body>
<span class="tag">css</span>
<span class="tag hot">trending</span>
<span class="tag">design</span>
<p class="note">Only paragraphs with class "note" get the gold bar.</p>
<div class="note">This div has the class, but p.note does not match it.</div>
</body>
</html>p.note means "a <p> that also has the class note" — notice there is no space between p and .note. Likewise .tag.hot would match only elements that have both classes.
ID selector
An ID selector starts with #. An ID must be unique within a page, so it styles exactly one element.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; margin: 0; }
#banner {
background: #264de4;
color: white;
padding: 24px;
font-size: 24px;
text-align: center;
}
#banner + p { padding: 12px; }
</style>
</head>
<body>
<div id="banner">Welcome!</div>
<p>The banner above is styled with an ID selector.</p>
</body>
</html>Tip: Prefer classes over IDs for styling. IDs are very specific (they beat classes in the cascade), which makes them hard to override later. Keep IDs for anchors, JavaScript hooks and form labels.
Universal selector
* matches everything. It is most often used for a small "reset" at the top of a stylesheet:
* {
box-sizing: border-box;
margin: 0;
}Use it sparingly; styling every element can have surprising effects.
Grouping selectors
Separate selectors with commas to share one declaration block.
<!DOCTYPE html>
<html>
<head>
<style>
* { font-family: sans-serif; }
h1, .subtitle, #footer {
color: darkorchid;
text-align: center;
}
#footer { font-size: 12px; margin-top: 40px; }
</style>
</head>
<body>
<h1>Grouped Styles</h1>
<p class="subtitle">I share the purple color</p>
<p>I am an ordinary paragraph.</p>
<p id="footer">So do I, but I am smaller.</p>
</body>
</html>Naming classes well
- Name classes after purpose, not appearance:
.errorages better than.red-text. - Use lowercase words joined with hyphens:
.product-card,.nav-link. - Class names cannot start with a digit (
.2colis invalid; use.two-col).
Test yourself
Which selector targets all elements with class="price"?
#priceprice.price*price
What does p.intro select?
- Any element with class intro inside a p
- Paragraphs that have the class intro
- All paragraphs and all .intro elements
- The element with id intro
How many elements should share a single ID on a page?
- Exactly one
- Up to three
- Any number
- Only block elements