You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20-16Lines changed: 20 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -64,40 +64,44 @@ console.log(output);
64
64
65
65
### Defining Custom Properties with `--*`
66
66
67
-
A custom property is any property whose name starts with two dashes (U+002D HYPHEN-MINUS). A property must be in a rule.
67
+
A custom property is any property whose name starts with two dashes `--`. A property must be in a rule.
68
68
69
-
*Note: `:root` is nothing more than the selector for the root DOM node.*
69
+
*Note: `:root` is nothing more than the selector for the root DOM node. Any other selector like `.class`, `#id`, or even `#foo ~ .bar > span.baz` works.*
70
70
71
71
```css
72
72
:root {
73
73
--foo-width: 100px;
74
74
--foo-bg-color: rgba(255, 0, 0, 0.85);
75
75
}
76
-
```
77
-
78
-
You may also declare custom properties in a normal selector. Any other selector like `.class`, `#id`, or even `#foo ~ .bar > span.baz` works.
79
76
80
-
```css
81
77
.foo {
82
78
--foo-width: 100px;
83
79
--foo-bg-color: rgba(255, 0, 0, 0.85);
84
80
}
85
81
```
86
82
87
-
A custom property can be declared multiple times, but only the last one takes precedence.
83
+
Custom properties can be declared multiple times, but like variable scope in other languages, only the most specific one takes precedence.
88
84
89
85
```css
90
-
:root {
91
-
--foo-width: 100px;
92
-
--foo-width: 200px; /* This is the declaration used. */
86
+
:root: {
87
+
--some-color: red;
93
88
}
94
89
95
90
.foo {
96
-
--foo-width: 100px;
91
+
/* red */
92
+
color: var(--some-color);
97
93
}
98
94
99
-
.foo {
100
-
--foo-width: 200px; /* This is the declaration used. */
95
+
96
+
.bar {
97
+
--some-color: blue;
98
+
/* blue */
99
+
color: var(--some-color);
100
+
}
101
+
102
+
.bar:hover {
103
+
--some-color: green;
104
+
/* Automically gets a `color: green;` declaration because we `--some-color` used within scope elsewhere */
101
105
}
102
106
```
103
107
@@ -138,7 +142,7 @@ It's perfectly okay to declare CSS variables inside media queries and the like.
138
142
}
139
143
```
140
144
141
-
This will be transformed to:
145
+
Will be transformed to:
142
146
143
147
```css
144
148
.box {
@@ -167,7 +171,7 @@ Psuedo-classes are also dealt with correctly, because it's easy to statically de
167
171
}
168
172
```
169
173
170
-
This will be transformed to:
174
+
Will be transformed to:
171
175
172
176
```css
173
177
.foo {
@@ -350,7 +354,7 @@ Continue to the next section to see where some of these might be unsafe to do. T
350
354
351
355
# Caveats
352
356
353
-
When declaring a CSS variable inside one selector, but consume it in another, this does make an unsafe assumption which can be non-conforming in certain edge cases. Here is an example where these limitations unavoidably result in non-conforming behavior.
357
+
When you declare a CSS variable inside one selector, but consume it in another, this does make an unsafe assumption about it which can be non-conforming in certain edge cases. Here is an example where these limitations result in non-conforming behavior.
0 commit comments