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 Explained Simply — How CSS Chooses What to Style

When I first learned CSS, my biggest question was:

👉 “How does CSS know which part of the page to style?”

The answer is simple: Selectors.

CSS selectors are like instructions that tell the browser which HTML elements to target and style.

Let’s understand this step by step.


Why Are CSS Selectors Needed?

Imagine a webpage with:

  • Many paragraphs

  • Multiple buttons

  • Several images

  • Different sections

If you write CSS, the browser needs to know:

👉 Which element should get this color?
👉 Which text should be bigger?
👉 Which box should have a border?

Selectors solve this problem.

They are the way CSS points to specific HTML elements.


Think of Selectors Like Addressing People

Let’s use a real-world example.

Imagine you’re in a classroom.

You can say:

  • “Everyone stand up” → targets all students

  • “Students wearing blue shirts stand up” → targets a group

  • “John, stand up” → targets one person

CSS selectors work exactly like this.


Element Selector (Basic Level)

Element selectors target HTML tag names.

Example:

p {
  color: blue;
}

This means:

👉 Style ALL <p> elements on the page.

Before:

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

After:
Both paragraphs become blue.

This is the most basic selector.


Class Selector (Targeting Groups)

Class selectors are used when you want to style specific elements as a group.

HTML:

<p class="highlight">Text</p>

CSS:

.highlight {
  color: red;
}

Important rule:

👉 Class selector always starts with a dot (.)

Classes are reusable.
You can apply the same class to many elements.

This makes styling flexible and clean.


ID Selector (Targeting One Unique Element)

ID selectors are used when you want to target one unique element.

HTML:

<div id="main-box"></div>

CSS:

#main-box {
  background: yellow;
}

Important rule:

👉 ID selector always starts with #

Also:

  • One ID should be used only once per page

  • IDs are meant for unique sections


Element vs Class vs ID (Quick Summary)

Think of it like this:

  • Element selector → All of one type

  • Class selector → Selected group

  • ID selector → One special element

Order of learning (and usage):

👉 Element → Class → ID


Group Selectors (Styling Multiple Elements Together)

Sometimes you want to style many elements with the same rule.

Instead of writing this:

h1 {
  color: green;
}

p {
  color: green;
}

You can write:

h1, p {
  color: green;
}

This is called a group selector.

It saves time and keeps code clean.


Descendant Selectors (Targeting Inside Elements)

Descendant selectors target elements inside other elements.

HTML:

<div>
  <p>Hello</p>
</div>

CSS:

div p {
  color: purple;
}

This means:

👉 Only <p> tags that are inside <div> will be styled.

It’s like saying:

“Style paragraphs only inside this section.”


Basic Selector Priority (High-Level View)

What if multiple selectors target the same element?

CSS follows a simple priority rule:

👉 ID selector has highest priority
👉 Class selector is next
👉 Element selector has lowest priority

Example:

If all three target the same paragraph:

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

The final color will be:

👉 Red (ID wins)

You don’t need deep theory now — just remember this order.


Why Selectors Are the Foundation of CSS

CSS without selectors is useless.

Selectors are how you:

  • Connect CSS to HTML

  • Control page layout

  • Style components

  • Build responsive designs

If you understand selectors properly, the rest of CSS becomes much easier.