Skip to main content

Command Palette

Search for a command to run...

Mastering CSS Selectors from Scratch

Published
5 min read
Mastering CSS Selectors from Scratch

Before CSS Selectors… Everything Looks the Same

Imagine building a house.

You paint it.

But instead of painting just the doors blue, you accidentally paint the entire house blue — walls, ceiling, furniture, even the dog.

That’s what CSS would feel like without selectors.

CSS selectors are how we say:

“Not everything. Just this one.”

Selectors are the language we use to choose which HTML elements should receive styles.

They are the foundation of CSS.

If HTML is the structure of your house, selectors are the address system that tells CSS exactly where to deliver the paint.

Why CSS Selectors Are Needed

HTML gives us elements:

<h1>Welcome</h1>
<p>Hello world</p>
<p>This is CSS</p>

But CSS needs to know:

  • Which <p>?

  • All <p>?

  • Only the first one?

  • A specific one with a special role?

Selectors answer that question.

They are targeting tools.

Think of selectors like:

  • Calling everyone in a room → element selector

  • Calling people wearing blue shirts → class selector

  • Calling a person by their name → ID selector

1️⃣ Element Selector (Type Selector)

What it does:

Targets all elements of a specific type.

Syntax:

p {
  color: blue;
}

What happens?

Every <p> on the page becomes blue.

When do we use it?

  • When styling global typography

  • Resetting margins

  • Applying consistent base styles

Daily Use Examples:

body { margin: 0; }
h1 { font-size: 2rem; }
a { text-decoration: none; }

Element selectors are broad and powerful — but not precise.

2️⃣ Class Selector (Most Used in Real Projects)

What it does:

Targets elements with a specific class.

Syntax:

.card {
  border: 1px solid #ddd;
}

HTML:

<div class="card">Content</div>

Why classes matter:

Classes are reusable.

Multiple elements can share the same class.

<div class="card"></div>
<div class="card"></div>
<div class="card"></div>

All three will be styled.

When do we use it?

Almost every day.

  • Buttons

  • Cards

  • Layout containers

  • Reusable components

Real-world analogy:

Class selector = “Everyone wearing a red badge.”

3️⃣ ID Selector (Unique Targeting)

What it does:

Targets a single unique element.

Syntax:

#header {
  background: black;
}

HTML:

<div id="header"></div>

Important:

An ID should be used once per page.

When do we use it?

  • Page sections

  • Unique elements

  • JavaScript targeting

  • Anchor links

Real-world analogy:

ID selector = Calling someone by their name.

There should only be one “Rahul” in this exact context.

4️⃣ Group Selector (Styling Multiple Selectors at Once)

Sometimes you want multiple selectors to share styles.

Syntax:

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

Now all headings use the same font.

Why it matters:

  • Reduces repetition

  • Cleaner CSS

  • Easier maintenance

Daily use:

Very common when styling headings or resetting margins.

5️⃣ Descendant Selector (Targeting Inside Something)

This is where CSS becomes powerful.

Syntax:

div p {
  color: red;
}

This means:

Select all <p> inside a <div>.

HTML:

<div>
  <p>This becomes red</p>
</div>

<p>This does not</p>

When do we use it?

Every single day.

Especially in layouts.

Example:

.nav a {
  color: white;
}

This targets links inside navigation.

6️⃣ Child Selector (>)

Targets direct children only.

div > p {
  color: blue;
}

Only <p> that are directly inside <div>.

Not grandchildren.

When do we use it?

When structure matters.

Very common in layout systems.

7️⃣ Universal Selector (*)

Targets everything.

* {
  box-sizing: border-box;
}

Often used in CSS resets.

Use carefully.

8️⃣ Attribute Selector

Targets elements with specific attributes.

input[type="text"] {
  border: 1px solid gray;
}

Very useful in forms.

Daily use:

Common in form styling.

9️⃣ Adjacent Sibling Selector (+)

Selects the element immediately after another.

h1 + p {
  margin-top: 0;
}

Used in typography spacing.

🔟 General Sibling Selector (~)

Selects all siblings after a specific element.

h1 ~ p {
  color: gray;
}

Less common but useful in specific layout cases.

Selector Priority

Not all selectors are equal.

CSS follows a priority system called specificity.

Basic order:

  1. ID selector → strongest

  2. Class selector → medium

  3. Element selector → weakest

Example:

p { color: blue; }
.text { color: green; }
#main { color: red; }

If an element has all three:

It becomes red.

Because ID wins.

Remember:

The more specific the selector, the stronger it is.

The Selectors You’ll Use Daily

In real projects, you mostly use:

  • Class selectors

  • Descendant selectors

  • Element selectors

  • Group selectors

  • Attribute selectors (forms)

ID selectors are used less frequently in modern component-based systems.

Selector Targeting Flow

Think of it like this:

HTML Element

Selector identifies

CSS applies style

Browser renders

Selector = Bridge between HTML and CSS.

Class vs ID (Simple Rule)

ClassID
ReusableUnique
Used dailyUsed occasionally
Good for componentsGood for unique sections

Modern CSS philosophy:

Prefer classes for styling.

Real-World Analogy: Addressing People

Imagine a school:

  • “All students” → element selector

  • “Students in red uniform” → class selector

  • “Roll number 25” → ID selector

  • “Students inside Class 10A” → descendant selector

Selectors are just ways of calling the right group.

Before & After Example

Without Selectors:

Everything looks the same.

With Selectors:

body {
  font-family: Arial;
}

.card {
  padding: 20px;
  border-radius: 8px;
}

#main-title {
  font-size: 2rem;
}

Now structure becomes style.

The Foundation of CSS

Selectors are not advanced CSS.

They are the beginning.

Every animation.
Every layout.
Every hover effect.
Every responsive design.

All of it starts with:

Selecting the right element.

If you understand selectors deeply, CSS becomes predictable.

If you don’t, CSS feels random.

Final Thought

Selectors are precision tools.

They teach you to think:

  • What exactly am I targeting?

  • Is this reusable?

  • Is this too broad?

  • Is this too specific?

Master selectors…

And CSS stops feeling magical.

It starts feeling logical.

More from this blog

From Basics to Binary

13 posts

A learning-focused tech blog documenting my journey from fundamentals to real-world software engineering, covering version control, programming basics, system thinking, and lessons learned.