Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101 : Targeting Elements with Precision

Updated
4 min read
CSS Selectors 101 : Targeting Elements with Precision
M

Software Developer

Hey there! If you've ever stared at a webpage and wondered, "How does that button get blue while the others stay gray?" or "Why does only one heading look huge?" — the answer is CSS selectors.

CSS selectors are like magic labels you stick on your HTML elements to say, "Hey browser, apply these styles only to these specific parts!" Without selectors, CSS would be useless — you'd have no way to tell the browser which elements to style.

Think of selectors as ways to address people in a crowded room:

  • "All boys" → element selector

  • "Everyone wearing a red shirt" → class selector

  • "Hey, Rahul in the front row!" → ID selector

Why Do We Even Need CSS Selectors?

Imagine you have a big HTML page with dozens of paragraphs, headings, buttons, and lists. You want:

  • All paragraphs to have gray text

  • Only the important notes to be bold and yellow

  • One special banner at the top to be huge and centered

Selectors let you target exactly what you want without touching everything else. They're the foundation of CSS — everything stylish on the web starts here.

1. Element Selector

This targets all elements of a certain type (tag name).

Syntax: Just write the tag name.

HTML

<!-- HTML -->
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<h1>Main Title</h1>

CSS

/* CSS */
p {
  color: navy;
  font-size: 18px;
}

Before: Plain black text, default size. After: Both paragraphs turn navy blue and bigger — but the <h1> stays untouched.

Use it when you want a style applied everywhere for that tag (like making all links underlined or all lists have square bullets).

2. Class Selector (The Reusable Favorite)

Classes are like sticky notes you can put on any element — and reuse as many times as you want.

Syntax: Starts with a dot . followed by the class name.

HTML

<!-- HTML -->
<p class="highlight">Important note!</p>
<button class="highlight">Click Me</button>
<div class="highlight">Special box</div>

CSS

.highlight {
  background-color: yellow;
  padding: 10px;
  border: 2px solid orange;
}

Before: Normal boring elements. After: All three get a yellow background, padding, and orange border — even though they're different tags!

Classes are super common because they're flexible. Use them for reusable styles like .btn, .card, .error-message.

3. ID Selector (The Unique VIP)

IDs are for one-of-a-kind elements — like a unique name tag.

Syntax: Starts with a hash # followed by the ID name.

HTML

<!-- HTML -->
<header id="main-header">Welcome to My Site</header>

CSS

#main-header {
  background-color: darkblue;
  color: white;
  text-align: center;
  padding: 40px;
  font-size: 48px;
}

Before: Regular header. After: This specific header becomes a big, centered, dark blue banner — nothing else changes.

IDs should be unique on the page (don't reuse them). They're great for one-time things like a navigation bar, footer, or a specific section.

Quick tip: Specificity-wise, ID beats class beats element. So #main-header will override .highlight or header if they conflict.

4. Group Selectors (Styling Multiple Things at Once)

Sometimes you want the same style on different selectors. Instead of repeating the code, group them with commas.

CSS

h1, h2, h3 {
  font-family: Arial, sans-serif;
  color: #333;
}

p, li {
  line-height: 1.6;
}

This applies the same rules to all headings and all paragraphs/lists — keeps your CSS clean and DRY (Don't Repeat Yourself).

5. Descendant Selectors (Targeting Inside Other Elements)

This is where things get powerful: select elements that are inside others.

Syntax: Space between selectors (no comma).

HTML

<!-- HTML -->
<div class="blog-post">
  <h2>Article Title</h2>
  <p>Some text here.</p>
  <p>More text <strong>with emphasis</strong>.</p>
</div>

<p>Normal paragraph outside the post.</p>

CSS

.blog-post p {
  font-style: italic;
  color: green;
}

Before: All paragraphs look normal. After: Only paragraphs inside .blog-post turn italic and green. The outside <p> stays unchanged.

The space means "descendant" — it targets any <p> nested inside .blog-post, even deep down.

Super Quick Priority Recap (High-Level Only)

When styles conflict, the browser uses specificity to decide the winner:

  • ID (#) → Highest priority

  • Class (.) → Medium

  • Element (p, div) → Lowest

More specific wins. Example:

CSS

p { color: blue; }              /* element */
.highlight { color: red; }      /* class — wins over element */
#main-header p { color: purple; } /* descendant with class/ID — even higher */