A variable is a good thing
In any language, the variable has a bit of the same effect, that is to reduce maintenance costs, accompanied by higher performance, the benefits of higher file compression ratio.
With the CSS precompiled tool Sass/less/stylus and gradually popular, CSS Working group quickly follow up the specification of CSS variables, and, many browsers have been followed, currently, in some projects can be directly used.
Chrome/firefox/safari browsers are green, compatibility is much more than I expected, so decisive early adopters to record the syntax usage and characteristics.
Second, the CSS variable var () syntax and usage and characteristics
The syntax for a variable definition that is native to CSS is: The --* variable uses the syntax: var(--*) , which * represents our variable name. About naming this thing, a variety of languages have some display, such as CSS selectors can not be the beginning of the number, JS variables are not direct values, but in CSS variables, these restrictions are not, for example:
: root { --1: #369;} Body { Background-color:var (--1);}
The resulting background color is as follows:
However, cannot contain $ ,,, [ ^ ( , % etc characters, the normal characters are limited to these combinations as long as "number", "Letter" " [0-9] [a-zA-Z] underline _ " and "dash" - , but can be Chinese, Japanese or Korean, For example:
Body { --deep Blue: #369; Background-color:var (--Deep Blue);}
Therefore, we can directly use the Chinese name as a variable, even if the English level 4 has not been a small partner will not have pressure, we do not need to hang a translator at any time around.
Both the definition and use of a variable can only be in the declaration block {} , for example, the following is not valid:
--Navy: #369; body { Background-color:var (--Deep Blue);}
The definition of a variable, or a declaration similar to the declaration of a CSS counter, you should get rid of the pre-compiler tool syntax, such as sass/less, and interpret the native variables of CSS as a CSS property.
In this way, you can apply rules to their weights and variables much easier to understand.
For example, the following example:
--color: Purple}--color: Green}--color: Red}colorvar(--color }<p> My purple inheritance root element </p><div> My green from direct settings </div><div id= ' alert ' > ID selector weights higher, So Allah is red! <p> I was also red, which accounted for the inherited light </p></div>
We can get this information from the above example:
- Variables are also followed by the CSS selector, which has no effect if the selector of the variable and the element that uses the variable have no intersection. For example
#alert , a defined variable id can only be alert enjoyed by an element. If you want the variable to be used globally, you can set it on the :root selector;
- When there are multiple variables of the same name, the override rule of the variable is determined by the weight of the CSS selector, but there is no
!important such use, because it is not necessary to !important kill the JS style setting, but there is no requirement for the definition of the variable.
CSS property name can walk variable?
This is similar to the following:
body { --bc:background-color; var (--BC): #369;}
The answer is "no", if you can support it, then the compression of the CSS will be the inverse of the day, it is estimated that all the attributes will become one or two characters.
Does the CSS variable support multiple declarations at the same time?
This is similar to the following:
...
Sorry, not similar, grammar is not supported at all.
CSS variables use full syntax
The complete syntax for using CSS variables is: var( [, ]? ) , in Chinese, is: var( <自定义属性名> [, <默认值 ]? ) ,
This means that if the variable we are using is not defined (note that it is limited to no definition), then the value that follows is used as the attribute value of the element. As an example:
. box { --1: #369;} Body { Background-color:var (--1, #cd0000);}
The background color at this point is #cd0000 :
Default property for CSS variables not valid
Take a look at the following example:
body { --color:20px; Background-color: #369; Background-color:var (--color, #cd0000);}
Excuse me, <body> what is the background color at this time?
A. Transparent B. 20px c. #369 D. #cd0000
The answer is ......... ............. A. Transparent
I don't know if you got it right!
This is a very interesting point of CSS variables, for CSS variables, as long as the syntax is correct, even if the value inside the variable is a messy thing, but also as a normal declaration resolution, if the value of the variable is found to be illegal, such as the above background color is obviously not 20px , then use the background color of the default value, This is the default value instead, so the above CSS is equivalent to:
Body {
--color:20px;
Background-color: #369;
Background-color:transparent;
}
Do not assume that the equivalent background-color:20px , this is why the above to emphasize the use of CSS default values are limited to variables not defined, not including the variable is not legal.
Space trailing properties of CSS variables More Questions Css/css3 Continue reading
Take a look at the following example:
body { --size:20; Font-size:var (--size) px;}
Excuse me, what <body> is the size at this time font-size ?
If you think it's 20px naïve, actually, here's the font-size:var(--size)px equivalent font-size:20 px , notice that 20 there's a space behind it, so here's the font-size default size of the <body> element. Therefore, do not attempt to cancel the use of a numeric value to run through the entire audience, or use a sound approach:
body { --size:20px; Font-size:var (--size);}
or use CSS3 to calc() calculate:
body { --size:20; Font-size:calc (VAR (--size) * 1px);}
At this point, <body> the font-size size is 20px ,
Mutual transitive properties of CSS variables
That is, we can introduce other variables to our own use when we define CSS variables, for example:
Body { --green: #4CAF50; --backgroundcolor:var (--green);}
or more complex calculations using CSS3 calc() , for example:
body { --columns:4; --margins:calc (24px/var (--columns));}
For complex layouts, this cross-passing and direct reference to CSS variables can simplify our code and implementation costs, especially when it comes to dynamic layout, whether the CSS is responsive or the JS-driven layout changes.
Let's look at a CSS variable and a responsive layout example, you can click here: CSS variable and responsive layout instance demo
The default is 4 columns, such as:
As the browser width decreases, the 4 bar may become 3 columns, 2 columns and even 1 columns, when we actually develop, obviously not only the column number changes, the width is small, often means the access device size is limited, at this time we tend to reduce the gap spacing and text size, so that, Limited screen to show more content.
In other words, when we change the response, the value of the CSS property changed is not 1, but 3 or more, if we have 3 response points, is not at least 9 CSS declaration? However, since we have CSS variables, and CSS variables can be passed, when we encounter the response point, we only need to change a CSS property value.
Here is the demo core CSS code (only need to change --columns this one variable):
. box { --columns:4; --margins:calc (24px/var (--columns)); --space:calc (4PX * VAR (--columns)); --fontsize:calc (20px-4/var (--columns));} @media screen and (max-width:1200px) { . box { --columns:3; }} @media screen and (max-width:900px) { . box { --columns:2; }} @media screen and (max-width:600px) { . box { --columns:1; }}
So, we are in the 2 column effect is this, the size, spacing as the number of columns decreased also reduced, and then the spacing between each column is enlarged:
Have you ever felt that the CSS is getting cock! haha ~
Iii. concluding remarks
Since there are almost no articles on CSS3, the var() grammatical features described above are var() obtained by looking at the canonical document, plus detailed tests. However, a person's ability is always limited, so there must be a lot of var() variables interesting points did not find, therefore, we hope that if you find var() other interesting places, welcome comments to inform, we add in the article in time, convenient for you I he she it.
Understanding CSS/CSS3 Native Variable var