Skip to main content

Command Palette

Search for a command to run...

Css Selectors

Published
•3 min read•View as Markdown
Css Selectors

Why CSS Selectors Are Needed

Let’s start with a basic question:

How does CSS know what to style?

HTML gives us structure, but CSS needs a way to point to specific elements and say:

“Style this part.”

CSS selectors are simply ways to choose elements from the HTML so styles can be applied to them.

Without selectors, CSS would have no direction—it wouldn’t know where to work.


Selectors as a Real-World Analogy

Imagine we are in a classroom.

  • Saying “Students” → everyone stands up

  • Saying “Students wearing blue shirts” → only some stand up

  • Saying “Ayush” → only one person responds

CSS selectors work the same way.
They let us target elements with different levels of precision.


Element Selector

What It Does

An element selector targets all elements of a given type.

Example

HTML:

<p>This is a paragraph</p>
<p>This is another paragraph</p>

CSS:

p {
  color: blue;
}

What Happens

All <p> elements turn blue.

When We Use It

  • When we want to style every instance of an element

  • Headings, paragraphs, lists, etc.

This is the broadest selector.


Class Selector

Why Classes Exist

Sometimes we don’t want to style all elements—only a group we choose.

That’s where class selectors come in.

Example

HTML:

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

CSS:

.highlight {
  color: red;
}

What Happens

Only elements with class="highlight" turn red.

Key Points

  • Classes can be used multiple times

  • Best for reusable styling

Think of a class as:

“People wearing the same uniform”


ID Selector

What Makes IDs Special

An ID selector targets one unique element.

Example

HTML:

<h1 id="main-title">Welcome</h1>

CSS:

#main-title {
  font-size: 32px;
}

What Happens

Only that specific heading is styled.

Key Points

  • IDs should be unique

  • Used for special or single elements

This is like calling one person by name.


Order So Far (Broad → Specific)

  • Element selector → everyone

  • Class selector → selected group

  • ID selector → one unique element

This order matters for understanding priority.


Group Selectors

Why Group Selectors Are Needed

Often, different elements need the same style.

Instead of repeating CSS, we group them.

Example

CSS:

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

What Happens

All h1, h2, and p elements get the same font.

Group selectors help us:

  • Reduce repetition

  • Keep CSS clean


Descendant Selectors

What They Do

Descendant selectors target elements inside other elements.

Example

HTML:

<div class="card">
  <p>Card text</p>
</div>

CSS:

.card p {
  color: green;
}

What Happens

Only <p> tags inside .card turn green.

Analogy

It’s like saying:

“Students inside Classroom A”

Context matters here.


Basic Selector Priority (Very High Level)

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

More specific selector wins

At a very high level:

  1. ID selector (strongest)

  2. Class selector

  3. Element selector (weakest)

We don’t need formulas yet—just remember:
specific > general


Before & After Example

Before CSS

<p>Hello World</p>

Looks plain.

After CSS

p {
  color: purple;
  font-size: 18px;
}

Now it has style and personality.

This transformation is powered entirely by selectors.


Why Selectors Are the Foundation of CSS

Selectors are the entry point of CSS.

Without them:

  • No styling logic

  • No control

  • No structure in design

Once we understand selectors well:

  • Layout becomes easier

  • Debugging becomes logical

  • CSS stops feeling random