.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / no-descending-specificity / README.md
1 # no-descending-specificity
2
3 Disallow selectors of lower specificity from coming after overriding selectors of higher specificity.
4
5 ```css
6     #container a { top: 10px; } a { top: 0; }
7 /** ↑                           ↑
8  * The order of these selectors represents descending specificity */
9 ```
10
11 Source order is important in CSS, and when two selectors have the *same* specificity, the one that occurs *last* will take priority. However, the situation is different when one of the selectors has a *higher* specificity. In that case, source order does *not* matter: the selector with higher specificity will win out even if it comes first.
12
13 The clashes of these two mechanisms for prioritization, source order and specificity, can cause some confusion when reading stylesheets. If a selector with higher specificity comes *before* the selector it overrides, we have to think harder to understand it, because it violates the source order expectation. **Stylesheets are most legible when overriding selectors always come *after* the selectors they override.** That way both mechanisms, source order and specificity, work together nicely.
14
15 This rule enforces that practice *as best it can*. (It cannot catch every *actual* overriding selector (because it does not know the DOM structure, for one), but it can catch certain common mistakes.)
16
17 Here's how it works: **This rule looks at the last *compound selector* in every full selector, and then compares it with other selectors in the stylesheet that end in the same way.**
18
19 So `.foo .bar` (whose last compound selector is `.bar`) will be compared to `.bar` and `#baz .bar`, but not to `#baz .foo` or `.bar .foo`.
20
21 And `a > li#wag.pit` (whose last compound selector is `li#wag.pit`) will be compared to `div li#wag.pit` and `a > b > li + li#wag.pit`, but not to `li`, or `li #wag`, etc.
22
23 There's one other important feature: Selectors targeting pseudo-elements are not considered comparable to similar selectors without the pseudo-element, because they target other elements on the rendered page. For example, `a::before {}` will not be compared to `a:hover {}`, because `a::before` targets a pseudo-element whereas `a:hover` targets the actual `<a>`.
24
25 This rule only compares rules that are within the same media context. So `a {} @media print { #baz a {} }` is fine.
26
27 This rule resolves nested selectors before calculating the specificity of the selectors.
28
29 ## Options
30
31 ### `true`
32
33 The following patterns are considered violations:
34
35 ```css
36 b a {}
37 a {}
38 ```
39
40 ```css
41 a + a {}
42 a {}
43 ```
44
45 ```css
46 b > a[foo] {}
47 a[foo] {}
48 ```
49
50 ```css
51 a {
52   & > b {}
53 }
54 b {}
55 ```
56
57 ```css
58 @media print {
59   #c a {}
60   a {}
61 }
62 ```
63
64 The following patterns are *not* considered violations:
65
66 ```css
67 a {}
68 b a {}
69 ```
70
71 ```css
72 a {}
73 a + a {}
74 ```
75
76 ```css
77 a[foo] {}
78 b > a[foo] {}
79 ```
80
81 ```css
82 b {}
83 a {
84   & > b {}
85 }
86 ```
87
88 ```css
89 a::before {}
90 a:hover::before {}
91 a {}
92 a:hover {}
93 ```
94
95 ```css
96 @media print {
97   a {}
98   #c a {}
99 }
100 ```
101
102 ```css
103 a {}
104 @media print {
105   #baz a {}
106 }
107 ```