Skip to content

Commit 147b6c3

Browse files
committed
Rewrite the 'defining vars' section to be more consistent in using 'data property' and 'variable'. Also improve examples.
1 parent c060207 commit 147b6c3

2 files changed

Lines changed: 107 additions & 54 deletions

File tree

css-variables/Overview.html

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ <h2 class="no-num no-toc" id=contents>Table of contents</h2>
128128
</ul>
129129

130130
<li><a href="#defining-variables"><span class=secno>2. </span> Defining
131-
Variables</a>
131+
Variables With Data Properties</a>
132132

133133
<li><a href="#using-variables"><span class=secno>3. </span> Using
134134
Variables</a>
@@ -219,8 +219,8 @@ <h2 id=intro><span class=secno>1. </span> Introduction</h2>
219219

220220
<h3 id=placement><span class=secno>1.1. </span> Module Interactions</h3>
221221

222-
<p>This module defines a new type of primitive value, the <a
223-
href="#variable"><i>variable</i></a>, which is accepted by all properties.
222+
<p>This module defines a new type of primitive value, the <i>variable</i>,
223+
which is accepted by all properties.
224224

225225
<h3 id=values><span class=secno>1.2. </span> Values</h3>
226226

@@ -235,15 +235,12 @@ <h3 id=values><span class=secno>1.2. </span> Values</h3>
235235
when combined with this module, expands the definition of the
236236
&lt;color&gt; value type as used in this specification.
237237

238-
<h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
238+
<h2 id=defining-variables><span class=secno>2. </span> Defining Variables
239+
With Data Properties</h2>
239240

240-
<p>A <dfn id=variable>variable</dfn> is defined by assigning a value to a
241-
property whose name starts with the prefix "data-" (this class of
242-
properties will be referred to <a href="#data-property"><i>data
243-
properties</i></a>). <em>Any</em> property with the "data-" prefix is
244-
defined to be valid but meaningless, and will never be given a specific
245-
meaning or effect by a CSS specification or user agent; this is similar to
246-
the <code>data-*</code> family of custom attributes in HTML.
241+
<p>This specification defines an open-ended set of properties called <a
242+
href="#data-property"><i>data properties</i></a>, which are used to define
243+
<i>variables</i>.
247244

248245
<table class=propdef>
249246
<tbody>
@@ -262,8 +259,7 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
262259
<tr>
263260
<th>Initial:
264261

265-
<td>
266-
<div class=issue>???</div>
262+
<td>invalid (a keyword, but see prose)
267263

268264
<tr>
269265
<th>Applies To:
@@ -286,6 +282,22 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
286282
<td>all
287283
</table>
288284

285+
<p><em>Any</em> property name starting with the prefix "data-" is a <a
286+
href="#data-property"><i>data property</i></a>. <span class=issue>Spec
287+
what values a data property can have.</span> Data properties are defined
288+
to be valid but meaningless as they are meant solely for allowing authors
289+
to pass custom data around their page, similar to the <a
290+
href="http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data-with-the-data-*-attributes">custom
291+
data attributes</a> in HTML. Other specifications and user agents must not
292+
assign a particular meaning to data properties or attach a specific effect
293+
to them beyond the bare minimum that comes from them being valid
294+
properties.
295+
296+
<p>For each <a href="#data-property"><i>data property</i></a>, there is an
297+
associated variable with the same name save for the "data-" prefix. For
298+
example, a data property named "data-foo" is associated with the variable
299+
named "foo". See the next chapter for details on how to use variables.
300+
289301
<div class=example>
290302
<p>This style rule:</p>
291303

@@ -297,8 +309,8 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
297309
<p>declares a <a href="#data-property"><i>data property</i></a> named
298310
"data-header-color" on the root element, and assigns to it the value
299311
"#06c". This property is then inherited to the elements in the rest of
300-
the document, and its value can then be used in any place where it would
301-
be valid, such as:
312+
the document. Its value can be referenced via the "header-color"
313+
variable:
302314

303315
<pre>h1 { background-color: data(header-color); }</pre>
304316

@@ -307,45 +319,47 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
307319
name makes the origin of the color clearer, and if &lsquo;<code
308320
class=css>data(header-color)</code>&rsquo; is used elsewhere in the
309321
stylesheet, all of the uses can be updated at once by changing the
310-
variable declaration.</p>
322+
&lsquo;<code class=property>data-header-color</code>&rsquo; property on
323+
the root element.</p>
311324
</div>
312325

