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
Abstract: This module introduces cascading variables as a new primitive value type that is accepted by all CSS properties, and custom properties for defining them.
18
+
Default Highlight: css
18
19
</pre>
19
20
20
21
<pre class=link-defaults>
@@ -239,11 +240,11 @@ Custom Property Value Syntax</h3>
239
240
#alert { --color: red; }
240
241
* { color: var(--color); }
241
242
242
-
<p>I inherited blue from the root element!</p>
243
-
<div>I got green set directly on me!</div>
243
+
<p><span style="color: blue">I inherited blue from the root element!</span></p>
244
+
<div><span style="color: green">I got green set directly on me!</span></div>
244
245
<div id='alert'>
245
-
While I got red set directly on me!
246
-
<p>I'm red too, because of inheritance!</p>
246
+
<span style="color: red">While I got red set directly on me!</span>
247
+
<p><span style="color: red">I'm red too, because of inheritance!</span></p>
247
248
</div>
248
249
</pre>
249
250
</div>
@@ -401,6 +402,21 @@ Using Cascading Variables: the ''var()'' notation</h2>
401
402
(Doing so usually produces invalid syntax,
402
403
or else a value whose meaning has no connection to the variable.)
403
404
405
+
<div class='example'>
406
+
For example, the following code incorrectly attempts to use a variable as a property name:
407
+
408
+
<pre>
409
+
.foo {
410
+
--side: margin-top;
411
+
var(--side): 20px;
412
+
}
413
+
</pre>
414
+
415
+
This is <em>not</em> equivalent to setting ''margin-top: 20px;''.
416
+
Instead, the second declaration is simply thrown away as a syntax error
417
+
for having an invalid property name.
418
+
</div>
419
+
404
420
The first argument to the function is the name of the <a>custom property</a> to be substituted.
405
421
The second argument to the function, if provided,
406
422
is a fallback value,
@@ -410,38 +426,6 @@ Using Cascading Variables: the ''var()'' notation</h2>
410
426
For example, ''var(--foo, red, blue)'' defines a fallback of ''red, blue'';
411
427
that is, anything between the first comma and the end of the function is considered a fallback value.
412
428
413
-
If a property contains one or more ''var()'' functions,
414
-
and those functions are syntactically valid,
415
-
the entire property's grammar must be assumed to be valid at parse time.
416
-
It is only syntax-checked at computed-value time,
417
-
after ''var()'' functions have been <a lt="substitute a var()">substituted</a>.
418
-
419
-
To <dfn export>substitute a var()</dfn> in a property's value:
420
-
421
-
<ol>
422
-
<li>
423
-
If the <a>custom property</a> named by the first argument to the ''var()'' function is <a>animation-tainted</a>,
424
-
and the ''var()'' function is being used in the 'animation' property or one of its longhands,
425
-
treat the <a>custom property</a> as having its initial value for the rest of this algorithm.
426
-
427
-
<li>
428
-
If the value of the <a>custom property</a> named by the first argument to the ''var()'' function
429
-
is anything but the initial value,
430
-
replace the ''var()'' function by the value of the corresponding <a>custom property</a>.
431
-
432
-
<li>
433
-
Otherwise, if the ''var()'' function has a fallback value as its second argument,
434
-
replace the ''var()'' function by the fallback value.
435
-
If there are any ''var()'' references in the fallback,
436
-
<a lt="substitute a var()">substitute</a> them as well.
437
-
438
-
<li>
439
-
Otherwise,
440
-
the property containing the ''var()'' function is <a>invalid at computed-value time</a>.
441
-
442
-
Note: Other things can also make a property <a>invalid at computed-value time</a>.
443
-
</ol>
444
-
445
429
<div class='example'>
446
430
The fallback value allows for some types of defensive coding.
447
431
For example,
@@ -475,21 +459,42 @@ Using Cascading Variables: the ''var()'' notation</h2>
475
459
</pre>
476
460
</div>
477
461
478
-
<div class='example'>
479
-
For example, the following code incorrectly attempts to use a variable as a property name:
462
+
If a property contains one or more ''var()'' functions,
463
+
and those functions are syntactically valid,
464
+
the entire property's grammar must be assumed to be valid at parse time.
465
+
It is only syntax-checked at computed-value time,
466
+
after ''var()'' functions have been <a lt="substitute a var()">substituted</a>.
480
467
481
-
<pre>
482
-
.foo {
483
-
--side: margin-top;
484
-
var(--side): 20px;
485
-
}
486
-
</pre>
468
+
To <dfn export lt="substitute a var()|var() substitution">substitute a var()</dfn> in a property's value:
487
469
488
-
This is <em>not</em> equivalent to setting ''margin-top: 20px;''.
489
-
Instead, the second declaration is simply thrown away as a syntax error
490
-
for having an invalid property name.
470
+
<ol>
471
+
<li>
472
+
If the <a>custom property</a> named by the first argument to the ''var()'' function is <a>animation-tainted</a>,
473
+
and the ''var()'' function is being used in the 'animation' property or one of its longhands,
474
+
treat the <a>custom property</a> as having its initial value for the rest of this algorithm.
475
+
476
+
<li>
477
+
If the value of the <a>custom property</a> named by the first argument to the ''var()'' function
478
+
is anything but the initial value,
479
+
replace the ''var()'' function by the value of the corresponding <a>custom property</a>.
480
+
481
+
<li>
482
+
Otherwise, if the ''var()'' function has a fallback value as its second argument,
483
+
replace the ''var()'' function by the fallback value.
484
+
If there are any ''var()'' references in the fallback,
485
+
<a lt="substitute a var()">substitute</a> them as well.
486
+
487
+
<li>
488
+
Otherwise,
489
+
the property containing the ''var()'' function is <a>invalid at computed-value time</a>.
490
+
491
+
Note: Other things can also make a property <a>invalid at computed-value time</a>.
492
+
</ol>
491
493
492
-
Similarly, you can't build up a single token where part of it is provided by a variable:
494
+
<div class=note>
495
+
Note that <a>var() substitution</a> takes place at the level of CSS tokens [[css-syntax-3]],
496
+
not at a textual level;
497
+
you can't build up a single token where part of it is provided by a variable:
493
498
494
499
<pre>
495
500
.foo {
@@ -498,7 +503,7 @@ Using Cascading Variables: the ''var()'' notation</h2>
498
503
}
499
504
</pre>
500
505
501
-
Again, this is <em>not</em> equivalent to setting ''margin-top: 20px;'' (a length).
506
+
This is <em>not</em> equivalent to setting ''margin-top: 20px;'' (a length).
502
507
Instead, it's equivalent to ''margin-top: 20 px;'' (a number followed by an ident),
503
508
which is simply an invalid value for the 'margin-top' property.
504
509
Note, though, that ''calc()'' can be used to validly achieve the same thing, like so:
0 commit comments