DEV Community

Cover image for 🎨 Mastering CSS Selectors: How to Target Only What You Want
Rakesh Reddy Peddamallu
Rakesh Reddy Peddamallu

Posted on

🎨 Mastering CSS Selectors: How to Target Only What You Want

Ever wondered why a CSS style you wrote is affecting everything — or nothing at all? That’s because CSS selectors need to be sharp and scoped. Let’s explore how to precisely target elements without unexpected results.


📌 1. Selecting a Child Inside a Parent

Suppose your HTML looks like this:

<div class="card">
  <div class="title">My Blog Post</div>
</div>
Enter fullscreen mode Exit fullscreen mode

You want to style .title only when it’s inside .card. Use:

.card .title {
  font-size: 20px;
}
Enter fullscreen mode Exit fullscreen mode

âś… This applies to .title at any nesting level inside .card.


📏 2. Targeting Only Direct Children

If you want styles to apply only when the .title is directly inside .card (not nested deeper), use the > selector:

.card > .title {
  font-size: 20px;
}
Enter fullscreen mode Exit fullscreen mode

🚫 This won’t apply if .title is wrapped like this:

<div class="card">
  <div>
    <div class="title">Nested Title</div> <!-- ❌ will not be styled -->
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

🚫 3. Avoid Class Combinations That Don’t Exist

You might see this and get confused:

.card.featured .title {
  color: gold;
}
Enter fullscreen mode Exit fullscreen mode

This selector only works when your HTML is:

<div class="card featured">
  <div class="title">Golden Post</div>
</div>
Enter fullscreen mode Exit fullscreen mode

If you don’t have featured in your class list, this style simply won’t apply. No errors, no warnings — just silence.


đź§Ľ 4. Scoping Styles for Specific Sections

When you want to style something only on a specific page or component, create a unique class:

<div class="profile-page">
  <div class="card">
    <div class="title">User Info</div>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Then:

.profile-page .card .title {
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

This keeps your styles clean, predictable, and component-friendly.


đź§Ş 5. Bonus: Use :has() to Style Based on Child Presence (Modern CSS)

If you want to style a parent only if it contains a certain child:

.card:has(.title) {
  border: 1px solid green;
}
Enter fullscreen mode Exit fullscreen mode

⚠️ Works in modern browsers like Chrome and Safari. Not supported everywhere yet.


âś… Cheat Sheet Summary

Goal CSS Selector
Any child inside a parent .parent .child
Only direct child .parent > .child
Parent with multiple classes .parent.special
Scoped styling .section .child
Conditional parent styling .parent:has(.child)

This is your CSS aiming guide — no more accidental global styles or head-scratching "why isn’t it working?" moments. Use the right selector, and your UI will behave like a dream.

Top comments (1)

Collapse
 
teki_solves_fb72717580279 profile image
Teki Solves

Nice post!