Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Published
3 min read
M

Second-year Computer Science student passionate about becoming a Software Development Engineer. Currently focused on Data Structures & Algorithms in C++, JavaScript, and building strong problem-solving skills. I enjoy learning by building projects and preparing for top tech company interviews.

how CSS actually finds things to style?

First problem: how does CSS know WHAT to style?

Imagine you wrote HTML like this:

<h1>Welcome</h1>
<p>Hello world</p>
<button>Login</button>

Now you write CSS:

color: red;

Browser be like:

ok… but apply to WHAT??

Everything? Only heading? Only button?

CSS needs a way to choose elements.

That choosing system is called:

CSS selectors.

If HTML is structure,
then selectors are:

the targeting system of CSS.

Without selectors → CSS useless.


Think selectors like calling someone in class

Teacher wants to call students.
Different ways:

  • "Everyone" → element selector

  • "All girls" → class selector

  • "Mahi only" → id selector

CSS also works same.
It needs ways to target specific elements.

That’s exactly what selectors do.


1. Element selector (most basic)

Targets all elements of same type.

Example:

p {
  color: blue;
}

Meaning:

all paragraphs become blue

Example HTML:

<p>hello</p>
<p>world</p>

Both will turn blue.

Real life:
Teacher says:

all students stand up

Everyone stands.

Simple.


2. Class selector

Used when we want to style specific group.

HTML:

<p class="redText">Hello</p>
<p>Normal</p>

CSS:

.redText {
  color: red;
}

Only first paragraph becomes red.

Class selector starts with:

dot (.)

You can use same class many times.

Example:

<p class="redText">A</p>
<h1 class="redText">B</h1>
<div class="redText">C</div>

All become red.

Real life:
Teacher says:

all students wearing black shirt stand

Only that group stands.


3. ID selector (for one unique element)

ID is for single element only.

HTML:

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

CSS:

#title {
  color: green;
}

ID selectors starts with : #(hash)

Used for unique elements like:

  • navbar

  • main title

  • footer

Real life:
Teacher says:

Mahi , stand up

Only one person.

Important:

one id should be used only once


Class vs ID (simple difference)

Class:

  • reusable

  • many elements can have same class

ID:

  • unique

  • only one element

Use class most of the time.
Use id rarely.


4. Group selector

Sometimes we want same styling on many elements.

Instead of writing separate CSS:

h1 {color:red}
p {color:red}
button {color:red}

Write:

h1, p, button {
  color: red;
}

This is group selector.
Comma used to group.

Saves time.
Cleaner CSS.


5. Descendant selector (target inside element)

Used when element is inside another element.

Example HTML:

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

CSS:

div p {
  color: blue;
}

Meaning:

paragraph inside div becomes blue

Only inside one.
Not all paragraphs.

Real life:
Teacher says:

students inside this classroom stand

Only those inside stand.


Small idea of selector priority (very basic)

Sometimes multiple CSS apply.
Which one wins?

Simple order:

ID > Class > Element

Example:

#title {color:red}
.heading {color:blue}
h1 {color:green}

If all apply → id wins.

So remember:

id strongest
class medium
element weakest

Don’t go deep now.
Just basic idea enough.


Before vs after using selectors

Without selector:
CSS confused.
Doesn't know where to apply.

With selector:
CSS knows exact target.

So selectors are like:

address of elements.


Why selectors are foundation of CSS

Every styling starts with selector.

No selector → no styling.

When you build website:

  • navbar styling

  • button design

  • layout

  • colors

Everything uses selectors.

So if selectors clear:

CSS becomes easy


summary

Selectors = way to choose elements.

Element selector → all same tags
Class selector → group
ID selector → unique
Group selector → many together
Descendant → inside element

And remember:

CSS cannot work without selectors

Once this clicks, CSS starts feeling fun instead of confusing