Thanks to the popularity of CSS pre-processors, CSS is starting to gain some more 'programmatic' features. The first of which is custom properties. They are more often referred to as variables although that is not necessarily their only use case. You can find the full specification at http://dev.w3.org/csswg/css-variables/. Be warned, as of early 2015, browser implementations are few and far between (only Firefox).
CSS custom properties allow us to store information in our style sheets that can then be utilized in that style sheet or perhaps acted upon with JavaScript. An obvious use case would be to store a font-family name and then reference it. Here is how we create a custom property:
:root {
--MainFont: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}Here, we are using the :root pseudo-class to store the custom property in the document root (although you can store them inside any rule you like).
The :root pseudo-class always references the top-most parent element in a document structure. In an HTML document this would always be the HTML tag but for an SVG document (we look at SVG in Chapter 7, Using SVGs for Resolution Independence), it would reference a different element.
A custom property always begins with two dashes, then the custom name, and then its end, signified like every other property in CSS; with a colon.
We can reference that value with the var() notation. Like so:
.Title {
font-family: var(--MainFont);
}You could obviously store as many custom properties as you need in this manner. The main benefit of this approach is that you can change the value inside the variable and every rule that makes use of the variable gets the new value without having to amend them directly.
It's envisaged that in future these properties might be parsed and utilized by JavaScript. For more on that kind of craziness, you might be interested in the new CSS Extensions module: