Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Published
3 min read
CSS Selectors 101: Targeting Elements with Precision
D
CS student focused on backend & OS fundamentals. Building real projects and sharing practical learning.

Selector

in most basic term a selecctor tell the browser which element to apply CSS rule

Example:

h1 {
  color: red;
}

we have

  • h1 → selector

  • color: red; → style rule

Why Selector Are Needed

CSS selector are needed because CSS has to know exactly which HTML elements you want to style. Without selector , CSS would be blind , it wouldnot know where to apply colors , fonts, spacing or layout . but we are taalking of webpaehgs there ar hundreds of elements , para button , image , heading . You dont want to style everythings the same way.

  1. They Connect CSS to HTML

  2. They save time and reduce repetition

  3. They Give You Control and Accuracy

Element Selector

The element selector targets all HTML elements of a specific type.

You directly use the tag name.

Example:

p {
  color: blue;
}

It:
Selects every <p> tag on the page and makes the text blue.

Class Selector

Class selectors target elements that have a specific class attribute.

Syntax: Use a dot (.) before the class name.

Example:

.highlight {
  background-color: yellow;
}

HTML:

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

Result:
Only elements with class="highlight" get styled.

Why classes matter:

  • You can reuse them

  • Apply to multiple elements

  • They give you better control than element selectors

Note : If you want reusable styling → use classes.

ID Selector

ID selectors target one unique element on the page.

Syntax: Use a hash (#) before the ID.

Example:

#header {
  background-color: black;
}

HTML:

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

Note : An ID must be unique. Only one element should have that ID.

Why IDs are risky:

  • Very high specificity

  • Hard to override later

  • Bad for reusable styling

Use IDs only when targeting one special element, like main header or footer.

Group Selectors

Group selectors allow you to apply the same style to multiple selectors at once.

Syntax: Separate selectors using commas.

Example:

h1, h2, p {
  color: green;
}

This means: Apply green color to all h1, h2, and p elements.

Descendant Selector

Descendant selectors target elements inside other elements.

Syntax: Use a space between selectors.

Example:

div p {
  color: red;
}

Select all <p> elements that are inside a <div>.

HTML example:

<div>
  <p>This will be red</p>
</div>

<p>This will NOT be red</p>

Class vs IDs

Basic selector priority

When multiple CSS selectors target the same element, the browser doesn’t guess. It follows a rule called specificity (priority). in simple words some selecctor are stronger than other

priority order

  1. ID selector (#id) → Highest priority

  2. Class selector (.class) → Medium priority

  3. Element selector (p, div) → Lowest priority

Example Scenario

HTML:

<p id="text" class="para">Hello</p>

CSS:

p {
  color: blue;
}

.para {
  color: green;
}

#text {
  color: red;
}

Result: Text will be red.

why CSS selector are foundation of CSS

They are the core mechanism that make css work .

withut this

  1. CSS an nt find element

  2. styles can not be applied

  3. layout can not be controlled