313-
<p>There is no restriction on the possible names for <a
314-
href="#data-property"><i>data properties</i></a>, except that they must
315-
start with "data-" and be a valid identifier like any other property name.
316-
Data properties are ordinary properties, so they can be declared on any
326+
<p>Data properties are ordinary properties, so they can be declared on any
317327
element, are resolved with the normal inheritance and cascade rules, can
318328
be made conditional with &lsquo;<code class=css>@media</code>&rsquo; and
319329
other conditional rules, can be used in HTML's <code>style</code>
320330
attribute, can be read or set using the CSSOM, etc..
321331

322332
<div class=example>
323-
<p>If a variable is declared multiple times, the standard cascade rules
324-
help resolve it:</p>
333+
<p>If a <a href="#data-property"><i>data property</i></a> is declared
334+
multiple times, the standard cascade rules help resolve it. Variables
335+
always draw from the computed value of the associated data property on
336+
the same element:</p>
325337

326338
<pre>
327339
:root { data-color: blue; }
328340
div { data-color: green; }
329341
#alert { data-color: red; }
330342
* { color: data(color); }
331343

332-
&lt;p>I'm blue!&lt;/p>
333-
&lt;div>I'm green!&lt;/div>
344+
&lt;p>I inherited blue from the root element!&lt;/p>
345+
&lt;div>I got green set directly on me!&lt;/div>
334346
&lt;div id='alert'>
335-
And I'm red!
336-
&lt;p>So am I, because of inheritance!&lt;/p>
347+
While I got red set directly on me!
348+
&lt;p>I'm red too, because of inheritance!&lt;/p>
337349
&lt;/div></pre>
338350
</div>
339351

340352
<p><a href="#data-property"><i>Data properties</i></a> may use variables in
341353
their own values to build up composite variables. This can create cyclic
342-
dependencies where two or more variables attempt to use each other's
343-
value; doing so makes all the data properties involved in the cycle define
344-
<a href="#invalid-variable"><i>invalid variables</i></a> instead of the
354+
dependencies where two or more <a href="#data-property"><i>data
355+
properties</i></a> each attempt to use the variable that the other
356+
defines; doing so makes all the <a href="#data-property"><i>data
357+
properties</i></a> involved in the cycle define <a
358+
href="#invalid-variable"><i>invalid variables</i></a> instead of the
345359
values they had intended to define.
346360

347361
<div class=example>
348-
<p>This example shows a variable safely depending on another:</p>
362+
<p>This example shows a data property safely using a variable:</p>
349363

