Skip to main content

Command Palette

Search for a command to run...

CSS Selectors Explained and How CSS Chooses HTML Elements.

A beginner-friendly guide to element, class, ID, and other essential CSS selectors

Published
3 min read
CSS Selectors Explained and How CSS Chooses HTML Elements.
M

Hello, I’m Asim, a passionate Full Stack Developer with 1 year of hands-on experience in building scalable and user-friendly web applications. I work across both frontend and backend, enjoy solving real-world problems, and focus on writing clean, efficient, and maintainable code. I’m always eager to learn new technologies and grow as a developer.

Introduction Why CSS Selectors Are Needed :

CSS selectors are the foundation of styling on the web.

HTML creates the structure of a webpage, but CSS decides how things look. The big question is:

How does CSS know which HTML element to style?

That’s where CSS selectors come in. Selectors are simply ways to choose HTML elements so styles can be applied to them.

Think of selectors as instructions that tell CSS: “Apply these styles to these elements.”


Real world Analogy Addressing People .

Imagine you want to give instructions:

  • Everyone in the room stand up → Element selector

  • People wearing blue shirts stand up → Class selector

  • Asim, you stand up → ID selector

CSS works exactly the same way.


Element Selector .

The element selector targets HTML elements by their tag name.

Example :

p {
  color: blue;
}

This means: Style all <p> elements on the page.

When to Use :

  • When you want to apply styles globally.

  • For basic typography like headings and paragraphs.


Class Selector .

A class selector targets elements using the class attribute.

Class selectors start with a dot (.)

Example :

.highlight {
  background-color: yellow;
}
<p class="highlight">Important text</p>

This styles only elements with class="highlight".

Key Points

  • Classes can be reused.

  • One class can be applied to many elements.

  • Most commonly used selector in real projects.


ID Selector .

The ID selector targets a single unique element.

ID selectors start with a hash (#)

Example :

#main-title {
  font-size: 32px;
}
<h1 id="main-title">Welcome</h1>

Rules :

  • IDs must be unique.

  • Use IDs sparingly.

  • Often used for layout or JavaScript hooks.


Group Selector .

Group selectors allow you to apply the same styles to multiple selectors.

Example :

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

This avoids repeating code and keeps CSS clean.


Descendant Selector .

Descendant selectors target elements inside other elements.

Example :

div p {
  color: red;
}

This means: Style only <p> elements that are inside a <div> .


Basic Selector Priority .

Not all selectors are equal.

Very simple rule: ID > Class > Element

Example:

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

The element with id=”uniquq” will be red, even if it’s also a class or paragraph.

This is called specificity (you’ll learn it deeply later).


Selector Targeting Flow .


Why Selectors Matter So Much .

CSS selectors:

  • Decide what gets styled.

  • Control design consistency.

  • Are the base of responsive and scalable CSS.

  • Are used everywhere (CSS, frameworks, JavaScript).

Without selectors CSS cannot exist.


Practice Tip Inspect in Browser .

Open any website → Right click → Inspect

You’ll see:

  • HTML structure.

  • Classes and IDs.

  • CSS selectors in action.

This is the best way to learn CSS.


CSS selectors may look simple, but they power every design decision on the web.

Once you understand:

  • Element selectors.

  • Class selectors.

  • ID selectors

  • How CSS chooses elements.

You unlock the real power of CSS. And remember:

Selectors are not decoration they are the logic of CSS.