# Selectors & Combinators

> CSS selectors pick which elements a rule applies to. Combinators add structural context.

Subject: CSS | Lesson 3 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/selectors-and-combinators
Previous: [How CSS Works: Syntax & Linking](https://codewithanimation.in/css/how-css-works.md) | Next: [Pseudo-classes & Pseudo-elements](https://codewithanimation.in/css/pseudo-classes-and-elements.md)

## Selectors: from simple to structural

A selector is the part of a CSS rule that identifies target elements. The simplest selectors match by tag name (`h2`), class (`.card`), or id (`#hero`). Attribute selectors go further: `[type=email]` matches inputs of a specific type, `[href^=https]` matches links that start with https.

Combinators describe how elements relate to each other in the DOM tree. A space means "any descendant": `nav a` matches every `<a>` anywhere inside `<nav>`. The `>` child combinator is strict: `ul > li` only matches `<li>` elements that are direct children of `<ul>`, not grandchildren.

The adjacent sibling `+` matches the element immediately after: `h2 + p` targets the first `<p>` right after an `<h2>`. The general sibling `~` matches all following siblings: `h2 ~ p` targets every `<p>` that follows an `<h2>` under the same parent.

## Selectors define the scope of every declaration

Every CSS property declaration is useless without a selector that points it at the right elements. Precise selectors keep styles predictable and prevent unintended side effects on elements you did not mean to target.

Selectors also determine specificity, which controls which rule wins when two rules declare the same property. Understanding selector types is therefore inseparable from understanding the cascade.

## Combining selectors for precise targeting

A **type selector** (`p`) targets all elements of that tag. A **class selector** (`.btn`) targets all elements with that class. An **id selector** (`#main`) targets a single unique element. Classes are the workhorse of real-world CSS because they are reusable, low-specificity, and easy to add or remove via JavaScript.

Chain selectors without a space to narrow targeting: `.btn.primary` matches elements that have both classes. Combine a type and class: `button.btn` matches only `<button>` elements with the `.btn` class, not other elements.

**Descendant** `nav a` - any `<a>` inside `<nav>`, any depth. **Child** `ul > li` - only direct `<li>` children. **Adjacent sibling** `h2 + p` - the `<p>` directly after `<h2>`. **General sibling** `h2 ~ p` - all `<p>` after `<h2>` under the same parent.

## Common mistake: Over-specific selectors that are hard to override

Writing `#sidebar .widget h3 span` just because it works will haunt you: the specificity is so high that almost nothing can override it later without reaching for `!important`, which creates its own arms race. Prefer flat, class-based selectors (`.widget-title`) over deep descendant chains.

IDs in CSS are especially problematic because a single id selector `#hero` has specificity (0,1,0,0) - equivalent to 100 class selectors. Use ids for JavaScript hooks and fragment links, not as CSS selectors.

## Tip: Write selectors that describe what an element IS

Prefer `.nav-link` over `header nav ul li a`. The class name describes what the element is; the long chain describes where it accidentally lives today. When the DOM structure changes (it always does), class-based selectors survive; deep chains break.

## Try it: Try it

Pick a selector type and watch which nodes in the sample DOM tree light up.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/selectors-and-combinators

## Check yourself

### Question 1

Which selector targets only `<li>` elements that are DIRECT children of `<ul>`, not deeper descendants?

- A. `ul > li`
- B. `ul li`
- C. `ul + li`
- D. `ul ~ li`

Correct answer: A

The child combinator `>` limits matching to direct children only. `ul > li` will not match a `<li>` nested inside another `<li>` or any intermediate element.

- Why not B: The space (descendant combinator) matches `<li>` at any depth inside `<ul>`, not just direct children.
- Why not C: `+` is the adjacent sibling combinator. It would select a `<li>` that immediately follows a `<ul>` as a sibling, not a child.
- Why not D: `~` is the general sibling combinator. It selects `<li>` elements that follow `<ul>` as siblings, not children.

### Question 2

You want to select the `<p>` element immediately following an `<h2>`. Which selector is correct?

- A. `h2 + p`
- B. `h2 ~ p`
- C. `h2 > p`
- D. `h2 p`

Correct answer: A

The adjacent sibling combinator `+` selects the single element that immediately follows the first element and shares the same parent. `h2 + p` is the correct choice for targeting the next `<p>` right after an `<h2>`.

- Why not B: `~` selects ALL `<p>` siblings after an `<h2>`, not just the immediately adjacent one.
- Why not C: `>` is the child combinator. `<p>` following `<h2>` is a sibling, not a child.
- Why not D: The space (descendant) would select `<p>` inside `<h2>`, not after it.

### Question 3

Which has the HIGHEST specificity?

- A. `#nav`
- B. `.nav.active.open`
- C. `nav ul li a`
- D. `[data-active=true]`

Correct answer: A

Specificity is calculated in layers: inline styles, then ids (0,1,0,0), then classes/attributes/pseudo-classes (0,0,1,0), then types/pseudo-elements (0,0,0,1). A single id beats any number of classes.

- Why not B: Three classes give specificity (0,0,3,0). A single id gives (0,1,0,0), which is higher.
- Why not C: Four type selectors give specificity (0,0,0,4), the lowest of these options.
- Why not D: An attribute selector counts the same as a class: (0,0,1,0). An id is still higher.

## Remember this

- Type (`h2`), class (`.btn`), id (`#hero`), attribute (`[type=email]`) are the four main selector types.
- `>` = direct child only. Space = any descendant. `+` = immediately adjacent sibling. `~` = all following siblings.
- Ids have very high specificity (0,1,0,0). Prefer class selectors for reusable, overridable styles.
- Chain selectors without a space to AND them: `.btn.primary` matches elements with both classes.
