Skip to main content

Command Palette

Search for a command to run...

CSS Selectors 101: Targeting Elements with Precision

Updated
3 min read

If HTML is the skeleton of your website, then CSS (Cascading Style Sheets) is the interior design. It’s the paint, the furniture, and the lighting.

But how does the browser know which wall to paint blue and which chair to make velvet? That’s where Selectors come in. Think of selectors as the addressing system for your webpage.


1. Why do we need Selectors?

Imagine you are a teacher in a room full of students. If you shout "Hey, student!", everyone looks up. If you want a specific person to stand up, you need a way to target them.

Selectors allow you to "pick" specific HTML elements and tell the browser: "Hey, you—look like this."


2. The Big Three: Standard Selectors

There are three main ways to target elements. Think of them in order of "broad" to "specific."

Element Selector (Broadest)

This targets every element of a certain type. If you want every paragraph on your site to have the same font, you use this.

  • Target: p { color: grey; }

  • Analogy: Addressing "all humans."

Class Selector (Specific)

Classes are like "team uniforms." You can give the same class to multiple elements, and they will all share that styling. In CSS, classes start with a dot (.).

  • Target: .btn-primary { background-color: blue; }

  • Analogy: Addressing "everyone wearing a blue shirt."

ID Selector (Most Specific)

An ID is a unique name for only one element on a page. It’s like a social security number. In CSS, IDs start with a hashtag (#).

  • Target: #main-header { font-size: 40px; }

  • Analogy: Addressing "John Smith."


3. Combining Forces: Advanced Selectors

Sometimes, you need to be a bit more clever about how you choose your targets.

Grouping Selectors

If you want your h1, h2, and p to all be centered, you don't have to write three separate rules. Just separate them with a comma.

  • Syntax: h1, h2, p { text-align: center; }

Descendant Selectors

This targets an element only if it is inside another specific element. It uses a simple space between the names.

  • Syntax: nav a { color: white; }

  • Meaning: "Find all links (<a>) that are inside a navigation bar (<nav>)."


4. Selector Priority (The "Power Level")

What happens if you tell a paragraph to be red using its element name, but blue using its class? Who wins?

In CSS, this is called Specificity. A simple rule of thumb for now is:

  1. ID beats Class.

  2. Class beats Element.

  3. If they are equal, the last one written in the code wins!


The "Before & After"

Here is a quick look at how these selectors actually change the raw skeleton:

Selector TypeHTMLCSSResult
Element<p>Hello</p>p { color: red; }Red text.
Class<p class="bold">Hi</p>.bold { font-weight: 800; }Bold text.
ID<div id="box"></div>#box { width: 100px; height: 100px; }A 100px square.

More from this blog

Prakash Jha Blogs

23 posts

Trying to write Blogs on day-to-day applications or technology from a different perspective, you never thought of how amazing those technologies are.