Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Comprehensive CSS Guide to Using Classes and Tags

Published
3 min read
CSS Selectors 101: Targeting Elements with Precision
A

Hi, I’m Abdul Samad. A web development learner and tech enthusiast. I write about what I learn, share practical coding tips, and publish in-depth blogs on programming and modern web development.

Check out my full collection of blogs on Hashnode: https://abdulsamad30.hashnode.dev/

Connect with me on X for quick updates and insights: @abdul_sama60108

1. Why CSS selectors are needed

CSS selectors tell the browser which HTML elements you want to style.

Without selectors, CSS would not know where to apply colors, sizes, spacing, or layouts.

Think of selectors as the bridge between HTML and CSS.

HTML creates elements.

Selectors choose elements.

CSS styles them.

Selectors are the foundation of CSS. If you understand selectors, CSS becomes easy.


2. Explain selectors with a real world analogy

Imagine a classroom.

  • Element selector is like saying:

"All students"

  • Class selector is like saying:

"All students wearing a blue uniform"

  • ID selector is like saying:

"That one student named Ahmed"

CSS works the same way. You are addressing elements.


2.1 Element selector

Selects all elements of a given type.

Example

HTML:

<p>p tag 1</p>
<p>p tag 2</p>

CSS:

p {
color: blue;
}

Result

All <p> elements turn blue.

Use element selectors when you want to style every element of the same type.


2.2 Class selector

Selects elements with a specific class.

Syntax uses a dot .

HTML:

    <p class="highlight">highlight class</p>
    <p>not highlight class</p>

CSS:

.highlight {
color: red;
}

Result

Only elements with class="highlight" turn red.

Important points

• Classes are reusable

• Multiple elements can share the same class


2.3 ID selector

Selects one unique element.

Syntax uses #

HTML:

<p id="main-text">Hello</p>

CSS:

#main-text {
font-size:24px;
background-color: lightblue;
}

Result

Only that one element is affected.

Rules

• IDs must be unique

• Use IDs sparingly

• Mostly used for special or unique elements


2.4 Group selectors

Used when multiple selectors share the same style.

HTML:

    <h1>I am h1</h1>
    <h2>I am h2</h2>
    <p>I am p</p>

CSS:

h1,h2,p {
color: green;
}

Result

All listed elements get the same styling.

This avoids repeated CSS and keeps code clean.


2.5 Descendant selectors

Used to select elements inside other elements.

HTML:

    <div>
        <p>p inside div</p>
    </div>
    <p>p outside div</p>

CSS:

div p {
color: purple;
}

Result

Only the <p> inside the <div> turns purple.

This is powerful for layouts and component based design.


3. Basic selector priority (very high level)

When multiple selectors target the same element, priority matters.

Order from low to high

  1. Element selector

  2. Class selector

  3. ID selector

Example

<p class="text" id="special">p will be green becuase of id #special</p></body>
p {color: blue; }
.text {color: red; }
#special {color: green; }

If an element matches all three, the ID selector wins.

For now, remember

More specific selector = higher priority

3.1 Understand through diagram


4. Class vs ID usage

4.1 Class

4.2 Id