Skip to main content

Command Palette

Search for a command to run...

CSS Selectors

Published
•4 min read•View as Markdown

CSS Selectors Explained Simply (With Real-World Examples)

When you write CSS, you’re not styling the whole page at once.

You’re saying:

“Hey browser, style this element, not everything.”

That’s exactly what CSS selectors are for.


Why CSS Selectors Are Needed

Imagine a webpage with hundreds of elements:

  • Headings

  • Paragraphs

  • Buttons

  • Images

  • Forms

Without selectors, CSS would be useless — you wouldn’t know which element to style.

👉 Selectors are how CSS chooses elements.

Think of selectors as addresses.


Real-World Analogy: Addressing People

Imagine a classroom 👨‍🏫

Way of addressingMeaning
“Students”Everyone
“Students wearing blue shirts”A group
“Vaibhav”One specific person

CSS works the same way.


1️⃣ Element Selector (Most General)

What it selects

All elements of a given type.

Example

p {
  color: blue;
}

What happens

All <p> tags become blue.

HTML

<p>Hello</p>
<p>World</p>

✅ Use when

You want to style all elements of the same type.


2️⃣ Class Selector (Group of Elements)

What it selects

Elements with a specific class name.

Syntax

.highlight {
  background-color: yellow;
}

HTML

<p class="highlight">Important</p>
<p>Normal</p>
<p class="highlight">Very Important</p>

Only the elements with class="highlight" are styled.

✅ Use when

  • Multiple elements need the same style

  • Most common selector in real projects


3️⃣ ID Selector (One Unique Element)

What it selects

A single unique element.

Syntax

#header {
  font-size: 32px;
}

HTML

<h1 id="header">Welcome</h1>

⚠️ IDs should be unique on a page.

✅ Use when

  • Styling one specific element

  • Targeting layout sections


Element → Class → ID (In That Order)

This is the recommended learning and usage order:

  1. Element selector → broad

  2. Class selector → grouped

  3. ID selector → specific


4️⃣ Group Selector (Apply Same Style to Many)

What it selects

Multiple selectors at once.

Syntax

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

Meaning

All h1, h2, and p elements get the same font.

✅ Use when

Different elements share the same styling rules.


5️⃣ Descendant Selector (Elements Inside Other Elements)

What it selects

Elements inside other elements.

Syntax

div p {
  color: red;
}

HTML

<div>
  <p>This turns red</p>
</div>

<p>This stays normal</p>

Only <p> inside <div> is styled.

✅ Use when

You want styles based on structure, not class names.


Before & After Styling Example

Before CSS

<p>Plain text</p>
<p class="note">Important note</p>

CSS

p {
  color: gray;
}

.note {
  color: red;
  font-weight: bold;
}

After

  • All text → gray

  • Important note → red & bold

✨ This shows how selectors control who gets styled.


Basic Selector Priority (Very High Level)

When multiple selectors apply to the same element:

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

Priority Order (Simple Rule)

ID > Class > Element

So the browser chooses:

  1. #id first

  2. .class next

  3. element last

👉 More specific selector wins.

(No deep specificity math for now — this is enough to start.)


CSS Selectors Are the Foundation of CSS

Everything in CSS builds on selectors:

  • Layout

  • Responsive design

  • Animations

  • Frameworks like Tailwind, Bootstrap

If you understand selectors, CSS stops feeling like magic.


Diagram: Selector Targeting Flow


Diagram: Class vs ID Usage


Key Takeaways

  • CSS selectors choose elements

  • Element → Class → ID (learn in this order)

  • Classes are reusable, IDs are unique

  • Selectors are the core building blocks of CSS.