CSS is wonderful. It can put clothes on your page, all kinds of colorful clothes. However, for developers, it is not flexible enough, so there are all kinds of clothes. Such preprocessors include Sass, LESS, and Stylus (the author recommends Sass's SCSS syntax). These preprocessors can greatly improve the flexibility of CSS and enhance the code organization and maintenance of CSS. But after all, they are not biologically related. Then the experts in charge of CSS development couldn't sit still and began to give some new features to CSS. This article will discuss the progress and application of CSS variables.
W3C’s latest exploration of CSS variables is reflected in CSS Custom Properties for Cascading Variables Module Level 1. The latest update was on May 6, 2014. It is currently a working draft, so this article is only for research and exploration. There is a risk of modification to the knowledge points involved. This article will be updated simultaneously after W3C modifications.
We know that a website or application often contains a large amount of CSS, and some attribute values in these CSS are often reused, such as the color system used by the website, background color, text Color, link color, etc. These reused attribute values are scattered in a large number of CSS documents. If you need to modify some of the values, such as changing the color, it is simply a nightmare. We need to find and replace each file, which is dizzying and mechanical. Repeated large-scale operations will inevitably lead to errors and headaches, so the organization and maintenance of CSS code has become an important bottleneck that plagues the use of CSS.
Of course, CSS preprocessing can effectively solve these problems. The use of preprocessors has become the de facto industry standard, and Sass is becoming the choice of more and more front-end ers. However, we need to deploy a compilation environment and enable development tools to support Sass, etc., which requires some additional work. When things happen, we can’t help but wonder, what if one day, we can use preprocessors like ordinary CSS?
So there is W3C’s CSS Various.
About variables, it is nothing more than definition and use. Next, we will analyze them separately.
We store the values that need to be reused in a custom attribute. This custom attribute is marked with a double dotted line at the beginning, as shown in the following code.
/* 变量定义 */:root { --main-color: #06c;}/* 变量使用 */#foo h1 { color: var(--main-color);}
Custom attribute names and variable names follow the definition rules of CSS identifiers and can contain alphanumeric (a-z, A-Z, 0-9), ISO 10646 character list U 00A0 and above characters, underscore (_), hyphen (-), etc., cannot start with numbers, hyphen numbers, or double hyphens.
CSS variable syntax is case-sensitive and can contain letters, numbers, underscores, and hyphens, and it is best not to start with numbers or hyphens.
/* 正确的变量名 */:root{ --link-color: #06c; --_hover-color: #f6c; --toolTip_color: #ff0; --main_background_color: #333;}/* 下面两个变量都有效,表示两个变量 */:element{ --link-color:#06c; --Link-color:#66c;}
The variable value can accept any value that conforms to the grammar. Its default value is " ", but it cannot be "", otherwise an error will be reported.
The declaration and use of variables follow the cascading characteristics of CSS, similar to the variable scope in ordinary programming languages. Please look at the code below.
:root { --color: blue; }div { --color: green; }#alert { --color: red; }* { color: var(--color); }<p>I inherited blue from the root element!</p><div>I got green set directly on me!</div><div id='alert'> While I got red set directly on me! <p>I’m red too, because of inheritance!</p></div>
We can use variables through var(), but we cannot use variables in attribute names and selectors. We can only use variables in attribute values. Use The syntax of the variable is as follows.
/* * var()接受两个参数,自定义属性名(变量名)、缺省值 * 第一个参数custom-property-name调用变量 * 第二参数可选,指定自定义变量无效时的缺省值。 */var() = var( <custom-property-name> [, <any-value> ]? )/* for example *//* 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 */}
We can nested calls to CSS variables.
/*one example */:root { --main-color: #c06; --accent-background: linear-gradient(to top, var(--main-color), white);}/*another example */<one><two><three /></two></one>one { --foo: 10px; }two { --bar: calc(var(--foo) + 10px); }three { --foo: calc(var(--bar) + 10px); }
For front-end developers, compatibility and inclusion are always an unattainable pain. As a front-end er with a "geek mentality", we still have to work hard.
First of all, let’s take a look at the current appalling compatibility of CSS variables, as shown in the figure below, the data comes from caniuse.
The whole line is popular but only firefox knows it, so how can I play it?
What should I do if a brother says that I want to be willful and have fun? A foreigner wrote a patch (polyfill), you might as well give it a try.
To sum up, CSS variables are in the draft stage and browser compatibility is average. I am writing this article today just to broaden my horizons and reserve knowledge. For commercial use, please use preprocessors such as Sass or LESS.
For more details, use the list below to learn more.