Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Updated
4 min read
CSS Selectors 101: Targeting Elements with Precision
S

Hi, I’m Swastisunder 👋 I’m a full-stack web developer focused on building clean, modern, and practical web applications. I work mainly with the MERN stack (MongoDB, Express, React, Node.js) and enjoy turning ideas into real, usable products. Right now, I’m also learning Python and AI/ML, one concept and one project at a time. Always learning. Always building.

When we learn HTML, we create elements.

Like:

<h1>Hello</h1>
<p>Welcome</p>
<button>Click Me</button>

But what if we want only heading become blue?

Or only button become red?

How CSS know which element we want to style?

This is where CSS Selectors come in.

Selectors help us choose elements.

You can think selector like address of a house.

If you want deliver a parcel, you need correct address.

Same way CSS need selector to know where styling should go.


Why CSS Selectors are Needed

Imagine a webpage have:

  • 10 headings

  • 20 paragraphs

  • 5 buttons

Now you want style only one button.

Without selectors CSS will not know which button you talking about.

Selectors help CSS find exact element.

Then CSS apply styles only there.

So selectors are foundation of CSS.

Without selectors CSS is almost useless.


Element Selector

Element selector target all elements of same type.

Example:

p{
    color: blue;
}

HTML:

<p>First Paragraph</p>
<p>Second Paragraph</p>

Output:

Both paragraphs become blue.

Because selector target every p tag.

This is simplest selector.


Class Selector

Class selector is one of most used selector in CSS.

Class start with dot .

HTML:

<p class="text">Hello</p>

CSS:

.text{
    color:red;
}

Output:

Only element having class text become red.

You can use same class on many elements.

Example:

<p class="text">Para</p>
<h1 class="text">Heading</h1>
<div class="text">Box</div>

All of them get same style.

This is why classes are used a lot.


ID Selector

ID selector start with #

HTML:

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

CSS:

#title{
    color:green;
}

Output:

Only that element become green.

Unlike class, ID should be unique.

Normally one page should not have same ID multiple times.

Think ID like Aadhaar number.

Every person have different Aadhaar.

Same idea here.


Group Selector

Sometimes multiple elements need same style.

Instead of writing CSS again and again, we can group selectors.

Example:

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

Now:

  • h1

  • p

  • button

all get same font.

This save time and reduce repeated code.


Descendant Selector

Sometimes we want target element inside another element.

This is called descendant selector.

HTML:

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

CSS:

div p{
    color:blue;
}

Meaning:

Find every p inside div.

Output:

Paragraph become blue.

Think like this:

House Room Chair

If we say:

House Chair

We mean chair inside house.

Same logic work in CSS.


Selector Priority

Sometimes multiple selectors target same element.

Example:

<p class="text" id="title">
Hello
</p>

CSS:

p{
    color:blue;
}

.text{
    color:red;
}

#title{
    color:green;
}

Now which color will apply?

Green.

Because ID selector have higher priority than class selector and element selector.

Basic order:

ID > Class > Element

No need go deep now.

Just remember this simple rule.


Real Life Analogy

Imagine you are in a classroom.

Teacher says:

"All students stand up."

This is like element selector.

Then teacher says:

"All students from Section A stand up."

This is like class selector.

Then teacher says:

"Rahul stand up."

This is like ID selector.

As selector become more specific, fewer people get selected.

Same thing happen in CSS.


Common Selectors Summary

Element Selector:

p{}

Class Selector:

.text{}

ID Selector:

#title{}

Group Selector:

h1,p,button{}

Descendant Selector:

div p{}

One Important Thing

Most beginners focus on fancy CSS properties.

But selectors are actually more important.

Because before styling anything, CSS must first find the correct element.

If selectors are wrong, styling will never work.

So spend some time practicing selectors.

It will make learning CSS much easier.


Conclusion

CSS selectors are used to target HTML elements.

We learned:

  • Element selector

  • Class selector

  • ID selector

  • Group selector

  • Descendant selector

  • Basic selector priority

Once you understand selectors, writing CSS become much easier because now you know exactly how to choose the elements you want to style.

Selectors may look small, but they are one of the most important concepts in CSS.

More from this blog

W

WebDev 2026

13 posts

A developer blog where I share web development, networking, JavaScript, Git, Node.js and computer science concepts in simple and beginner friendly way. Some blogs are technical deep dives, some are story based explanations, and some are part of my learning journey as a developer. This blog is basically my public notebook where I learn, build, experiment and explain things in the simplest way possible.