CSScss_selectorWeb DevelopmentwebdevCohort2026ChaiCodeChaiaurcodeChaiCohort

CSS Selectors 101: Targeting Elements with Precision

Learn CSS selectors: elements, classes, IDs, groups, descendants, and specificity for precise web styling

Kenil Patel
Kenil PatelFull Stack Engineer
CSS Selectors 101: Targeting Elements with Precision

Imagine you are at a large wedding. If you want to call someone, you can’t just yell "Hey Person!"—everyone will look at you. You might call them by their Category (e.g., "Waiters"), their Last Name (e.g., "The Sharmas"), or a Unique Name (e.g., "Rahul").

In the world of web development, CSS Selectors work exactly like this. They are the "addresses" we use to tell the browser which specific HTML element we want to style.

1. Why do we need CSS Selectors?

HTML gives structure to your content, but without selectors, you can't apply styles. Selectors act as a bridge between your HTML structure and your CSS design. They allow you to:

  • Change colors, fonts, and layouts.

  • Keep your code organized.

  • Target specific parts of a page without affecting the rest.


2. The Big Three: Element, Class, and ID

A. The Element Selector (The "Category")

This targets all elements of a specific HTML tag.

  • Analogy: Calling everyone wearing a "Red Shirt."

  • Use Case: Setting global styles like font-family for all paragraphs.

CSS

/* All <p> elements will be blue */
p {
  color: blue;
}

B. The Class Selector (The "Group")

Classes are used to target a group of elements. You can use the same class on multiple tags. In CSS, a class starts with a dot (.).

  • Analogy: Calling everyone in the "Engineering Team."

  • Use Case: Styling buttons or specific highlighted text.

CSS

/* Any element with class="highlight" will have a yellow background */
.highlight {
  background-color: yellow;
}

C. The ID Selector (The "Unique Individual")

An ID is unique and can only be used once per page. In CSS, an ID starts with a hash (#).

  • Analogy: Calling a person by their Aadhar Card number or Social Security number.

  • Use Case: Styling a unique element like a "Navbar" or "Footer-Submit-Button."

CSS

/* The element with id="main-header" will be bold */
#main-header {
  font-weight: bold;
}

3. Combining Forces: Group and Descendant Selectors

Group Selectors

If you want to apply the same style to different elements, you don't need to write separate code. Just separate them with a comma.

  • Example: Making all headers and paragraphs center-aligned.

CSS

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

Descendant Selectors

This targets an element that is inside another element. It uses a space between selectors.

  • Example: Styling only the links (<a>) that are inside a <footer>.

CSS

footer a {
  color: gray;
  text-decoration: none;
}

4. Priority: Who Wins? (High-Level)

When multiple selectors target the same element, CSS follows a "Hierarchy of Power." This is called Specificity.

  1. ID Selector (Strongest)

  2. Class Selector (Medium)

  3. Element Selector (Weakest)

Example: If a paragraph has an ID of #text and a class of .blue-text, and you give them different colors, the ID's style will win!


Summary: Before and After

Without selectors, your website is a plain black-and-white document. With them, you gain the precision to paint your digital canvas exactly how you want.

Selector TypeSyntaxBest For...
Elementh1Global styles
Class.btnReusable styles
ID#headerUnique, one-time elements