Skip to main content

Command Palette

Search for a command to run...

CSS Selectors: How We Tell CSS What to Style

Published
3 min read

When you look at a website, everything feels nicely designed — headings look different, buttons stand out, links behave differently.
But behind the scenes, CSS needs clear instructions.

CSS selectors are those instructions.

They tell the browser:

“Hey, apply this style here, not everywhere.”

Without selectors, CSS would be useless because it wouldn’t know which HTML element to style.


Why CSS Selectors Exist

Think about talking in a crowd.

If you shout “Come here!”, everyone may respond.
But if you say “Hey Rahul in the blue shirt”, only one person reacts.

CSS selectors work the same way.

They help us:

  • avoid styling everything

  • target only the elements we want

  • keep designs clean and controlled


Element Selector – Styling by Tag Name

The simplest selector is the element selector.

You directly target an HTML tag.

Example:

p {
  color: blue;
}

This means:

“Make all paragraphs blue.”

You don’t need classes or IDs here.

Use this when:

  • you want a general style

  • all elements of that type should look same


Class Selector – Styling a Group of Elements

Real websites are never that simple.

Sometimes you want:

  • some paragraphs normal

  • some paragraphs highlighted

This is where class selectors help.

Example:

<p class="note">Important note</p>
.note {
  color: red;
}

Only elements with that class get styled.

Why classes are important:

  • reusable

  • flexible

  • works with any element

👉 In real projects, classes are used the most.


ID Selector – Styling One Unique Element

IDs are for one special element.

Example:

<div id="navbar"></div>
#navbar {
  background: black;
}

This style applies to only that one element.

Use IDs when:

  • element appears once

  • header, footer, main container

⚠️ Avoid overusing IDs for styling.


Group Selector – One Style, Many Elements

Sometimes multiple elements need the same style.

Instead of repeating code, we group selectors.

Example:

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

This says:

“Apply this font to all of them.”

Why this is useful:

  • less code

  • cleaner CSS

  • easier maintenance


Descendant Selector – Styling Based on Location

Sometimes styling depends on where an element lives.

Example:

footer a {
  color: gray;
}

This means:

“Style links only if they are inside the footer.”

Links elsewhere stay unchanged.

Use this when:

  • you need context-based styling

  • you don’t want extra classes everywhere


Basic Selector Priority (Very High Level)

What if multiple selectors target the same element?

Browser follows a simple rule:

ID > Class > Element

Example:

p { color: blue; }
.text { color: red; }

A paragraph with class text becomes red, not blue.

More specific selector always wins.


Final Thoughts

CSS selectors are the base of all styling.

Once you understand selectors:

  • CSS feels logical

  • designs become predictable

  • debugging becomes easier

Think of selectors as addresses for styling.
Better address → better design