Basic Selectors: Type, Class, ID, Universal and Grouping
The building blocks of every selector: type selectors, the universal selector, classes and multiple classes, IDs and why to prefer classes, compound selectors, grouping selectors into lists, and how to name classes well.
kvi5hnuCode Sprout team member9 min readOn this page
A selector is the part of a rule that says which elements to style. CSS has dozens of kinds, but most selectors you'll write are built from five simple ones: element names, *, classes, IDs and attributes. This article covers the first four, and how to combine them into compound selectors and selector lists. Attribute selectors are next, in article 8.
Type selectors
A type selector is an element name. It matches every element of that type:
p { line-height: 1.5; }
h1 { font-size: 2rem; }
button { font: inherit; }
Type selectors are good for defaults: how every paragraph, heading, link or table should look unless something more specific says otherwise. They're too broad for styling components, because ul matches your navigation, your footer links and every list in an article.
Type selectors match HTML element names without regard to case, so P and p match the same elements. Custom elements work too: book-card { display: block; }.
The universal selector
* matches every element. Alone it's used mostly for resets:
*, *::before, *::after { box-sizing: border-box; }
It's also implied whenever a selector has no type: .sale really means *.sale, and :hover means *:hover. You never need to write * in those cases.
* costs nothing in specificity (article 15), and it's fast: modern browsers match it as quickly as any other selector. Its risk is reach. A rule such as * { margin: 0; } also removes the margins of form controls, list items and anything else you might want to keep, so use it deliberately.
Class selectors
A class selector is a period followed by a class name. It matches elements whose class attribute contains that name:
.price { font-weight: 700; color: #2f7d4f; }
<p class="price">€14.50</p>
<span class="price">€9.00</span>
The class attribute is a space-separated list, so an element can have several classes, in any order, and a class selector matches if its name appears anywhere in the list:
<article class="book-card featured on-sale">...</article>
That element matches .book-card, .featured and .on-sale. Class names are case-sensitive: .Featured doesn't match featured.
Classes are the main tool for styling components: you choose the names, they can be reused on any element and any page, and they say what the element is for in your design.
Multiple classes on one element
Chain class selectors without spaces to require all of them on the same element:
.book-card.featured { border: 2px solid #8a3b12; } /* both classes */
.book-card .featured { ... } /* different! a .featured INSIDE a .book-card */
The space matters: with a space, the selector becomes a descendant combinator (article 9). Chaining is how you style a variant of a component, as in .button.primary, although many naming systems give variants their own class, such as .button--primary, to keep selectors flat (article 116).
.book-card { border: 1px solid #ccc; padding: 0.5rem 0.75rem; margin-bottom: 0.5rem; border-radius: 6px; }
.featured { background: #fff8e6; }
.book-card.featured { border: 2px solid #8a3b12; }
.on-sale::after { content: " · on sale"; color: #2f7d4f; font-weight: 700; }
The last paragraph gets the pale background from .featured and the "on sale" text, but not the thick border, which needs both book-card and featured.
ID selectors
An ID selector is # followed by an ID. It matches the element whose id attribute equals that value:
#site-header { position: sticky; top: 0; }
<header id="site-header">...</header>
IDs work differently from classes in a few ways:
- An ID must be unique in the page. Browsers don't enforce it, and
#xmatches every element withid="x"if you break the rule, but scripts and links assume one. - An element has at most one ID, and an ID can't contain spaces.
- IDs have much higher specificity than classes. One ID beats any number of classes (article 15). That's the main reason to avoid them in selectors: a rule like
#sidebar ais very hard to override later without another ID or!important.
IDs remain essential in HTML: for fragment links (href="#events"), for <label for="email">, for ARIA references such as aria-describedby, and as hooks for scripts. You can keep the IDs and style the same elements with classes.
If you need to target an ID without its specificity, use an attribute selector, [id="site-header"], or wrap it in :where(#site-header), which has zero specificity (article 12).

The middle panel is the problem with IDs in selectors: to style that link differently later, you'd need another ID, !important, or a rewrite.
Class or ID?
| Class | ID | |
|---|---|---|
| reusable | yes, on any number of elements | no, one element per page |
| several per element | yes | no |
| specificity | low, easy to override | high, hard to override |
| use for styling | almost always | rarely |
| other uses | script hooks | fragment links, <label for>, ARIA references |
Compound selectors
A compound selector is a sequence of simple selectors with no spaces between them, all describing the same element. A type selector, if there is one, must come first:
a.external /* an <a> with class external */
button.primary:hover /* a hovered <button> with class primary */
input#email[required] /* an <input> with id email and a required attribute */
li.book.on-sale /* an <li> with both classes */
Every part must match for the compound to match. Pseudo-classes such as :hover (article 11) and attribute selectors (article 8) can be added anywhere after the type. A pseudo-element such as ::before must come last (article 13).
Avoid qualifying classes with types unless it's needed. .price works on any element. span.price stops working when someone changes the span to a <p>, and adds specificity you'll have to beat later.

The highlights come from running each selector with querySelectorAll() in Chrome. Compare .price with span.price: adding the type narrows the match from two elements to one.
Grouping: selector lists
A selector list separates selectors with commas, and applies one declaration block to all of them:
h1, h2, h3 {
font-family: Georgia, serif;
line-height: 1.2;
}
That's exactly the same as writing three rules with the same declarations. Each selector in the list is matched independently and has its own specificity.
Remember from article 3 that a plain selector list is all or nothing: if one selector is invalid in a browser, the whole rule is dropped. Wrapping the list in :is() makes it forgiving (article 12).
Grouping and separate rules combine naturally: put shared declarations in a grouped rule, and the differences in individual rules:
h1, h2, h3 { font-family: Georgia, serif; color: #8a3b12; margin: 0.4em 0; }
h1 { font-size: 1.8rem; }
h2 { font-size: 1.3rem; border-bottom: 2px solid #f4c95d; }
h3 { font-size: 1rem; text-transform: uppercase; letter-spacing: 0.05em; }
Naming classes
Class names are for people reading the code. A few guidelines:
- Name what something is, not how it looks.
.noticesurvives a redesign..red-textstops being true when the notice turns orange. - Be consistent. Most projects use lowercase words separated by hyphens:
.book-card,.site-header. - Class names can't start with a digit in a selector without escaping.
class="2col"is valid HTML, but the selector must be written.\32 col, so choose names that start with a letter. The same goes for IDs:id="123", common in CMS output, is selected with#\31 23. In JavaScript,CSS.escape("123")produces the escaped form forquerySelector(). - Avoid styling with classes that scripts depend on. Some teams prefix script hooks, as in
.js-open-menu, so that renaming a style class can't break behavior.
Larger projects use naming conventions such as BEM (.book-card__title, .book-card--featured) to keep components separate. Part 17 covers these.
What's current
- All the selectors in this article have worked in every browser for decades.
- Selector matching is fast in all modern browsers. Readability and specificity are better reasons to choose one selector over another than speed.
Common questions
What's the difference between .a.b and .a .b?
.a.b (no space) matches one element that has both classes. .a .b (with a space) matches an element with class b inside an element with class a. They select completely different things.
Can an element have an ID and classes at the same time?
Yes, any combination: <header id="top" class="site-header sticky">. Selectors can use any of them, and you can even combine them in a compound selector, #top.sticky.
Is * bad for performance?
Not in modern browsers. A rule such as *, *::before, *::after { box-sizing: border-box; } is standard practice. The real cost of * is that it reaches everything, including elements you didn't mean to change.
Why shouldn't I use IDs for styling, if they're unique anyway?
Their specificity. A rule with an ID beats any number of class-based rules, so every later change to those elements needs another ID or !important. Classes keep specificity low and even, which makes the cascade predictable. Keep IDs in the HTML for links, labels and scripts.
Are class names case-sensitive?
Yes, in HTML documents in standards mode, which is every page with <!doctype html>. .Card doesn't match class="card". (In quirks mode, old browsers' compatibility mode, class and ID matching is case-insensitive, another reason to always use a doctype.)
Summary
- Type selectors match elements by name and suit site-wide defaults.
*matches every element. It's implied in selectors without a type, such as.price.- Class selectors match any element whose class list contains the name. Classes are reusable, combinable and low in specificity, so they're the main tool for styling.
- Chain classes with no space,
.a.b, to require both on one element. A space means a descendant. - ID selectors match one element, have very high specificity, and are best kept for links, labels and scripts.
- A compound selector, such as
button.primary:hover, combines simple selectors that all describe the same element. - A selector list groups selectors with commas. Each matches independently, and one invalid selector drops the whole rule.
Practice questions
Which elements does each selector match?
.alert.large { }
.alert .large { }
.alert, .large { }
Answer
.alert.large matches elements that have both classes. .alert .large matches elements with class large that are inside an element with class alert. .alert, .large matches every element with either class.
A stylesheet has these two rules. Why doesn't the second one change the color of <a class="button"> links inside #news, and what are two ways to fix it?
#news a { color: navy; }
.button { color: white; }
Answer
#news a contains an ID, so its specificity beats .button, which has only a class. Fixes: rewrite the first rule with a class, such as .news a, or lower its specificity with :where(#news) a. Adding another ID or !important to the button rule also works, but makes the problem worse for the next override.
Is p.intro or .intro the better selector for styling <p class="intro">, and why?
Answer
Usually .intro. It keeps working if the element changes to a <div> or <section>, and it has lower specificity, so it's easier to override. Qualify a class with a type only when the same class must behave differently on different elements.
Write one rule that gives h2 and h3 elements and any element with class section-title the same font, without repeating the declarations.
Answer
h2, h3, .section-title { font-family: Georgia, serif; }
Previous: Progressive Enhancement: Fallbacks, @supports and Writing CSS That Degrades Gracefully
Next: Attribute Selectors: Presence, Exact, Partial Matches and Case Sensitivity

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.