CSS Selectors 101: Targeting Elements with Precision

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
Let’s imagine a simple webpage without CSS.
It has text.
It has links.
It technically works.
But it looks… boring.
All text looks the same.
No spacing.
No colors.
No personality.
That’s because HTML only describes structure, not appearance.
This is where CSS comes in.
CSS (Cascading Style Sheets) is the language that controls how a webpage looks.
CSS decides:
Colors
Fonts
Spacing
Layout
Visual hierarchy
If HTML is the skeleton of a webpage,
CSS is the clothes, posture, and style.
This is the most important question in CSS.
A webpage can have:
many paragraphs
many headings
many buttons
If you write CSS like:
color: red;
The browser will immediately ask:
“Color what exactly?”
That’s why CSS selectors exist.
Selectors are simply ways to choose HTML elements.

They answer the question:
“Which element should these styles apply to?”
Think of selectors like addressing people in a room.
Imagine you’re in a hall full of people.
If you say:
“Everyone stand up” → very broad
If you say:
“People wearing blue shirts stand up” → more specific
If you say:
“Alex, stand up” → one exact person
CSS selectors work exactly like this.
The element selector targets elements by their tag name.
Example HTML:
<p>This is paragraph one</p>
<p>This is paragraph two</p>
CSS:
p {
color: blue;
}
Before CSS
All paragraphs look default.
After CSS
All paragraphs turn blue.
You’re telling the browser:
“Style every
<p>element.”
Element selectors are:
simple
broad
great for base styling
This is like saying:
“All students, pay attention.”
Sometimes you don’t want to style everything — only some elements.
That’s where class selectors come in.
HTML:
<p class="highlight">Important message</p>
<p>Normal message</p>
CSS:
.highlight {
color: red;
font-weight: bold;
}
Before CSS
Both paragraphs look the same.
After CSS
Only the “Important message” stands out.
Classes let you:
group elements
reuse styles
apply meaning visually
Think of a class as a name badge:
“Anyone wearing this badge gets special treatment.”
This is why class selectors are the most commonly used in real projects.
Sometimes, you want to style only one unique element.
That’s when ID selectors are useful.
HTML:
<div id="header">Welcome</div>
CSS:
#header {
background-color: black;
color: white;
}
You’re telling the browser:
“Style the element whose ID is header.”
Important idea:
IDs should be unique
One ID = one element
ID selectors are like calling someone by name:
“Alex, please come here.”
<h1>My Website</h1>
<p>Hello world</p>
<p class="highlight">Read this first</p>
h1 {
color: purple;
}
p {
color: gray;
}
.highlight {
color: red;
}
Heading turns purple
All paragraphs become gray
Highlighted paragraph becomes red
This is CSS selectors doing their job — choosing who gets what.
Without selectors:
CSS wouldn’t know where to apply styles
Everything would either be styled or not styled
No control, no structure
Selectors give CSS precision.
They allow you to:
style globally or locally
reuse designs
build scalable layouts
avoid chaos
Before learning animations, layouts, or frameworks, selectors must make sense.
CSS is not about memorizing properties.
It’s about understanding selection.
Once you know:
how elements are chosen
how styles are applied
CSS stops feeling confusing and starts feeling logical.
And that’s the foundation everything else is built on.