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

What is CSS and Why Do We Even Need It?

Let’s imagine a simple webpage without CSS.

It has text.
It has links.
It technically works.

But it looks… boring.

All text looks the same.
No spacing.
No colors.
No personality.

That’s because HTML only describes structure, not appearance.

This is where CSS comes in.

CSS (Cascading Style Sheets) is the language that controls how a webpage looks.

CSS decides:

  • Colors

  • Fonts

  • Spacing

  • Layout

  • Visual hierarchy

If HTML is the skeleton of a webpage,
CSS is the clothes, posture, and style.


But how does CSS know what to style?

This is the most important question in CSS.

A webpage can have:

  • many paragraphs

  • many headings

  • many buttons

If you write CSS like:

color: red;

The browser will immediately ask:

“Color what exactly?”

That’s why CSS selectors exist.


CSS selectors: choosing who gets styled

Selectors are simply ways to choose HTML elements.

Generated image

They answer the question:

“Which element should these styles apply to?”

Think of selectors like addressing people in a room.


Real-world analogy: addressing people

Imagine you’re in a hall full of people.

  • If you say:
    “Everyone stand up” → very broad

  • If you say:
    “People wearing blue shirts stand up” → more specific

  • If you say:
    “Alex, stand up” → one exact person

CSS selectors work exactly like this.


Element selector: “Everyone of this type”

The element selector targets elements by their tag name.

Example HTML:

<p>This is paragraph one</p>
<p>This is paragraph two</p>

CSS:

p {
  color: blue;
}

What happens?

Before CSS
All paragraphs look default.

After CSS
All paragraphs turn blue.

You’re telling the browser:

“Style every <p> element.”

Element selectors are:

  • simple

  • broad

  • great for base styling

This is like saying:

“All students, pay attention.”


Class selector: “Only those with this label”

Sometimes you don’t want to style everything — only some elements.

That’s where class selectors come in.

HTML:

<p class="highlight">Important message</p>
<p>Normal message</p>

CSS:

.highlight {
  color: red;
  font-weight: bold;
}

What happens?

Before CSS
Both paragraphs look the same.

After CSS
Only the “Important message” stands out.

Classes let you:

  • group elements

  • reuse styles

  • apply meaning visually

Think of a class as a name badge:

“Anyone wearing this badge gets special treatment.”

This is why class selectors are the most commonly used in real projects.


ID selector: “That one specific element”

Sometimes, you want to style only one unique element.

That’s when ID selectors are useful.

HTML:

<div id="header">Welcome</div>

CSS:

#header {
  background-color: black;
  color: white;
}

You’re telling the browser:

“Style the element whose ID is header.”

Important idea:

  • IDs should be unique

  • One ID = one element

ID selectors are like calling someone by name:

“Alex, please come here.”


Before & After: seeing selectors in action

HTML (before styling)

<h1>My Website</h1>
<p>Hello world</p>
<p class="highlight">Read this first</p>

CSS

h1 {
  color: purple;
}

p {
  color: gray;
}

.highlight {
  color: red;
}

Result

  • Heading turns purple

  • All paragraphs become gray

  • Highlighted paragraph becomes red

This is CSS selectors doing their job — choosing who gets what.


Why selectors are the foundation of CSS

Without selectors:

  • CSS wouldn’t know where to apply styles

  • Everything would either be styled or not styled

  • No control, no structure

Selectors give CSS precision.

They allow you to:

  • style globally or locally

  • reuse designs

  • build scalable layouts

  • avoid chaos

Before learning animations, layouts, or frameworks, selectors must make sense.


Final thought

CSS is not about memorizing properties.

It’s about understanding selection.

Once you know:

  • how elements are chosen

  • how styles are applied

CSS stops feeling confusing and starts feeling logical.

And that’s the foundation everything else is built on.