350364
<pre>
351365
:root {
@@ -396,13 +410,36 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
396410
class=css>30px</code>&rsquo;. Right?</p>
397411
</div>
398412

399-
<p>Variables can refer to other variables in their value. If a dependency
400-
cycle is created, all the declarations that directly contribute to the
401-
cycle define <a href="#invalid-variable"><i>invalid variables</i></a>.
413+
<p>The initial value of a <a href="#data-property"><i>data property</i></a>
414+
is a special invalid value which makes the associated variable an <a
415+
href="#invalid-variable"><i>invalid variable</i></a>. This is represented
416+
by the keyword &lsquo;<code class=css>invalid</code>&rsquo;, but that
417+
keyword has no special meaning in itself, and is valid if set explicitly
418+
in a data property.
402419

403-
<p>Before they are defined by a <a href="#data-property"><i>data
404-
property</i></a>, all variables are <a href="#invalid-variable"><i>invalid
405-
variables</i></a>.
420+
<div class=example>
421+
<pre>
422+
&lt;div>
423+
&lt;p>Sample text&lt;/p>
424+
&lt;/div>
425+
&lt;style>
426+
p { data-foo: invalid; }
427+
div,p { font-family: data(foo); }
428+
&lt;/style></pre>
429+
430+
<p>In this example, the "foo" variable is an <a
431+
href="#invalid-variable"><i>invalid variable</i></a> at the time the DIV
432+
element references it, because the &lsquo;<code
433+
class=property>data-foo</code>&rsquo; property still has its initial
434+
value. This causes the DIV's &lsquo;<code
435+
class=property>font-family</code>&rsquo; property to compute to the
436+
initial value for &lsquo;<code class=property>font-family</code>&rsquo;.</p>
437+
438+
<p>On the other hand, the P element defines an explicit value for the
439+
&lsquo;<code class=property>data-foo</code>&rsquo; property. Its
440+
&lsquo;<code class=property>font-family</code>&rsquo; property thus
441+
references a font named "invalid".</p>
442+
</div>
406443

407444
<h2 id=using-variables><span class=secno>3. </span> Using Variables</h2>
408445

@@ -967,8 +1004,6 @@ <h2 class=no-num id=index>Index</h2>
9671004
title="style sheet, as conformance class"><strong>6.2.</strong></a>
9681005
</ul>
9691006

970-
<li>variable, <a href="#variable" title=variable><strong>2.</strong></a>
971-
9721007
<li><a href="#variablerule"><code>VARIABLE_RULE</code></a>, <a
9731008
href="#variablerule" title="VARIABLE_RULE"><strong>4.2.1.</strong></a>
9741009
</ul>
@@ -1000,7 +1035,7 @@ <h2 class=no-num id=property-index>Property index</h2>
10001035

10011036
<td>Figure this out - restricted or wide-open
10021037

1003-
<td>???
1038+
<td>invalid (a keyword, but see prose)
10041039

10051040
<td>all elements
10061041

css-variables/Overview.src.html

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ <h3 id="values">
8080
definition of the &lt;color&gt; value type as used in this specification.</p>
8181

8282
<h2 id="defining-variables">
83-
Defining Variables</h2>
83+
Defining Variables With Data Properties</h2>
8484

85-
<p>A <dfn>variable</dfn> is defined by assigning a value to a property whose name starts with the prefix "data-" (this class of properties will be referred to <i>data properties</i>). <em>Any</em> property with the "data-" prefix is defined to be valid but meaningless, and will never be given a specific meaning or effect by a CSS specification or user agent; this is similar to the <code>data-*</code> family of custom attributes in HTML.</p>
85+
<p>This specification defines an open-ended set of properties called <i>data properties</i>, which are used to define <i>variables</i>.</p>
8686

8787
<table class='propdef'>
8888
<tr>
@@ -93,7 +93,7 @@ <h2 id="defining-variables">
9393
<td><div class='issue'>Figure this out - restricted or wide-open</div>
9494
<tr>
9595
<th>Initial:
96-
<td><div class='issue'>???</div>
96+
<td>invalid (a keyword, but see prose)
9797
<tr>
9898
<th>Applies To:
9999
<td>all elements
@@ -108,6 +108,10 @@ <h2 id="defining-variables">
108108
<td>all
109109
</table>
110110

111+
<p><em>Any</em> property name starting with the prefix "data-" is a <i>data property</i>. <span class='issue'>Spec what values a data property can have.</span> Data properties are defined to be valid but meaningless as they are meant solely for allowing authors to pass custom data around their page, similar to the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#embedding-custom-non-visible-data-with-the-data-*-attributes">custom data attributes</a> in HTML. Other specifications and user agents must not assign a particular meaning to data properties or attach a specific effect to them beyond the bare minimum that comes from them being valid properties.</p>
112+
113+
<p>For each <i>data property</i>, there is an associated variable with the same name save for the "data-" prefix. For example, a data property named "data-foo" is associated with the variable named "foo". See the next chapter for details on how to use variables.</p>
114+
111115
<div class=example>
112116
<p>This style rule:</p>
113117

@@ -116,36 +120,36 @@ <h2 id="defining-variables">
116120
data-header-color: #06c;
117121
}</pre>
118122

119-
<p>declares a <i>data property</i> named "data-header-color" on the root element, and assigns to it the value "#06c". This property is then inherited to the elements in the rest of the document, and its value can then be used in any place where it would be valid, such as:
123+
<p>declares a <i>data property</i> named "data-header-color" on the root element, and assigns to it the value "#06c". This property is then inherited to the elements in the rest of the document. Its value can be referenced via the "header-color" variable:
120124

121125
<pre>h1 { background-color: data(header-color); }</pre>
122126

123-
<p>The preceding rule is equivalent to writing ''background-color: #06c;'', except that the variable name makes the origin of the color clearer, and if ''data(header-color)'' is used elsewhere in the stylesheet, all of the uses can be updated at once by changing the variable declaration.</p>
127+
<p>The preceding rule is equivalent to writing ''background-color: #06c;'', except that the variable name makes the origin of the color clearer, and if ''data(header-color)'' is used elsewhere in the stylesheet, all of the uses can be updated at once by changing the 'data-header-color' property on the root element.</p>
124128
</div>
125129

