Skip to main content

Command Palette

Search for a command to run...

CSS Selectors – Targeting HTML Elements

Updated
3 min readView as Markdown

Yesterday, I began my CSS journey by learning what CSS is, why it's important, and the different ways to apply it.

Today, I explored one of the most fundamental concepts in CSS: Selectors.

Before CSS can style an element, it needs a way to identify which element should receive the style. That's exactly what selectors do.

Let's dive in.


What is a CSS Selector?

A CSS selector is a pattern used to select one or more HTML elements so that styles can be applied to them.

General syntax:

selector {
    property: value;
}

Example:

h1 {
    color: blue;
}

Here, the selector is h1, which targets all <h1> elements.


1. Universal Selector (*)

The universal selector targets every element on the page.

* {
    margin: 0;
    padding: 0;
}

Use Case

It is commonly used to reset browser default spacing.


2. Element Selector

Targets all elements of a specific HTML tag.

p {
    color: green;
}

This styles every <p> element.


3. Class Selector (.)

A class selector targets elements that share the same class.

HTML:

<p class="highlight">Hello World</p>

CSS:

.highlight {
    background-color: yellow;
}

Multiple elements can share the same class, making it reusable.


4. ID Selector (#)

An ID selector targets a unique element.

HTML:

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

CSS:

#title {
    color: red;
}

Unlike classes, an ID should be used only once on a page.


5. Grouping Selector

Apply the same styles to multiple elements.

h1,
h2,
h3 {
    color: navy;
}

This reduces code duplication.


6. Descendant Selector

Targets elements inside another element.

div p {
    color: purple;
}

Only <p> elements inside a <div> are affected.


7. Child Selector (>)

Targets only the direct children.

div > p {
    color: orange;
}

Only immediate child paragraphs are selected.


8. Attribute Selector

Targets elements based on attributes.

input[type="text"] {
    border: 2px solid blue;
}

Useful when styling forms.


9. Pseudo-Class Selector

Targets elements in a particular state.

Example:

button:hover {
    background-color: black;
    color: white;
}

Other common pseudo-classes:

  • :hover

  • :focus

  • :active

  • :first-child

  • :last-child

These make websites more interactive.


Selector Specificity (Introduction)

Sometimes multiple CSS rules target the same element.

The browser decides which one to apply based on specificity.

General priority:

Inline CSS
↓

ID Selector (#)

↓

Class Selector (.)

↓

Element Selector

↓

Universal Selector (*)

We'll study specificity in more detail later.


Best Practices

✔ Use classes for reusable styles.

✔ Use IDs only for unique elements.

✔ Avoid unnecessary nesting.

✔ Keep selectors simple and readable.

✔ Group selectors when styles are the same.


My Biggest Takeaway

Before today, I thought CSS simply applied styles to HTML elements.

Now I understand that selectors are what make CSS powerful.

They allow us to target exactly the elements we want, whether it's a single heading, a group of buttons, or every paragraph inside a section.

Learning selectors feels like learning how to "communicate" with HTML through CSS.

100 Days of Code: My Journey to Becoming a Full Stack Developer

Part 22 of 50

Welcome to my 100 Days of Code journey! In this series, I'll document my daily progress as I learn Full Stack Web Development from the ground up. Every post will cover what I learned, challenges I faced, mistakes I made, and the projects I built. My goal is not just to complete 100 days but to become a better developer through consistency, discipline, and learning in public. Topics I'll cover include: • Git & GitHub • HTML, CSS & JavaScript • React.js • Node.js & Express • MongoDB • APIs • Real-world Projects • AI tools for Developers Whether you're just starting out or revising your fundamentals, I hope this journey helps you learn alongside me. Let's build, learn, and grow together! 🚀

Up next

Colors, Units & Backgrounds in CSS

333Yesterday, I learned how CSS selectors target HTML elements. Today, I explored one of the most exciting parts of CSS—adding colors, working with measurement units, and styling backgrounds. This is

More from this blog

T

TheSaurceCode

73 posts

Documenting my journey to becoming a Full Stack Developer through daily blogs, coding challenges, projects, tutorials, and lessons learned. Learn, build, and grow with me