DEV Community

Rajat Yadav
Rajat Yadav

Posted on

CSS Selectors 101: Targeting Elements with Precision

When you write CSS, you’re always answering one important question:

“Which elements do I actually want to style?”

Because honestly… without a way to point at specific elements in your HTML, you couldn’t change colors, fonts, spacing etc. Everything would just look default and boring.

That’s where CSS selectors comes inn.

Selectors are simply the way you choose which elements get which styles.

We’ll use a simple “addressing people in a room” analogy, and also show small before/after examples so you can actually see what’s happening.


Why CSS Selectors Are Needed

The Basic Problem

Your HTML page has:

  • headings
  • paragraphs
  • buttons
  • links
  • lists
  • sections
  • and many more things

Your CSS needs to say things like:

  • “Make all paragraphs blue.”
  • “Make this button red.”
  • “Make everything inside the sidebar smaller.”

If you had no way to target specific elements, then either:

  • You style everything the same
  • Or you don’t style anything

Selectors are what lets you say:

“Apply these styles to these elements not all of them.”


Real-World Analogy: Addressing People

Imagine a room full of people.

  • “Everyone named John” → selecting by name (like element selector)
  • “Everyone wearing red shirt” → selecting by class
  • “The person with badge #42” → selecting by ID
  • “Everyone named John sitting in the left corner” → selecting by location (descendant selector)

CSS selectors work exactly like this. They are just different ways of addressing elements.


The Structure of a CSS Rule

Every CSS rule has two parts:

selector {
  property: value;
}
Enter fullscreen mode Exit fullscreen mode
  • Selector → which elements
  • Declaration block → what styles to apply

If you don’t understand selectors, you can’t control where styles go. So honestly, selectors are foundation of CSS.


1. Element Selector – Style by Tag Name

What It Does

An element selector (also called type selector) selects every element that matches a given HTML tag.

You just write the tag name. No dot. No hash.

Syntax

