Skip to main content

Command Palette

Search for a command to run...

#12 CSS Selectors 101: Targeting Elements with Precision

Updated
3 min read
P
Since I am developer. My blogs are related to Tech and also blogs write during web dev cohort by chai code.

If HTML is the skeleton of a webpage, CSS (Cascading Style Sheets) is the clothing. But before you can style a button or change a font color, you need a way to tell the browser exactly which element you are talking to.

Selectors are the "addresses" we use to target specific parts of our HTML.

1. Why Do We Need Selectors?

Imagine you are a teacher in a classroom full of students. If you shout "Sit down!", everyone sits down. If you want only one specific student to sit down, you need to use their name or a specific description.

In CSS, selectors allow you to be as broad or as specific as you want.

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

Most of your styling will rely on these three types of selectors.

The Element Selector

This targets every single instance of an HTML tag.

  • Usage: p { color: blue; }

  • Result: Every paragraph on your website turns blue.

  • Analogy: "All students must wear a white shirt."

The Class Selector

This is the most common selector. You can give a "class" name to any HTML element. You can use the same class on multiple elements. In CSS, classes start with a dot (.).

  • HTML: <button class="btn-primary">Click Me</button>

  • CSS: .btn-primary { background: green; }

  • Analogy: "Everyone in the Cricket Club gets a green cap."

The ID Selector

This targets a unique element. An ID can only be used once per page. In CSS, IDs start with a hashtag (#).

  • HTML: <div id="main-header">...</div>

  • CSS: #main-header { font-size: 24px; }

  • Analogy: "The Principal gets a private office."

3. Advanced Targetting

Group Selectors

If you want the same style for different elements, don't repeat yourself. Separate them with a comma.

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

  • Result: All h1s, h2s, and paragraphs will be centered.

Descendant Selectors

Sometimes you only want to style something if it is inside another element. You do this by putting a space between selectors.

  • Example: nav a { color: white; }

  • Result: Only links (<a>) that are inside a <nav> tag will turn white.

4. Selector Priority (Specificity)

What happens if you tell a paragraph to be red using an element selector, but blue using a class selector?

CSS has a "ranking" system. The more specific the selector, the more power it has:

  1. ID Selector (Most Powerful)

  2. Class Selector

  3. Element Selector (Least Powerful)

Pro Tip: In your React and Tailwind CSS work, you'll mostly use classes. IDs are rarely used for styling because they are too "rigid" and can't be reused.

5. Summary Table

Selector TypeSyntaxBest Used For...
Elementh1Global styles (resetting fonts/margins)
Class.cardReusable components (buttons, cards)
ID#navbarUnique, one-of-a-kind sections
Grouph1, h2Applying the same style to many things
Descendantdiv pStyling elements based on their location