Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1027260 of var()

  • Revision slug: Web/CSS/var()
  • Revision title: var()
  • Revision id: 1027260
  • Created:
  • Creator: rolfedh
  • Is current revision? No
  • Comment Editorial review: Needs tags. The definition of <custom-property-name> looks like it might require the author's attention. Also, the use of "production" seems odd/unfamiliar to me. Does it make sense to the intended audience?

Revision Content

{{CSSRef}}{{SeeCompatTable}}

Summary

The var() function can be used instead of any part of a value in any property on an element. The var() function can not be used as property names, selectors, or anything else besides property values. (Doing so usually produces invalid syntax, or else a value whose meaning has no connection to the variable.)

Syntax

The first argument to the function is the name of the custom property to be substituted. An optional second argument to the function serves as a fallback value. If the custom property referenced by the first argument is invalid, the function uses the second value.

Formal syntax: var() = var( <custom-property-name> [, <declaration-value> ]? )

Values

<custom-property-name>
The production corresponds to this: It’s defined as any valid identifier that starts with two dashes. Custom properties are solely for use by authors and users; CSS will never give them a meaning beyond what is presented here.
<declaration-value>
The <declaration-value> production matches any sequence of one or more tokens, so long as the sequence does not contain <bad-string-token>, <bad-url-token>, unmatched <)-token>, <]-token>, or <}-token>, or top-level <semicolon-token> tokens or <delim-token> tokens with a value of "!". It represents the entirety of what a valid declaration can have as its value.

Examples

:root{
  --main-bg-color: pink;
}

body {
  background-color: var(--main-bg-color);
}

/* fallback */
/* In the component’s style: */
.component .header {
  color: var(--header-color, blue);
}

.component .text {
  color: var(--text-color, black);
}

/* In the larger application’s style: */
.component {
  --text-color: #080; /* header-color isn’t set, and so remains blue, the fallback value */
}

Specifications

Specification Status Comment
{{SpecName('CSS3 Variables', '#using-variables', 'var()')}} {{Spec2('CSS3 Variables')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 48[2]
49
{{CompatGeckoDesktop("29")}}[1]
{{CompatGeckoDesktop("31")}}
{{CompatNo}} {{CompatNo}} 9.3[3]
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{CompatUnknown}} {{CompatGeckoDesktop("29")}} {{CompatUnknown}} {{CompatUnknown}} 9.3[3]

[1] This feature is implemented behind the preference layout.css.variables.enabled, defaulting to false and using the old var-variablename syntax in Gecko 29. Starting from Gecko 31 the preference is enabled by default and the new --variablename syntax is used.

[2] Chrome 48 enabled through the "Experimental Web Platform features" flag in chrome://flags.

[3] See {{webkitbug(19660)}}.

See also

Revision Source

<div>{{CSSRef}}{{SeeCompatTable}}</div>

<h2 id="Summary">Summary</h2>

<p>The var() function can be used instead of any part of a value in any property on an element. The var() function can not be used as property names, selectors, or anything else besides property values. (Doing so usually produces invalid syntax, or else a value whose meaning has no connection to the variable.)</p>

<h2 id="Syntax">Syntax</h2>

<p>The first argument to the function is the name of the custom property to be substituted. An optional second&nbsp;argument to the function serves as a <strong>fallback</strong> value. If the&nbsp;custom property referenced by the first argument is invalid, the function uses the second value.</p>

<pre class="syntaxbox">
<code><a href="/en-US/docs/Web/CSS/Value_definition_syntax">Formal syntax</a>: var() = var( &lt;custom-property-name&gt; [, &lt;declaration-value&gt; ]? )</code>
</pre>

<h3 id="Values">Values</h3>

<dl>
 <dt>&lt;custom-property-name&gt;</dt>
 <dd>The production corresponds to this: It’s defined as any valid identifier that starts with two dashes. Custom properties are solely for use by authors and users; CSS will never give them a meaning beyond what is presented here.</dd>
 <dt>&lt;declaration-value&gt;</dt>
 <dd>The &lt;declaration-value&gt; production matches any sequence of one or more tokens, so long as the sequence does not contain &lt;bad-string-token&gt;, &lt;bad-url-token&gt;, unmatched &lt;)-token&gt;, &lt;]-token&gt;, or &lt;}-token&gt;, or top-level &lt;semicolon-token&gt; tokens or &lt;delim-token&gt; tokens with a value of "!". It represents the entirety of what a valid declaration can have as its value.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<pre class="brush: css; highlight: language-css">
<code>:root{
  --main-bg-color: pink;
}

body {
  background-color: var(--main-bg-color);
}

</code></pre>

<pre class="brush: css; highlight: language-css">
<code>/* fallback */
/* In the component’s style: */
.component .header {
  color: var(--header-color, blue);
}

.component .text {
  color: var(--text-color, black);
}

/* In the larger application’s style: */
.component {
  --text-color: #080; /* header-color isn’t set, and so remains blue, the fallback value */
}
</code></pre>

<h2 id="Specifications">Specifications</h2>

<table>
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Variables', '#using-variables', 'var()')}}</td>
   <td>{{Spec2('CSS3 Variables')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>48<sup>[2]</sup><br />
    49</td>
   <td>{{CompatGeckoDesktop("29")}}<sup>[1]</sup><br />
    {{CompatGeckoDesktop("31")}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>9.3<sup>[3]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatGeckoDesktop("29")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>9.3<sup>[3]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] This feature is implemented behind the preference layout.css.variables.enabled, defaulting to false and using the old var-<var>variablename</var> syntax in Gecko 29. Starting from Gecko 31 the preference is enabled by default and the new --<var>variablename</var> syntax is used.</p>

<p>[2] Chrome 48 enabled through the "Experimental Web Platform features" flag in <code>chrome://flags</code>.</p>

<p>[3] See&nbsp;{{webkitbug(19660)}}.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/CSS/Using_CSS_variables">Using CSS variables</a></li>
</ul>
Revert to this revision