# CSS Specificity & Inheritance vs CSS :has() Relational Selector

- **Canonical URL:** https://frontend.rocks/vs/css-specificity-inheritance-vs-css-has-selector/
- **Relation:** Contrasts With
- **Last Verified:** 2026-11-15
- **Verdict:** :has() adopts the highest specificity of its relative selector list instead of summing combinators.

## Comparison Matrix

| Dimension | CSS Specificity & Inheritance | CSS :has() Relational Selector |
| :--- | :--- | :--- |
| **Maturity** | Baseline · Widely | Baseline · Newly |
| **Domain** | CSS & Layout | CSS & Layout |
| **Mental Model** | Cascade resolves Origin, @layer, (A,B,C) specificity, and source order; inherited properties propagate down only when no rule matches. | :has() matches an element based on its descendants or following siblings, eliminating JS parent-class toggling. |
| **Prerequisites** | None | CSS Specificity & Inheritance (:has() inherits the highest specificity among its relative selector arguments.) |
| **When Not to Use** | When not to use #id selectors in CSS: Never style with #id selectors in reusable components; reserve #id for DOM anchors and ARIA labelledby targets. | When not to use :has(): Avoid :has() on :root for high-frequency hover/scroll states; scope it to small local components like .field or .card. |

## CSS Specificity & Inheritance — Under the Hood
Specificity is a three-digit odometer `(IDs, Classes, Tags)` where ten `Classes` never roll over to beat one `ID`.

1. **Layer Gate**: Declarations inside a lower `@layer` lose to higher layers or unlayered rules before specificity is even compared.
2. **Tuple Comparison**: `A` counts `#id` selectors; `B` counts `.class`, `[attr]`, and `:hover`; `C` counts `div` and `::before`. `:where()` contributes `(0, 0, 0)`, while `:is()`, `:not()`, and [:has()](/concepts/css-has-selector) take the highest specificity in their argument list.
3. **Inheritance**: Properties like `color`, `font-family`, and [CSS Custom Properties](/concepts/css-custom-properties-cascade) copy from the parent's `ComputedStyle` only if no rule (not even a user-agent rule) targets the element.

```css
/* Zero specificity reset: trivial to override anywhere */
:where(button, input, select) {
  font: inherit;
  color: inherit;
}

/* Specificity (0, 1, 0) beats (0, 0, 1) regardless of order */
.btn-primary { background: #2563eb; }
```

## CSS :has() Relational Selector — Under the Hood
For twenty years CSS only looked downward and forward because engines feared O(n^2) re-evaluations during the [Browser Rendering Pipeline](/concepts/browser-rendering-pipeline). Modern engines solved this with bloom-filter fast-rejects and targeted invalidation flags.

1. **Right-to-Left Anchoring**: When a child changes (e.g., a checkbox becomes `:checked`), the engine checks an ancestor bitmask to see if any parent listens to `:has()`.
2. **Specificity**: Like `:is()`, `:has()` adopts the highest [Specificity](/concepts/css-specificity-inheritance) in its argument list unless wrapped in `:where()`.
3. **Sibling Lookahead**: `.tab:has(+ .tab:hover)` styles the element *before* the hovered sibling.

```css
/* Highlight form field label when its input is invalid and touched */
.field:has(input:user-invalid) > label {
  color: #dc2626;
}

/* Lock page scroll when a modal checkbox or dialog is open */
body:has(dialog[open]) {
  overflow: hidden;
}
```

## Interactive Labs & Dissections
- [CSS Specificity & Inheritance Lab](https://frontend.rocks/concepts/css-specificity-inheritance/#lab) ([Markdown](https://frontend.rocks/concepts/css-specificity-inheritance.md))
- [CSS :has() Relational Selector Lab](https://frontend.rocks/concepts/css-has-selector/#lab) ([Markdown](https://frontend.rocks/concepts/css-has-selector.md))

## Primary Sources
- https://drafts.csswg.org/selectors-4/#specificity-rules
- https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
- https://drafts.csswg.org/selectors-4/#relational
- https://developer.mozilla.org/en-US/docs/Web/CSS/:has
