A selector tells CSS which elements a rule should apply to.
There are many selectors to learn, and the useful skill is not memorising every one of them. It is knowing which handful covers almost every job, and knowing how to combine them when it does not.
This is the first lesson in the CSS Basics and Selectors module, and the next lesson in this module is about "How Do the Cascade and Specificity Decide Who Wins?".
What are the selectors that you will use every day?
Five selector types cover the vast majority of stylesheets. There is the type selector, the class selector, the id selector, the attribute selector, and the combinators that join them together.
/* type: every h2 on the page */
h2 { font-size: 1.5rem; }
/* class: any element with that class */
.card { border: 1px solid #ddd; }
/* id: one specific element */
#site-header { position: sticky; top: 0; }
/* attribute: elements with that attribute, whatever the value */
input[required] { border-color: #b3261e; }
/* combinator: a descendant of something */
.card h3 { margin-top: 0; }The class selector is the workhorse.
It is reusable, it carries meaning, and it keeps presentation out of the markup. The same class can appear on any number of elements of any type.
The id selector is for the one element that is genuinely unique, and styling by id is usually a sign that a class would serve you better.
What do the combinators do?
There are four combinators that connect two selectors, and the difference between them is what sits between the two parts.
/* descendant: any depth */
nav a { color: inherit; }
/* child: exactly one level down */
nav > a { padding: 0.5rem; }
/* adjacent sibling: the element right after */
h2 + p { margin-top: 0; }
/* general sibling: any element after */
h2 ~ p { color: #555; }The child combinator is the one that earns its place, because it stops styles leaking down into deeper structures.
The nav a styles every link inside the navigation, however deeply it is nested, while the nav > a styles only the links that are direct children, and that is usually what you meant.
The sibling combinators are useful for spacing.
The h2 + p removes the top margin from the paragraph that follows a heading, and that is the standard way to stop the gap between a heading and its first paragraph from looking too large.
Which of the pseudo-classes matter most?
A pseudo-class selects elements by their state rather than their identity, and four of them do most of the work in an interface.
a:hover { color: #800d4d; } /* the pointer is over it */
input:focus-visible { outline: 2px solid #800d4d; } /* keyboard focus */
li:nth-child(2n) { background: #f7f7f7; } /* every second one */
input:disabled { opacity: 0.5; cursor: not-allowed; }The :nth-child is more capable than it looks.
The argument is a formula, and the keyword forms cover most needs. There is the :nth-child(odd) and the :nth-child(even) for alternating rows, the :nth-child(1) for the first, the :nth-last-child(1) for the last, and the :nth-child(2n+1) for every other one starting at the first.
/* the first three items in a row of five */
.grid > :nth-child(-n+3) { grid-column: span 3; }What can the attribute selectors match?
More than equality.
The operators cover presence, prefixes, suffixes, substrings, and values from a list, and the last one pays off most in real forms.
[type="email"] { ... } /* exact value */
[type^="tel"] { ... } /* value starts with "tel" */
[href$=".pdf"] { ... } /* value ends with ".pdf" */
[class*="button"] { ... } /* value contains "button" */
[lang|="en"] { ... } /* "en" or "en-GB" */Because a page can carry several classes, the [class*="button"] matches an element whose class list contains that word, and that lets you style component variants without adding another attribute.
And the [href^="mailto:"] is a neat way to style email links differently from web links, with no extra class in the markup.
How do you choose a selector?
You start from the most general rule that still works, and you go more specific only when you must.
Styling by tag name is fine for element defaults, like headings and lists.
Styling by class is the right answer for components.
Styling by id is for the one unique element.
And styling by structure, with a selector like the body > div > ul > li > a, is a maintenance trap, because it breaks the moment the markup changes.
/* brittle: tied to the exact nesting of the markup */
body > div > ul > li > a { color: blue; }
/* robust: tied to a class that means something */
.nav-link { color: blue; }There are two rules of thumb that keep a stylesheet maintainable.
You never style by position in the document, and you prefer a class that describes what the thing is over one that describes where it sits.
The .nav-link survives a redesign. The .second-menu-item does not.
Which of the selectors are worth avoiding?
Not all selectors are equal.
A few of them work perfectly and cost you nothing, and others work and then make the next change to the page a fight.
The one to avoid is the long descendant chain. The .page .content .sidebar .card .card__title matches exactly one element on your page, and it will break the moment any part of that structure changes.
The specificity of that selector is also enormous, so it will beat almost everything you write later. That means the only way to override it is an even longer chain or the !important.
The second is the tag-plus-class-plus-attribute combination. The div.card[data-state="open"] is three type-level steps that add nothing the class does not already say.
You write the .card[data-state="open"], and it reads the same and it behaves the same.
The third is styling by position. The div:nth-of-type(3) ties a rule to the order of the markup, so inserting a new element above it silently restyles the wrong thing.
Reach for a class when the rule is about a particular item.
<!-- fragile: the style depends on the order of the markup -->
<ul class="menu">
<li>Home</li>
<li class="is-current">Docs</li>
<li>Support</li>
</ul>/* stable: the class says what it is */
.menu .is-current {
color: #1f5fa9;
font-weight: 600;
}The pattern to aim for is a short, meaningful class on each element you want to style, and a selector that is at most one or two classes deep.
That keeps the specificity low enough that you can still override things later, and it keeps the stylesheet readable to whoever touches the page next, and that includes you in six months.
Quick reference
- The type, class, id, attribute, and combinator selectors cover almost every real stylesheet.
- You use the > to stop styles bleeding into deeper nesting.
- The
h2+ p removes the gap between a heading and the paragraph after it. - The
:nth-childtakes a formula, and the keyword forms cover most cases. - Attribute selectors can match prefixes, suffixes, and substrings, which is useful for component variants.
- You style by class and not by position in the document.
Try it yourself: write a selector that targets every third item in a list, one that targets every link whose address ends in a particular file type, and one that targets only the first child of a container. Use the developer tools to confirm that each one matches what you expected, before you write the rule.