Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Published
3 min read
CSS Selectors 101: Targeting Elements with Precision

CSS is not just about colors, fonts, or layouts.

Before you can style anything, you must first select it.

That’s exactly what CSS selectors do.

A selector tells the browser:

“Apply these styles to these elements — not everything.”

Without selectors, CSS has no direction.

Why CSS Selectors Exist

Imagine a room full of people.

If you say:

  • “Everyone sit down” → all respond

  • “People in the first row sit down” → some respond

  • “Amit, sit down” → only one responds

CSS works the same way.

Selectors decide who gets styled.

What a Selector Really Is

A CSS selector is simply an address.

It tells the browser where the style should be applied.

HTML creates the content.
CSS selectors decide which part of that content changes.

Main Types of CSS Selectors

You don’t need to learn everything at once.
Start with these — they cover 90% of real CSS.

1 Element Selector (Broad Target)

Targets all elements of a specific type.

CSS:

p {
  color: green;
}

HTML:

<p>First message</p>
<p>Second message</p>

Both paragraphs turn green.

Think of it as saying:

“All paragraphs, listen.”

Used mainly for base styling.

2 Class Selector (Group Target)

Targets elements with the same class name.

CSS:

.notice {
  background: lightyellow;
}

HTML:

<p class="notice">Warning message</p>
<p>Normal message</p>

Only elements with .notice are styled.

Classes are:

  • reusable

  • flexible

  • most commonly used

Think:

“Everyone wearing the same badge.”

3 ID Selector (Single Target)

Targets one unique element.

CSS:

#page-title {
  font-size: 36px;
}

HTML:

<h1 id="page-title">Dashboard</h1>

IDs must be unique.

Think:

“Only this one person.”

⚠️ Avoid using IDs heavily for styling.

4 Group Selector (Multiple at Once)

Apply the same style to multiple selectors.

CSS:

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

Cleaner CSS. Less repetition.

5 Descendant Selector (Inside Target)

Targets elements inside another element.

CSS:

.card p {
  color: gray;
}

HTML:

<div class="card">
  <p>This changes</p>
</div>

<p>This does not</p>

Think:

“Only people inside this room.”

Selector Priority (Specificity — Simple Version)

Sometimes multiple rules target the same element.

Which one wins?

Priority order (high → low):

  1. !important

  2. Inline styles

  3. ID selectors

  4. Class selectors

  5. Element selectors

Think of it like precision:

Element → wide
Class → narrower
ID → exact

More precise selector wins.

Class vs ID — What You Should Actually Use

Use classes when:

  • styling multiple elements

  • building reusable components

  • writing clean CSS

Example:

<button class="btn primary">Save</button>

This is normal and correct.

Use IDs when:

  • targeting one unique section

  • linking anchors

  • JavaScript needs exact reference

Example:

<section id="checkout"></section>

For styling: classes first, always.

IDs are not evil — misuse is.

Why Selectors Are the Foundation of CSS

Every CSS rule begins with one question:

“Which element am I styling?”

If you can’t answer that clearly:

  • styles collide

  • layouts break

  • CSS becomes chaos

Selectors give CSS structure and discipline.

Final Way to Think About It

If:

  • HTML = structure

  • CSS = design

Then:

Selectors = bridge between them

Master selectors, and CSS stops feeling random.

Everything advanced later — Flexbox, Grid, animations — depends on this foundation.

Get this right now, or you’ll fight CSS forever.