🍋
Menu
CSS

Specificity

CSS Specificity

The algorithm browsers use to determine which CSS rule takes precedence when multiple rules target the same element.

Chi tiết kỹ thuật

CSS specificity is calculated as a tuple (a, b, c): 'a' counts ID selectors, 'b' counts class/attribute/pseudo-class selectors, 'c' counts type/pseudo-element selectors. Inline styles override all selectors. !important overrides inline styles but creates maintenance problems. The new @layer rule (Cascade Layers) adds a fourth dimension to the cascade, allowing authors to define explicit priority ordering for entire groups of styles, reducing specificity wars in large codebases.

Ví dụ

```css
/* Specificity: (0,0,1) — type selector */
p { color: black; }

/* Specificity: (0,1,0) — class selector */
.highlight { color: blue; }  /* wins over p */

/* Specificity: (1,0,0) — ID selector */
#title { color: red; }       /* wins over .highlight */
```

Định dạng liên quan

Công cụ liên quan

Thuật ngữ liên quan