--- id: css-specificity-hierarchy title: "CSS Specificity Hierarchy" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["specificity hierarchy", "specificity weight", "specificity X-Y-Z", "selector ranking", "specificity calculation"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.86 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["css", "web", "frontend", "w3schools", "specificity", "selectors", "cascade"] raw_sources: ["https://www.w3schools.com/css/css_specificity_hierarchy.asp"] applied_in: [] github_commit: "" --- # [[CSS Specificity Hierarchy]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Every selector earns a three-number weight (X-Y-Z) counting ID, class/attribute/pseudo-class, and element/pseudo-element selectors β€” the higher leftmost number wins, deciding which rule applies. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Four ranked categories** β€” from highest to lowest the hierarchy is: inline styles, ID selectors, classes/attributes/pseudo-classes, and elements/pseudo-elements. [S1] - **Three-digit weight notation (X-Y-Z)** β€” X counts ID selectors, Y counts class, attribute, and pseudo-class selectors, and Z counts element and pseudo-element selectors. [S1] - **Leftmost-number-wins comparison** β€” the selector with the higher leftmost number wins; if those are equal, compare the next number, and so on. [S1] - **Universal selector contributes nothing** β€” the universal selector (`*`) and `:where()` add no specificity weight. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Count-then-compare pattern** β€” to resolve a conflict, count each selector's ID/class/element selectors into X-Y-Z, then compare position by position from the left. [S1] - **Compound selectors stack weight** β€” combining selector types (e.g. `p#demo`) adds their individual weights together (1-0-0 + 0-0-1 = 1-0-1). [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **Hierarchy of selector categories.** Specificity ranks selector types as follows (highest priority first) [S1]: | Rank (high β†’ low) | Selector category | | --- | --- | | 1 (highest) | Inline styles | | 2 | ID selectors | | 3 | Classes, attributes, and pseudo-classes | | 4 | Elements and pseudo-elements | | 5 (lowest) | Universal selector and `:where()` | **Weight notation (X-Y-Z).** Specificity is written as a three-number value where [S1]: - **X** = the count of ID selectors, - **Y** = the count of class selectors, attribute selectors, and pseudo-classes, - **Z** = the count of element selectors and pseudo-elements. The selector with the higher leftmost number wins; if equal, the next number is compared, and so on. [S1] **Example β€” competing selectors with computed weights.** The page demonstrates the weights of several selectors targeting the same element. The compound selector `p#demo` (weight 1-0-1) beats the bare `#demo` (weight 1-0-0) because, with the ID counts tied at 1, its element count breaks the tie. [S1] ```css #demo {color: blue;} /* weight: 1-0-0 */ p#demo {color: orange;} /* weight: 1-0-1 WINS! */ .test {color: green;} /* weight: 0-1-0 */ p.test {color: green;} /* weight: 0-1-1 */ p {color: red;} /* weight: 0-0-1 */ ``` **Tie-breaking rules noted on the page.** When specificity is equal, later rules win; ID selectors beat attribute selectors; and class selectors beat element selectors. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) The weight-annotated example above is the page's applied demonstration: each selector is labeled with its X-Y-Z weight so the reader can see exactly why `p#demo` (1-0-1) wins over the others. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Annotating selectors with their specificity weight (language: CSS): ```css #demo {color: blue;} /* 1-0-0 */ p#demo {color: orange;} /* 1-0-1 β€” wins: ID tied, element breaks the tie */ .test {color: green;} /* 0-1-0 */ p {color: red;} /* 0-0-1 */ ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.86 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[CSS Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[CSS Specificity]], [[CSS !important]], [[CSS Selectors]] - **μ°Έμ‘° λ§₯락:** Used to compute and compare selector weights when diagnosing why a particular CSS rule does or does not apply. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” CSS Specificity Hierarchy β€” https://www.w3schools.com/css/css_specificity_hierarchy.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "CSS Specificity Hierarchy" page (Astra wiki-curation, P-Reinforce v3.1 format).