126-
<p>There is no restriction on the possible names for <i>data properties</i>, except that they must start with "data-" and be a valid identifier like any other property name. Data properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with ''@media'' and other conditional rules, can be used in HTML's <code>style</code> attribute, can be read or set using the CSSOM, etc..</p>
130+
<p>Data properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with ''@media'' and other conditional rules, can be used in HTML's <code>style</code> attribute, can be read or set using the CSSOM, etc..</p>
127131

128132
<div class='example'>
129-
<p>If a variable is declared multiple times, the standard cascade rules help resolve it:</p>
133+
<p>If a <i>data property</i> is declared multiple times, the standard cascade rules help resolve it. Variables always draw from the computed value of the associated data property on the same element:</p>
130134

131135
<pre>
132136
:root { data-color: blue; }
133137
div { data-color: green; }
134138
#alert { data-color: red; }
135139
* { color: data(color); }
136140

137-
&lt;p>I'm blue!&lt;/p>
138-
&lt;div>I'm green!&lt;/div>
141+
&lt;p>I inherited blue from the root element!&lt;/p>
142+
&lt;div>I got green set directly on me!&lt;/div>
139143
&lt;div id='alert'>
140-
And I'm red!
141-
&lt;p>So am I, because of inheritance!&lt;/p>
144+
While I got red set directly on me!
145+
&lt;p>I'm red too, because of inheritance!&lt;/p>
142146
&lt;/div></pre>
143147
</div>
144148

145-
<p><i>Data properties</i> may use variables in their own values to build up composite variables. This can create cyclic dependencies where two or more variables attempt to use each other's value; doing so makes all the data properties involved in the cycle define <i>invalid variables</i> instead of the values they had intended to define.</p>
149+
<p><i>Data properties</i> may use variables in their own values to build up composite variables. This can create cyclic dependencies where two or more <i>data properties</i> each attempt to use the variable that the other defines; doing so makes all the <i>data properties</i> involved in the cycle define <i>invalid variables</i> instead of the values they had intended to define.</p>
146150

147151
<div class='example'>
148-
<p>This example shows a variable safely depending on another:</p>
152+
<p>This example shows a data property safely using a variable:</p>
149153

150154
<pre>
151155
:root {
@@ -180,9 +184,23 @@ <h2 id="defining-variables">
180184
<p>If I'm thinking correctly, one and two would get the value ''10px'' for ''data(foo)'', but three overrides 'data-foo' with a new definition. It gets the *inherited* value of 'data-bar', which is ''calc(10px + 10px)'' (or maybe just ''20px'' - ''calc()'' computed values aren't well defined yet), so it seems like it validly sets 'data-foo' to ''30px''. Right?</p>
181185
</div>
182186

183-
<p>Variables can refer to other variables in their value. If a dependency cycle is created, all the declarations that directly contribute to the cycle define <i>invalid variables</i>.</p>
187+
<p>The initial value of a <i>data property</i> is a special invalid value which makes the associated variable an <i>invalid variable</i>. This is represented by the keyword ''invalid'', but that keyword has no special meaning in itself, and is valid if set explicitly in a data property.</p>
188+
189+
<div class='example'>
190+
<pre>
191+
&lt;div>
192+
&lt;p>Sample text&lt;/p>
193+
&lt;/div>
194+
&lt;style>
195+
p { data-foo: invalid; }
196+
div,p { font-family: data(foo); }
197+
&lt;/style></pre>
198+
199+
<p>In this example, the "foo" variable is an <i>invalid variable</i> at the time the DIV element references it, because the 'data-foo' property still has its initial value. This causes the DIV's 'font-family' property to compute to the initial value for 'font-family'.</p>
200+
201+
<p>On the other hand, the P element defines an explicit value for the 'data-foo' property. Its 'font-family' property thus references a font named "invalid".</p>
202+
</div>
184203

185-
<p>Before they are defined by a <i>data property</i>, all variables are <i>invalid variables</i>.</p>
186204

187205
<h2 id='using-variables'>
188206
Using Variables</h2>

0 commit comments

Comments
 (0)