DEV Community

Cover image for CSS Selectors 101: Targeting Elements with Precision
Debashis Das
Debashis Das

Posted on

CSS Selectors 101: Targeting Elements with Precision

When you start learning CSS, one question comes up very early is that, “How does CSS know what to style?”
That’s where CSS selectors come in.

Selectors are the foundation of CSS. If HTML is the skeleton of a webpage, then CSS selectors are the eyes—they help CSS find the exact elements to style.

Let’s understand this step by step, without overcomplicating things.

1. Why CSS Selectors Are Needed

Imagine a webpage with:

  • headings
  • paragraphs
  • buttons
  • images

CSS needs a way to say:

“Style this paragraph, but not that one.”

Selectors are simply ways to choose HTML elements.

Without selectors, CSS would have no idea where to apply styles.


2. Think of Selectors Like Addressing People

Real-world analogy time

  • Element selector → calling everyone with the same role

    “All students, stand up.”

  • Class selector → calling a group

    “Students in Section A, stand up.”

  • ID selector → calling one specific person

    “Rahul, stand up.”

CSS works the same way.


3. Element Selector

The element selector targets all elements of a specific type.
Example:


p {
  color: blue;
}

Enter fullscreen mode Exit fullscreen mode

This means:

"Styles all <p> elements on the page."

Before:

<p>Hello World</p>
<p>Learning CSS</p>

Enter fullscreen mode Exit fullscreen mode

After:

Both paragraphs turn blue

  • Simple
  • Useful for global styling
  • Not very specific

4. Class Selector

A class selector targets elements with a specific class.

syntax:

.box {
  border: 1px solid black;
}
Enter fullscreen mode Exit fullscreen mode

html:

<div class="box">Learning  css</div>
<div class="box">Hello world</div>

Enter fullscreen mode Exit fullscreen mode

result:


This means:

“Style only elements with class box.”

✅ Reusable
✅ Most commonly used
✅ Can be applied to multiple elements


5. ID Selector

An ID selector targets one unique element.

syntax:


#main-title {
  font-size: 32px;
}
Enter fullscreen mode Exit fullscreen mode

HTML:

<h1 id="main-title">Welcome</h1>

Enter fullscreen mode Exit fullscreen mode

result:

Important rules:

  • IDs should be unique
  • One ID = one element only

Use IDs when something is special and one-of-a-kind.


6. Group Selectors

Group selectors allow you to apply the same style to multiple selectors.

example:


h1, h2, p {
  font-family: Arial;
}

Enter fullscreen mode Exit fullscreen mode

This means:

“Apply this style to h1, h2, and p together.”

This helps reduce repetition and keeps CSS clean.


7. Descendant Selectors

A descendant selector targets elements inside another element.

example:


div p {
  color: green;
}

Enter fullscreen mode Exit fullscreen mode

HTML:


<div>
  <p>This will be green</p>
</div>

<p>This will NOT be green</p>

Enter fullscreen mode Exit fullscreen mode

result:

Meaning:

“Select all <p> elements inside a <div>.”

This is very useful for structured layouts.


8. Basic Selector Priority (High-Level)

Sometimes multiple selectors target the same element.
CSS then follows a simple priority rule:

ID  >  Class  >  Element
Enter fullscreen mode Exit fullscreen mode

example:

p { color: blue; }
.text { color: green; }
#special { color: red; }

Enter fullscreen mode Exit fullscreen mode

HTML:

<p class="text" id="special">Hello</p>

Enter fullscreen mode Exit fullscreen mode

Result:

Final color: red (that mean - ID wins)

don’t stress too much now—just remember this order.


Diagram 1: Selector Targeting Flow


HTML Element
     ↓
Element Selector
     ↓
Class Selector
     ↓
ID Selector (most specific)

Enter fullscreen mode Exit fullscreen mode

Diagram 2: Class vs ID Usage


Class (.card) → Many elements
ID (#header) → One element only

Enter fullscreen mode Exit fullscreen mode

Final Thoughts

CSS selectors may look small, but they are extremely powerful.

If you understand:

  • element selectors
  • class selectors
  • id selectors
  • how targeting works

You already understand the core of CSS.

Everything else—layouts, animations, responsiveness—builds on this, and take it slow, practice a lot, and inspect real websites.
That’s how CSS really clicks.

Top comments (0)