CSS Selectors 101: Targeting Elements with Precision

Search for a command to run...

No comments yet. Be the first to comment.
You open WhatsApp, type a message, and hit send. A single grey tick appears. You're on a train with no signal, but the message went through anyway. Or did it? That single tick is the beginning of a su

You record a Reel, add a song, trim it, and then close the app before posting. When you reopen Instagram, the draft is right there waiting. Nothing was lost. That experience feels simple. The system b

If you have built a React Native app, you have felt the pain of navigation setup. It is easy to get wrong and hard to keep clean as apps grow. This article explains what routing means, why it matters,

Modern apps are not just a pile of screens. They are a living system of features, data flows, offline behavior, and performance constraints. If you have only shipped small apps, the jump to something

Your application has user accounts. Some routes should only be accessible to logged-in users. How do you protect routes and verify that a request actually comes from who they claim to be? JWT (JSON We

Ashish's Blog
57 posts
Imagine you're in a crowded room and need to get someone's attention. You could:
Shout "Hey, everyone wearing blue shirts!" (targeting a group)
Call out "Hey, Sarah!" (targeting one specific person)
Say "Hey, teachers!" (targeting people by role)
CSS selectors work exactly the same way—they're how you tell the browser which HTML elements to style.
You've built the skeleton of your webpage with HTML. Now you want to make it look good with CSS. But here's the challenge: how do you tell CSS which elements to style?
Without selectors, you'd have no way to say things like:
"Make all headings blue"
"Give this specific button a red background"
"Make paragraphs inside the sidebar smaller"
Selectors are the bridge between your HTML and your CSS styles. They let you target elements with precision, from broad sweeps (all paragraphs) to laser-focused targeting (just this one element).
Think of selectors as addresses for your HTML elements. Just like you need an address to send a letter to the right person, you need selectors to send styles to the right elements.
Without selectors:
<!-- You'd have to write inline styles everywhere -->
<p style="color: blue; font-size: 16px;">This is tedious</p>
<p style="color: blue; font-size: 16px;">And repetitive</p>
<p style="color: blue; font-size: 16px;">And hard to maintain</p>
With selectors:
/* Write the style once, apply it everywhere */
p {
color: blue;
font-size: 16px;
}
Much cleaner, right? That's the power of selectors.
The element selector (also called the "type selector") targets all elements of a specific HTML tag.
tagname {
property: value;
}
p {
color: gray;
line-height: 1.6;
}
h1 {
color: navy;
font-size: 32px;
}
h2 {
color: darkblue;
font-size: 24px;
}
a {
color: blue;
text-decoration: underline;
}
Use element selectors when you want to style all instances of an element.
Avoid them when you need different styles for different instances.
The class selector is your most versatile tool.
<p class="intro">This paragraph has a class.</p>
.intro {
font-size: 20px;
font-weight: bold;
}
Reusable
Can be combined
Work on any element
<div class="box shadow rounded">Content here</div>
.box {
padding: 20px;
}
.shadow {
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.rounded {
border-radius: 8px;
}
The ID selector targets one unique element per page.
<div id="header">Site Header</div>
#header {
background-color: navy;
color: white;
padding: 20px;
}
<div id="header">Header section</div>
<div id="main">Main content</div>
<div id="footer">Footer section</div>
Class: reusable, multiple elements -> .classname
ID: unique, one element per page -> #idname
Use classes for reusable styles.
Use IDs for unique sections or JavaScript hooks.
Apply the same styles to multiple selectors.
h1, h2, h3 {
font-weight: bold;
color: #333;
}
Target elements based on where they live in the HTML structure.
.article p {
color: gray;
}
This only styles paragraphs inside .article.
When multiple rules apply, CSS decides based on specificity:
Inline styles > IDs > Classes > Elements
More specific rules win. If specificity is equal, the last rule wins.
<header id="site-header">
<nav>
<a href="#" class="nav-link active">Home</a>
<a href="#" class="nav-link">About</a>
</nav>
</header>
#site-header {
background: #333;
}
.nav-link {
color: white;
}
.nav-link.active {
background-color: #555;
}
Selectors target HTML elements
Classes are reusable and flexible
IDs are unique and specific
Group and descendant selectors reduce repetition
Specificity determines which rule wins
Style one paragraph differently using a class
Style navigation links using descendant selectors
Create reusable card styles with classes
Style header and footer using IDs
Practice regularly, inspect elements using browser dev tools, and experiment with selector combinations.
Selectors are the foundation of CSS. Master them, and styling becomes predictable and powerful.