1. Introduction
This section is not normative.
Large documents or applications (and even small ones) can contain quite a bit of CSS. Many of the values in the CSS file will be duplicate data; for example, a site may establish a color scheme and reuse three or four colors throughout the site. Altering this data can be difficult and error-prone, since it’s scattered throughout the CSS file (and possibly across multiple files), and may not be amenable to Find-and-Replace.
This module introduces a family of custom author-defined properties known collectively as custom properties, which allow an author to assign arbitrary values to a property with an author-chosen name, and the var() function, which allow an author to then use those values in other properties elsewhere in the document. This makes it easier to read large files, as seemingly-arbitrary values now have informative names, and makes editing such files much easier and less error-prone, as one only has to change the value once, in the custom property, and the change will propagate to all uses of that variable automatically.
1.1. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
2. Defining Custom Properties: the --* family of properties
This specification defines an open-ended set of properties called custom properties, which, among other things, are used to define the substitution value of var() functions.
| Name: | --* |
|---|---|
| Value: | <declaration-value>? |
| Initial: | the guaranteed-invalid value |
| Applies to: | all elements and all pseudo-elements (including those with restricted property lists) |
| Inherited: | yes |
| Percentages: | n/a |
| Computed value: | specified value with variables substituted, or the guaranteed-invalid value |
| Canonical order: | per grammar |
| Animation type: | discrete |
User agents are expected to support this property on all media, including non-visual ones.
A custom property is any property whose name starts with two dashes (U+002D HYPHEN-MINUS), like --foo. The <custom-property-name> production corresponds to this: it’s defined as any <dashed-ident> (a valid identifier that starts with two dashes), except -- itself, which is reserved for future use by CSS. Custom properties are solely for use by authors and users; CSS will never give them a meaning beyond what is presented here.
Tests
- variable-declaration-29.html (live test) (source)
- variable-declaration-31.html (live test) (source)
- variable-declaration-32.html (live test) (source)
- variable-declaration-33.html (live test) (source)
- variable-declaration-34.html (live test) (source)
- variable-declaration-35.html (live test) (source)
- variable-declaration-36.html (live test) (source)
- variable-declaration-40.html (live test) (source)
- variable-declaration-41.html (live test) (source)
- variable-declaration-42.html (live test) (source)
- variable-empty-name-reserved.html (live test) (source)
:root{ --main-color : #06c; --accent-color : #006; } /* The rest of the CSS file */ #foo h1{ color : var ( --main-color); }
The naming provides a mnemonic for the colors, prevents difficult-to-spot typos in the color codes, and if the theme colors are ever changed, focuses the change on one simple spot (the custom property value) rather than requiring many edits across all stylesheets in the webpage.
Unlike other CSS properties, custom property names are not ASCII case-insensitive. Instead, custom property names are only equal to each other if they are identical to each other.
Perhaps more surprisingly, --foó and --foó are distinct properties. The first is spelled with U+00F3 (LATIN SMALL LETTER O WITH ACUTE) while the second is spelled with an ASCII "o" followed by U+0301 (COMBINING ACUTE ACCENT), and the "identical to" relation uses direct codepoint-by-codepoint comparison to determine if two strings are equal, to avoid the complexities and pitfalls of unicode normalization and locale-specific collation.
Operating systems, keyboards, or input methods sometimes encode visually-identical text using different codepoint sequences. Authors are advised to choose variable names that avoid potential confusion or to use escapes and other means to ensure that similar appearing sequences are identical. See Section 2.3 in [CHARMOD-NORM] for examples.
--fijord : red; --fijord : green; --fijord : blue; .test{ background-color : var ( --fijord); }
The reason is that the first custom property uses the character sequence LATIN SMALL LETTER F + LATIN SMALL LETTER I + LATIN SMALL LETTER J; the second, identical-looking one uses the character sequence LATIN SMALL LETTER F + LATIN SMALL LIGATURE IJ while the third uses the character sequence LATIN SMALL LIGATURE FI + LATIN SMALL LETTER J.
So the CSS contains three distinct custom properties, two of which are unused.
Custom properties are not reset by the all property. We may define a property in the future that resets all variables.
The CSS-wide keywords can be used in custom properties, with the same meaning as in any another property.