Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Published
3 min read

Why CSS Selectors Matter (And How They Actually Work)

If you’ve ever written CSS and wondered “How does this style know where to go?” — the answer is selectors.

CSS selectors are the bridge between your HTML and your styles. Without them, CSS wouldn’t know what to style, where to apply it, or how to behave when multiple styles compete.

Think of selectors as ways to choose elements. Simple idea. Massive impact.

A Real-World Way to Think About Selectors

Imagine you’re trying to get someone’s attention in a room:

  • You say “Hey, paragraph!” → every paragraph looks up

  • You say “Hey, people wearing blue!” → a specific group responds

  • You say “Hey, Alex!” → one exact person responds

That’s essentially how CSS selectors work.

1. Element Selector – The Broad Approach

The element selector targets all elements of a given type.

p {
  color: blue;
}

This applies to every <p> element on the page.

When to use it:

  • Global styles

  • Base typography

  • Reset or default styling

Before: paragraphs look plain
After: all paragraphs turn blue

It’s simple, powerful, and very broad.

2. Class Selector – The Flexible Favourite

Classes let you style specific groups of elements, regardless of their tag.

.highlight {
  background-color: yellow;
}
<p class="highlight">Important note</p>
<div class="highlight">Warning box</div>

Why classes are great:

  • Reusable

  • Flexible

  • Work across different elements

If CSS had a best friend, it would be the class selector.

3. ID Selector – The One-of-a-Kind Option

IDs are used to target one unique element on the page.

#main-title {
  font-size: 32px;
}
<h1 id="main-title">Welcome</h1>

Important rules:

  • IDs should be unique

  • Use them sparingly

  • They carry more weight than classes

Think of IDs as a full name, not a nickname.

4. Group Selectors – Same Style, Multiple Targets

Sometimes different elements need the same styling.

h1, h2, h3 {
  font-family: Arial, sans-serif;
}

This saves repetition and keeps your CSS cleaner.

5. Descendant Selectors – Styling Based on Location

Descendant selectors target elements inside other elements.

div p {
  color: red;
}

This only affects <p> tags inside a <div>.

Why this matters:

  • Context-based styling

  • Cleaner HTML

  • More control without extra classes

6. Basic Selector Priority (Very High Level)

When multiple selectors target the same element, CSS follows a simple rule:

ID > Class > Element

Example:

p { color: blue; }
.text { color: green; }
#intro { color: red; }
<p id="intro" class="text">Hello</p>

Result: The text is red.

Why? Because IDs have the highest priority.

You don’t need to memorise every specificity rule yet—just remember this order.

Why You Should Care About Selectors

Selectors are the foundation of CSS.

If you understand selectors:

  • Your styles become predictable

  • Your CSS becomes easier to maintain

  • You avoid fighting with “why isn’t this working?”

Before animations, layouts, or fancy effects—selectors come first.

Master them, and CSS starts to feel logical instead of frustrating.