Skip to main content

CSS Selectors 101: Targeting Elements with Precision

Element, class, ID, group and descendant selectors, and which rule wins when they clash

Updated 7 min readMarkdown
CSS Selectors 101: Targeting Elements with Precision

These two CSS rules differ by one character:

.card, p { color: crimson; }
.card p { color: crimson; }

The first turns every card and every paragraph on the page red. The second turns red only the paragraphs that sit inside a card. Same words, one comma versus one space, completely different result.

That's what CSS selectors are about: pointing at exactly the elements you mean, and no others. This post covers the five you'll use every day, in the order that makes sense to learn them, and ends with how the browser decides which rule wins when two of them disagree.

Why CSS needs selectors

A CSS rule has two halves: which elements to style, and how to style them.

h1 {
  color: navy;
}

h1 is the selector: the "which". color: navy; is the declaration: the "how". Without a selector, the browser would have a style and nowhere to put it.

Think of a selector as the way you address people in a crowded room:

  • "Everyone, please sit down" → every element of one type
  • "Everyone wearing a blue badge, come here" → every element with a class
  • "Riya, seat 14, you've won" → exactly one element with an ID

The more specific the address, the fewer people it reaches. Hold on to that idea; it comes back at the end.

Element selector: "everyone named p"

The simplest selector is a tag name. It matches every element of that type on the page.

<h1>Our menu</h1>
<p>Fresh coffee every morning.</p>
<p>Open until 9 pm.</p>
p {
  color: gray;
  font-size: 18px;
}

Before: both paragraphs use the browser's default black text. After: both turn gray and slightly larger. The h1 is untouched, because it isn't a p.

Element selectors are good for setting defaults across a whole site. They're too blunt when you want one paragraph to look different from the rest. That's what classes are for.

Class selector: "everyone wearing this badge"

A class is a label you add to elements yourself, with the class attribute. In CSS you select it with a dot.

<p>Fresh coffee every morning.</p>
<p class="highlight">Today only: free refills.</p>
<p>Open until 9 pm.</p>
.highlight {
  background-color: lightyellow;
  font-weight: bold;
}

Before: three identical paragraphs. After: only the middle one gets a yellow background and bold text.

Two things make classes the workhorse of CSS:

  • Reusable. Put class="highlight" on ten elements, of any type, and all ten get the style.
  • Combinable. An element can wear several badges at once: class="highlight large". Each class brings its own styles.

ID selector: "one specific person"

An ID names exactly one element on the page. In CSS you select it with a #.

<header id="site-header">Corner Café</header>
#site-header {
  background-color: black;
  color: white;
  padding: 16px;
}

Before: plain text at the top of the page. After: a black bar with white text.

The rule that matters: an ID must be unique on the page. Two elements with id="site-header" is invalid HTML, even though the browser will usually style both without complaining.

How to choose between them:

Decision flow for choosing a class or an ID: if a style could apply to more than one element, use a class, which is reusable and easy to override; use an ID only for exactly one element, knowing it is hard to override Class vs ID. When in doubt, use a class.

My recommendation: style with classes almost always. An ID's real jobs are links to a section of the page (href="#contact"), connecting a <label> to its input, and JavaScript lookups. The reason to avoid IDs for styling comes up in the priority section.

Group selector: the comma means "and these too"

When several selectors need the same styles, list them with commas instead of repeating the rule.

/* ❌ The same three lines, three times */
h1 { font-family: Georgia, serif; }
h2 { font-family: Georgia, serif; }
h3 { font-family: Georgia, serif; }
/* ✅ One rule, three selectors */
h1, h2, h3 {
  font-family: Georgia, serif;
}

The result is identical; the second is easier to change. Read the comma as "and": h1, and h2, and h3. Each part of the group is a completely separate selector. That's why .card, p from the opening hits every paragraph on the page: the p part doesn't care where it is.

Descendant selector: the space means "inside"

A space between two selectors means "the second, when it's somewhere inside the first".

<div class="card">
  <p>Inside the card.</p>
  <section>
    <p>Deeper inside the card.</p>
  </section>
</div>
<p>Outside the card.</p>
.card p {
  font-weight: bold;
}

Result: both paragraphs in the card turn bold, including the one nested inside <section>. The paragraph outside the card stays normal.

"Inside" means at any depth, not only the direct children. Children, grandchildren, further down: all count. If you've read about the DOM tree, this is the selector walking down that tree. (I covered the tree in HTML tags vs elements.)

Now the opening example makes sense:

Selector Read it as Matches
.card, p every .card and every p cards, plus all paragraphs on the page
.card p every p inside a .card only paragraphs within cards

When two rules disagree: priority

Sooner or later, two rules will try to style the same element differently:

<p id="intro" class="note">Welcome!</p>
#intro { color: red; }
.note  { color: green; }
p      { color: blue; }

The text is red. At a very high level, the browser ranks selectors like this:

  1. ID beats class, and class beats element.
  2. If two rules are equally specific, the later one in the CSS wins.

In the addressing analogy: "Riya, seat 14" outranks "everyone with a blue badge", which outranks "everyone".

The part that surprises people is that the ranking goes by category first, not by counting. I tested this in Chrome 157: an element styled by #hero and also by a selector with ten classes chained together (.a.b.c.d.e.f.g.h.i.j) still took the #hero colour. Ten classes don't add up to one ID.

That's the reason to keep IDs out of your styling. Once an ID sets a colour, the only way to change it later is another ID-level selector (or !important, which creates its own mess). Classes keep your styles easy to override.

The real rules (called specificity) have more detail than this, and also involve inline styles and !important. For a beginner, "ID > class > element, and later wins a tie" explains almost every clash you'll hit.

How the browser matches a selector

Putting it together, here's what happens to every rule in your stylesheet:

How the browser applies a CSS rule: it takes the selector, checks each element in the DOM, skips non-matches, and when two rules set the same property the higher priority wins, ID then class then element, with the later rule breaking a tie How a selector turns into styles on the page.

One detail that's useful to know: browser engines generally check a selector like .card p from the right. They find each p first, then look up its ancestors for a .card. You don't need to write CSS any differently because of it, but it explains why the rightmost part of a selector is called the "key".

Selectors are the foundation the rest of CSS sits on. Every colour, margin and layout rule you'll ever write starts with one, and if the selector points at the wrong elements, nothing after it can fix that.

The limit of this post is that it stops at the basics. Pseudo-classes like :hover, attribute selectors and the child combinator > all build on exactly these five, and they're worth learning next.

Try this in DevTools: inspect any element, look at the Styles panel, and find a rule that's crossed out. Which selector beat it, and was it an ID, a class or simply a later rule?

No comments yet. Be the first to comment.