Turn https://www.h5jun.com/post/autoprefixing-with-css-variables-lea-verou.html
Recently, when I was making this little website of Markapp.io, I came up with a clever technique for using CSS variables that we could use naturally in their dynamic nature. Let's take a look when you want to use a property, but this property has a different version, a no-prefix standard version, and one or more prefixed versions of the case. Here I give an example, for example, we use clip-path
, currently need to use both the prefix version and -webkit-
the version of the suffix, my method can be applied to this situation, regardless of the CSS property is what, how many prefixes, as long as it no matter what the prefix value is the same.
The first step is to define a property on all elements --clip-path
, with a value of initial
. This prevents the attribute from being inherited every time it is used, and *
any declarations that are used --clip-path
can be overwritten because there is no specificity. Then you define the property names for all the different versions, with the values var(--clip-path)
:
{ --clip-path: initial; -webkit-clip-path: var (--clip-path); clip-path: var (--clip-path);}
In this way, where we need to use clip-path
it, we all use the --clip-path
alternative, which can work properly:
{ --clip-path: Polygon (0% 0, 100% 0, 100% Calc (100%-2.5em), 0% 100%);}
Even !important
all work as expected, because it affects the cascading of CSS variables. In addition, if for some reason you want to explicitly set -webkit-clip-path
, you can also write normally, which is also because it *
is 0 specificity (which means the lowest priority selector--the translator note). The main disadvantage of this usage is that the browser must support both the properties you use and the CSS variables to work properly. However, in addition to the edge, all browsers support CSS variables and the edge is ready to support it. In addition to the above problem, I found no other shortcomings (except obviously have to use a few different properties than the standard attribute), but if you have found other pits, please leave a message in the comments let me know!
I think there's a lot more to be explored about the clever use of CSS variables. I want to know if this technique can be improved to allow for custom CSS properties to be fully written, such as box-shadow
separating --box-shadow-x
and --box-shadow-y
so on, but I haven't thought of a good way yet. Do you have a good idea???
"Translating" skillfully using CSS variable to realize automatic prefix