Skip to content

Commit 652b759

Browse files
committed
Finish rewrite of 'Defining Variables' section to the new data-properties form.
1 parent 8446c5f commit 652b759

2 files changed

Lines changed: 143 additions & 113 deletions

File tree

css-variables/Overview.html

Lines changed: 96 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222

2323
<h1>CSS Variables Module Level 1</h1>
2424

25-
<h2 class="no-num no-toc" id=longstatus-date>Editor's Draft 26 October
25+
<h2 class="no-num no-toc" id=longstatus-date>Editor's Draft 27 October
2626
2011</h2>
2727

2828
<dl>
2929
<dt>This version:
3030

3131
<dd><a
32-
href="http://www.w3.org/TR/2011/ED-css-variables-20111026/">http://dev.w3.org/csswg/css-variables/</a>
32+
href="http://www.w3.org/TR/2011/ED-css-variables-20111027/">http://dev.w3.org/csswg/css-variables/</a>
3333
<!--<dt>Latest version:
3434
<dd><a href="http://www.w3.org/TR/css-variables/">http://www.w3.org/TR/css-variables/</a>-->
3535

@@ -208,18 +208,19 @@ <h2 id=intro><span class=secno>1. </span> Introduction</h2>
208208
across multiple files), and may not be amenable to Find-and-Replace.
209209

210210
<p>This module introduces a family of custom user-defined properties known
211-
collectively as <b>Variables</b>, which allow an author to assign values
212-
to a property with an author-chosen name, and then use those values in
213-
other properties elsewhere in the document. This makes it easier to read
214-
large files, as seemingly-arbitrary values now have informative names, and
215-
makes editting such files much easier and less error-prone, as one only
216-
has to change the value once, at the variable definition site, and the
217-
change will propagate to all uses of that variable automatically.
211+
collectively as <a href="#data-property"><i>data properties</i></a>, which
212+
allow an author to assign arbitrary values to a property with an
213+
author-chosen name, and then use those values in other properties
214+
elsewhere in the document. This makes it easier to read large files, as
215+
seemingly-arbitrary values now have informative names, and makes editting
216+
such files much easier and less error-prone, as one only has to change the
217+
value once, at the variable definition site, and the change will propagate
218+
to all uses of that variable automatically.
218219

219220
<h3 id=placement><span class=secno>1.1. </span> Module Interactions</h3>
220221

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

224225
<h3 id=values><span class=secno>1.2. </span> Values</h3>
225226

@@ -286,7 +287,7 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
286287
</table>
287288

288289
<div class=example>
289-
<p>This variables declaration:</p>
290+
<p>This style rule:</p>
290291

