Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Updated
3 min read
CSS Selectors 101: Targeting Elements with Precision
S
Full-stack developer obsessed with performance, scalability, and clean systems. I use Arch btw.

Introduction

When you write CSS, you’re not styling the whole page at once. You’re telling the browser exactly which elements should look a certain way. CSS selectors are the tools that let you point at specific elements and say, “You, change your color”, or “You, add some spacing.”

Think of selectors like addressing people in real life. You could say “Hey, everyone!” (very broad), or “Hey, people wearing red shirts!” (more specific), or “Hey, Rahul!” (very precise).


Why CSS Selectors are Needed?

HTML gives structure to your page:

<h1>Welcome</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

But you use CSS to style your webpage. And without selectors, CSS wouldn’t know which elements to style.

If you write:

p {
    color: blue;
}

You’re telling the browser to select all the <p> elements and make their text blue. Selectors are simply ways to choose elements from your HTML so you can style them.


Types of CSS Selectors

Element Selector

This is the simplest selector. It targets elements by their tag name.

  • HTML:
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
  • CSS:
p {
    color: green;
}

Both paragraphs become green. Use this when you want to style every element of a certain type.

Class Selector

Classes let you target specific groups of elements.

  • HTML:
<p class="highlight">Important text</p>
<p>Normal text</p>
<p class="highlight">Another important text</p>
  • CSS:
.highlight {
    background-color: yellow;
}

Only the paragraphs with class="highlight" get a yellow background. Classes are reusable. Multiple elements can share the same class.

ID Selector

An ID is meant for a single, unique element.

  • HTML:
<h1 id="main-title">My Website</h1>
<h1>Another heading</h1>
  • CSS:
#main-title {
    color: red;
}

Only the first heading turns red. Use IDs when you want to style one unique element.

Group Selectors

Sometimes you want to apply the same style to multiple different elements.

  • CSS:
h1, p {
    font-family: Arial, sans-serif;
}

This selects all <h1> and all <p> elements. Grouping saves repetition and keeps your CSS clean.

Descendant Selectors

These target elements inside other elements.

  • HTML:
<div class="card">
  <p>This is inside the card.</p>
</div>

<p>This is outside the card.</p>
  • CSS:
.card p {
    color: blue;
}

Only the paragraph inside .card becomes blue. The outside paragraph stays unchanged. You’re not selecting all paragraphs, only those that are descendants of .card.


Basic Selector Priority

If multiple selectors target the same element, some have more power than others. A very high level rule is:

ID > Class > Element

Example:

p {
  color: green;
}

.highlight {
  color: blue;
}

#main-text {
  color: red;
}

For:

<p id="main-text" class="highlight">Hello</p>

Final color will be red, since the ID selector has more priority than element and class selectors. This priority system helps CSS decide which style to apply when there’s a conflict.


Conclusion

CSS selectors are how you “address” elements in your webpage. From broad choices like all paragraphs, to precise targets like one unique ID, selectors give you full control over styling. Mastering selectors means mastering the foundation of CSS. Once you can precisely target elements, you can style anything with confidence.