elementname {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Example

Select all paragraphs:

p {
  color: blue;
  font-size: 16px;
}
Enter fullscreen mode Exit fullscreen mode

Select all <h1>:

h1 {
  color: darkgreen;
  font-size: 2rem;
}
Enter fullscreen mode Exit fullscreen mode

Now every <p> becomes blue. Every <h1> becomes dark green. You didn’t touch the HTML at all.

When To Use It

Use element selector when you want every instance of that tag to look same.

It’s broad and simple.


2. Class Selector – Style by Class Name

What It Does

A class selector selects every element that has a specific class.

In HTML:

<p class="highlight">Important text</p>
Enter fullscreen mode Exit fullscreen mode

In CSS, you target class using a dot (.).

Syntax

.classname {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

The dot means “this is a class”.

Example

HTML:

<p class="highlight">This is important.</p>
<p>This is normal.</p>
<p class="highlight">This is important too.</p>
Enter fullscreen mode Exit fullscreen mode

CSS:

.highlight {
  background-color: yellow;
  font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode

Now only the paragraphs with class="highlight" get yellow background.

The middle one stays normal.


Multiple Classes

You can do:

class="button primary"
Enter fullscreen mode Exit fullscreen mode

Then in CSS:

.button.primary { }
Enter fullscreen mode Exit fullscreen mode

This means:

Only elements that have BOTH classes.

Or:

.primary { }
Enter fullscreen mode Exit fullscreen mode

This means:

Any element that has class primary.

Classes are flexible. Many elements can share one class.


When To Use Class

Use class when you want to style:

  • A group of elements
  • Reusable components
  • Things like buttons, cards, highlights

Think:

Class = “this type of thing”


3. ID Selector – Style by ID

What It Does

An ID selector selects the one element that has a specific id.

ID should be unique on the page.

In CSS, ID uses hash (#).

Syntax

#idvalue {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Example

HTML:

<header id="main-header">Welcome</header>
Enter fullscreen mode Exit fullscreen mode

CSS:

#main-header {
  background-color: navy;
  color: white;
  padding: 1rem;
}
Enter fullscreen mode Exit fullscreen mode

Only that one header gets styled.


Class vs ID (Simple Table)

Feature Class ID
Symbol . #
Can reuse? Yes No (should be unique)
Use for Group One specific element

Rule of thumb:

  • Use class for reusable styling
  • Use ID for one specific section

4. Group Selectors – Style Multiple Things

Sometimes you want same style for multiple selectors.

Instead of repeating:

h1 { color: #333; }
h2 { color: #333; }
h3 { color: #333; }
Enter fullscreen mode Exit fullscreen mode

You can group:

h1,
h2,
h3 {
  color: #333;
}
Enter fullscreen mode Exit fullscreen mode

Comma means:

Apply to all of these.

Less code. Cleaner CSS. Easier to maintain.

You can mix types too:

h1,
.intro,
#main-title {
  color: navy;
}
Enter fullscreen mode Exit fullscreen mode

5. Descendant Selectors – Style Inside Something

Now things get little more interesting.

A descendant selector targets elements inside another element.

Syntax

ancestor descendant {
  /* styles */
}
Enter fullscreen mode Exit fullscreen mode

Space means:

Inside

Example

HTML:

<article>
  <p>Inside article</p>
</article>

<p>Outside article</p>
Enter fullscreen mode Exit fullscreen mode

CSS:

article p {
  color: blue;
}
Enter fullscreen mode Exit fullscreen mode

Only the paragraph inside <article> becomes blue.

The outside one stays normal.

So now location matters.


Deeper Nesting

header nav ul li a {
  color: white;
}
Enter fullscreen mode Exit fullscreen mode

This means:

  • <a>
  • inside <li>
  • inside <ul>
  • inside <nav>
  • inside <header>

You can be very specific.

But careful — too much nesting can become messy.


Visual Flow (Conceptual)

HTML Element
     ↓
Match ID selector? → Yes → Apply ID styles
     ↓ No
Match Class selector? → Yes → Apply class styles
     ↓ No
Match Element selector? → Yes → Apply element styles
Enter fullscreen mode Exit fullscreen mode

6. Basic Selector Priority (Specificity)

Now important question:

What happens when two rules conflict?

Example:

p { color: blue; }
.highlight { color: red; }
Enter fullscreen mode Exit fullscreen mode

If a paragraph has class highlight, which color wins?

Answer: Class wins.


Strength Order (High Level)

From weakest to strongest:

  1. Element selector
  2. Class selector
  3. ID selector

So:

  • ID > Class
  • Class > Element

Example

HTML:

<p id="intro" class="highlight">Hello</p>
Enter fullscreen mode Exit fullscreen mode

CSS:

p { color: blue; }
.highlight { color: red; }
#intro { color: green; }
Enter fullscreen mode Exit fullscreen mode

Final color?

Green.

Because ID wins.


Same Strength Case

If specificity is equal:

.highlight { color: red; }
.highlight { color: blue; }
Enter fullscreen mode Exit fullscreen mode

The last rule wins.

So order matters too.


Final Complete Example

HTML:

<header id="site-header">
  <h1>My Blog</h1>
  <p class="tagline">Thoughts and code.</p>
</header>

<main>
  <article>
    <h2>First Post</h2>
    <p>Content here.</p>
    <p class="highlight">Key point.</p>
  </article>
</main>
Enter fullscreen mode Exit fullscreen mode

CSS:

p {
  font-size: 1rem;
  color: #333;
}

.highlight {
  background-color: #ffc;
  padding: 0.25rem;
}

#site-header {
  background-color: #333;
  color: white;
  padding: 1rem;
}

#site-header p {
  font-size: 0.9rem;
  color: #ccc;
}

main h1,
main h2 {
  font-family: Georgia, serif;
  color: #222;
}
Enter fullscreen mode Exit fullscreen mode

And now you can see:

  • All paragraphs get base style
  • .highlight gets extra styling
  • Header gets unique style
  • Only paragraphs inside header become smaller
  • Headings inside main get specific font

Everything is controlled by selectors.


Final Thoughts

If you really think about it, CSS is not first about colors or animations or flexbox.

It’s about:

“Who am I styling?”

Selectors are how you answer that.

Once you understand element, class, ID, grouping, and descendant selectors you’ve already understood the core of CSS. Rest things builds on this only.

Happy styling

Top comments (0)