291292
<pre>
292293
:root {
@@ -309,89 +310,99 @@ <h2 id=defining-variables><span class=secno>2. </span> Defining Variables</h2>
309310
variable declaration.</p>
310311
</div>
311312

312-
<p class=issue>Finish replacing the rest of this section.
313-
314-
<p>Defined variables are available to all stylesheets in the document.
315-
Thus, using &lsquo;<code class=css>@import</code>&rsquo; to include a
316-
stylesheet also includes all variables defined in the stylesheet (and
317-
makes all variables declared outside available within the stylesheet, if
318-
applicable). &lsquo;<code class=css>@var</code>&rsquo; rules present in
319-
disabled or alternate stylesheets do not define variables. Scoped
320-
stylesheets are an exception to this: variables defined in the global
321-
scope are available within a scoped stylesheet, but variables defined or
322-
imported within scoped stylesheets are only available within the scoped
323-
stylesheet and any imported stylesheets.
324-
325-
<p>A &lsquo;<code class=css>@var</code>&rsquo; rule is <dfn
326-
id=inactive-var-rule>inactive</dfn> if it's present in a disabled or
327-
alternate stylesheet, a stylesheet with a media query that evaluates to
328-
false, or is present within a conditional rule group [[CSS3CONDITIONAL]]
329-
whose condition evaluates to false. <a
330-
href="#inactive-var-rule"><i>Inactive</i></a> &lsquo;<code
331-
class=css>@var</code>&rsquo; rules do not define variables. All other
332-
&lsquo;<code class=css>@var</code>&rsquo; rules are <dfn
333-
id=active-var-rule>active</dfn>.
334-
335-
<p>If new &lsquo;<code class=css>@var</code>&rsquo; rules are added to the
336-
document through any means, such as direct CSSOM manipulation or new
337-
stylesheets being added to the document, or if an <a
338-
href="#inactive-var-rule"><i>inactive</i></a> &lsquo;<code
339-
class=css>@var</code>&rsquo; rule becomes <a
340-
href="#active-var-rule"><i>active</i></a>, they define variables.
341-
Similarly, if &lsquo;<code class=css>@var</code>&rsquo; rules are removed
342-
from the document or become <a
343-
href="#inactive-var-rule"><i>inactive</i></a>, they no longer define
344-
variables.
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
317+
element, are resolved with the normal inheritance and cascade rules, can
318+
be made conditional with &lsquo;<code class=css>@media</code>&rsquo; and
319+
other conditional rules, can be used in HTML's <code>style</code>
320+
attribute, can be read or set using the CSSOM, etc..
321+
322+
<div class=example>
323+
<p>If a variable is declared multiple times, the standard cascade rules
324+
help resolve it:</p>
325+
326+
<pre>
327+
:root { data-color: blue; }
328+
div { data-color: green; }
329+
#alert { data-color: red; }
330+
* { color: data(color); }
331+
332+
&lt;p>I'm blue!&lt;/p>
333+
&lt;div>I'm green!&lt;/div>
334+
&lt;div id='alert'>
335+
And I'm red!
336+
&lt;p>So am I, because of inheritance!&lt;/p>
337+
&lt;/div></pre>
338+
</div>
345339

346-
<p>If multiple <a href="#active-var-rule"><i>active</i></a> &lsquo;<code
347-
class=css>@var</code>&rsquo; rules attempt to define variables with the
348-
same name, the last one defined wins; the preceding &lsquo;<code
349-
class=css>@var</code>&rsquo; rules do not define a variable. For this
350-
purpose, user-agent defined variable rules come before author-defined
351-
variable rules, which come before user-defined variable rules, which come
352-
before override variable rules. Within each of the preceding groups, the
353-
ordering is document order.
340+
<p><a href="#data-property"><i>Data properties</i></a> may use variables in
341+
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
345+
values they had intended to define.
354346

355347
<div class=example>
356-
<p>Earlier declarations of the same variable have no effect, even if uses
357-
of the variable come between the earlier and final attempts to define the
358-
variable:</p>
348+
<p>This example shows a variable safely depending on another:</p>
359349

360350
<pre>
361-
@var $var red;
362-
p { color: $var; }
363-
@var $var blue;</pre>
351+
:root {
352+
data-main-color: #c06;
353+
data-accent-background: linear-gradient(to top, data(main-color), white);
354+
}</pre>
364355

365-
<p>In this example, the color of &lt;p> elements is blue, because the
366-
second declaration wins over the first declaration in all uses.</p>
356+
<p>The &lsquo;<code class=property>data-accent-background</code>&rsquo;
357+
property will automatically update when the &lsquo;<code
358+
class=property>data-main-color</code>&rsquo; property is changed.</p>
367359
</div>
368360

369-
<p>Variables can refer to other variables in their value. If a dependency
370-
cycle is created, all the declarations that directly contribute to the
371-
cycle define <a href="#invalid-variable"><i>invalid variables</i></a>.
361+
<div class="example invalid-example">
362+
<p>On the other hand, this example shows an invalid instance of variables
363+
depending on each other:</p>
372364

373-
<div class=example>
374-
<p>For example, the following set of variable declarations create a
375-
dependency cycle:</p>
365+
<pre>
366+
:root {
367+
data-one: calc(data(two) + 20px);
368+
data-two: calc(data(one) - 20px);
369+
}</pre>
370+
371+
<p>Both &lsquo;<code class=property>data-one</code>&rsquo; and
372+
&lsquo;<code class=property>data-two</code>&rsquo; now define <a
373+
href="#invalid-variable"><i>invalid variables</i></a> rather than
374+
lengths.</p>
375+
</div>
376+
377+
<div class=issue>
378+
<p>Is it possible to do a "cyclic" dependency safely?</p>
376379

377380
<pre>
378-
@var $foo red;
379-
@var $bar linear-gradient(transparent, $foo);
380-
@var $foo $bar;</pre>
381-
382-
<p>The last two declarations create the cycle, and so they define
383-
&lsquo;<code class=css>$bar</code>&rsquo; and &lsquo;<code
384-
class=css>$foo</code>&rsquo;, respectively, as <a
385-
href="#invalid-variable"><i>invalid variables</i></a> instead of as the
386-
values in the declarations. If the last rule were removed, however, there
387-
would be no problem - &lsquo;<code class=css>$foo</code>&rsquo; would be
388-
defined as &lsquo;<code class=css>red</code>&rsquo;, while &lsquo;<code
389-
class=css>$bar</code>&rsquo; would be defined as &lsquo;<code
390-
class=css>linear-gradient(transparent, red)</code>&rsquo;.</p>
381+
&lt;one>&lt;two>&lt;three />&lt;/two>&lt;/one>
382+
one { data-foo: 10px; }
383+
two { data-bar: calc(data(foo) + 10px); }
384+
three { data-foo: calc(data(bar) + 10px); }</pre>
385+
386+
<p>If I'm thinking correctly, one and two would get the value &lsquo;<code
387+
class=css>10px</code>&rsquo; for &lsquo;<code
388+
class=css>data(foo)</code>&rsquo;, but three overrides &lsquo;<code
389+
class=property>data-foo</code>&rsquo; with a new definition. It gets the
390+
*inherited* value of &lsquo;<code class=property>data-bar</code>&rsquo;,
391+
which is &lsquo;<code class=css>calc(10px + 10px)</code>&rsquo; (or maybe
392+
just &lsquo;<code class=css>20px</code>&rsquo; - &lsquo;<code
393+
class=css>calc()</code>&rsquo; computed values aren't well defined yet),
394+
so it seems like it validly sets &lsquo;<code
395+
class=property>data-foo</code>&rsquo; to &lsquo;<code
396+
class=css>30px</code>&rsquo;. Right?</p>
391397
</div>
392398

393-
<p>Before they are defined, all variables are <a
394-
href="#invalid-variable"><i>invalid variables</i></a>.
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>.
402+
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>.
395406

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

@@ -868,10 +879,10 @@ <h3 class=no-num id=normative-references>Normative references</h3>
868879
<dt id=CSS21>[CSS21]
869880

870881
<dd>Bert Bos; et al. <a
871-
href="http://www.w3.org/TR/2011/REC-CSS2-20110607/"><cite>Cascading Style
882+
href="http://www.w3.org/TR/2011/REC-CSS2-20110607"><cite>Cascading Style
872883
Sheets Level 2 Revision 1 (CSS&#160;2.1) Specification.</cite></a> 7 June
873884
2011. W3C Recommendation. URL: <a
874-
href="http://www.w3.org/TR/2011/REC-CSS2-20110607/">http://www.w3.org/TR/2011/REC-CSS2-20110607/</a>
885+
href="http://www.w3.org/TR/2011/REC-CSS2-20110607">http://www.w3.org/TR/2011/REC-CSS2-20110607</a>
875886
</dd>
876887
<!---->
877888

@@ -916,9 +927,9 @@ <h3 class=no-num id=other-references>Other references</h3>
916927
<dt id=CSS3COLOR>[CSS3COLOR]
917928

918929
<dd>Tantek &#199;elik; Chris Lilley; L. David Baron. <a
919-
href="http://www.w3.org/TR/2011/REC-css3-color-20110607/"><cite>CSS Color
930+
href="http://www.w3.org/TR/2011/REC-css3-color-20110607"><cite>CSS Color
920931
Module Level 3.</cite></a> 7 June 2011. W3C Recommendation. URL: <a
921-
href="http://www.w3.org/TR/2011/REC-css3-color-20110607/">http://www.w3.org/TR/2011/REC-css3-color-20110607/</a>
932+
href="http://www.w3.org/TR/2011/REC-css3-color-20110607">http://www.w3.org/TR/2011/REC-css3-color-20110607</a>
922933
</dd>
923934
<!---->
924935
</dl>
@@ -928,9 +939,6 @@ <h2 class=no-num id=index>Index</h2>
928939
<!--begin-index-->
929940

930941
<ul class=indexlist>
931-
<li>active, <a href="#active-var-rule"
932-
title=active><strong>2.</strong></a>
933-
934942
<li>authoring tool, <a href="#authoring-tool"
935943
title="authoring tool"><strong>6.2.</strong></a>
936944

@@ -940,9 +948,6 @@ <h2 class=no-num id=index>Index</h2>
940948
<li>data property, <a href="#data-property"
941949
title="data property"><strong>2.</strong></a>
942950

943-
<li>inactive, <a href="#inactive-var-rule"
944-
title=inactive><strong>2.</strong></a>
945-
946951
<li>invalid at computed-value time, <a
947952
href="#invalid-at-computed-value-time"
948953
title="invalid at computed-value time"><strong>3.1.</strong></a>

0 commit comments

Comments
 (0)