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

How does CSS know what to style?

HTML builds the structure of a webpage.

CSS makes it look awesome.

But here's a big question:

How does CSS know which element to style?

For instance, if your page has lots of paragraphs, headings, and buttons…

How do you tell CSS to:

  • Style just the headings

  • Change one special paragraph

  • Design a specific section

The answer is:

CSS Selectors

Why CSS Selectors Are Needed

A CSS selector is a way to pick HTML elements that you want to style.

Think of selectors like giving directions:

  • “Style all paragraphs”

  • “Style only items with this class”

  • “Style the element with this unique ID”

Without selectors, CSS wouldn't know where to apply styles.

Selectors are the backbone of CSS.

Selectors Analogy (Real-World Example)

Imagine a classroom.

You want to give instructions to students:

  • “All students stand up”

  • “Only students wearing blue shirts stand up”

  • “Only the class monitor stand up”

That's exactly how selectors work:

  • Element selector → everyone

  • Class selector → a group

  • ID selector → one unique person

Element Selector

The element selector picks out all elements of a certain type.

Example:

If you write:

p { color: blue; }

It means:

Style all paragraphs on the page.

Element selectors are the easiest ones to use.

Common examples:

  • h1

  • p

  • div

  • button

Class Selector

Sometimes you don't want to style everything, just a certain group.

That's where class selectors come in handy.

A class selector starts with a dot (.)

Example:

.highlight { background: yellow; }

This styles every element that has:

class="highlight"

Class selectors are great when you want multiple elements to look the same.

ID Selector

An ID selector is for one-of-a-kind elements.

It starts with a hash (#)

Example:

#mainTitle { font-size: 40px; }

This styles the element with:

id="mainTitle"

Important point:

  • A class can be used on many elements

  • An ID should be used on only one element

ID selectors are more specific than class selectors.

Element vs Class vs ID

Element selector:

  • Targets all elements of one type

Class selector:

  • Targets a group of elements

ID selector:

  • Targets one unique element

Group Selectors

Sometimes you want to apply the same style to different selectors. Instead of writing separate rules for each, you can group them together.

Example:

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

This means:

Use the same font style for h1, h2, and p.

Group selectors save time and keep your CSS tidy.

Descendant Selectors

A descendant selector is used to style elements that are inside another element.

Example:

div p { color: red; }

This means:

Only paragraphs inside a div will be red.

Not all paragraphs—just the ones inside that container.

Descendant selectors help you target elements more precisely.

Basic Selector Priority (High-Level)

When different selectors target the same element, CSS follows a simple priority rule:

ID selector is the strongest
Class selector is in the middle
Element selector is the weakest

So:

#id > .class > element

Example:

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

The element will be red because the ID rule is the strongest.

This is called selector specificity.

Before and After Example (Why Selectors Matter)

Without CSS:

All text looks plain.

With selectors:

  • Headings become large

  • Important sections get color

  • Buttons stand out

  • Layout becomes organized

Selectors make this possible.

Why Selectors Are the Foundation of CSS

CSS selectors are the first and most important part of styling.

They let you:

  • Choose what to style

  • Control design precisely

  • Build professional layouts

Once you understand selectors, you're on your way to mastering CSS!stand selectors, CSS becomes much easier.

More from this blog

C

Commit & Push

14 posts