Skip to content

Commit 9416470

Browse files
committed
[css-variables] Add an example of valid variable fallback right in the beginning.
1 parent 0cf77c6 commit 9416470

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

css-variables/Overview.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,34 @@ <h2 id=using-variables><span class=secno>3. </span> Using Cascading
573573
class=css>var()</code>’ function will mean that the containing
574574
declaration is invalid at computed-value time.
575575

576+
<div class=example> The fallback value allows for some types of defensive
577+
coding. For example, an author may create a component intended to be
578+
included in a larger application, and use variables to style it so that
579+
it's easy for the author of the larger application to theme the component
580+
to match the rest of the app.
581+
<p> Without fallback, the app author must supply a value for every
582+
variable that your component uses. With fallback, the component author
583+
can supply defaults, so the app author only needs to supply values for
584+
the variables they wish to override.
585+
586+
<pre class=example>/* In the component's style: */
587+
.component .header {
588+
color: var(header-color, blue);
589+
}
590+
.component .text {
591+
color: var(text-color, black);
592+
}
593+
594+
/* In the larger application's style: */
595+
.component {
596+
var-text-color: #080;
597+
/* header-color isn't set,
598+
and so remains blue,
599+
the fallback value */
600+
}
601+
</pre>
602+
</div>
603+
576604
<div class=example> For example, the following code incorrectly attempts to
577605
use a variable as a property name:
578606
<pre>.foo {

css-variables/Overview.src.html

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,39 @@ <h2 id='using-variables'>
348348
Otherwise, the result of evaluating the ''var()'' function will mean
349349
that the containing declaration is invalid at computed-value time.
350350

351+
<div class='example'>
352+
The fallback value allows for some types of defensive coding.
353+
For example,
354+
an author may create a component
355+
intended to be included in a larger application,
356+
and use variables to style it
357+
so that it's easy for the author of the larger application
358+
to theme the component to match the rest of the app.
359+
360+
Without fallback,
361+
the app author must supply a value for every variable that your component uses.
362+
With fallback, the component author can supply defaults,
363+
so the app author only needs to supply values for the variables they wish to override.
364+
365+
<pre class='example'>
366+
/* In the component's style: */
367+
.component .header {
368+
color: var(header-color, blue);
369+
}
370+
.component .text {
371+
color: var(text-color, black);
372+
}
373+
374+
/* In the larger application's style: */
375+
.component {
376+
var-text-color: #080;
377+
/* header-color isn't set,
378+
and so remains blue,
379+
the fallback value */
380+
}
381+
</pre>
382+
</div>
383+
351384
<div class='example'>
352385
For example, the following code incorrectly attempts to use a variable as a property name:
353386

0 commit comments

Comments
 (0)