Recently, when I was making the minisite for markapp.io, I realized a neat trick one can do with CSS variables, precisely due to their dynamic nature. Let’s say you want to use a property that has multiple versions: an unprefixed one and one or more prefixed ones. In this example we are going to use clip-path, which currently needs both an unprefixed version and a -webkit- prefixed one, however the technique works for any property and any number of prefixes or different property names, as long as the value is the same across all variations of the property name.
The first part is to define a --clip-path property on every element with a value of initial. This prevents the property from being inherited every time it’s used, and since the * has zero specificity, any declaration that uses --clip-path can override it. Then you define all variations of the property name with var(--clip-path) as their value:
* {
--clip-path: initial;
-webkit-clip-path: var(--clip-path);
clip-path: var(--clip-path);
}
Then, every time we need clip-path, we use –clip-path instead and it just works:
header {
--clip-path: polygon(0% 0%, 100% 0%, 100% calc(100% - 2.5em), 0% 100%);
}
Even !important should work, because it affects the cascading of CSS variables. Furthermore, if for some reason you want to explicitly set -webkit-clip-path, you can do that too, again because * has zero specificity. The main downside to this is that it limits browser support to the intersection of the support for the feature you are using and support for CSS Variables. However, all browsers except Edge support CSS variables, and Edge is working on it. I can’t see any other downsides to it (except having to use a different property name obvs), but if you do, let me know in the comments!
I think there’s still a lot to be discovered about cool uses of CSS variables. I wonder if there exists a variation of this technique to produce custom longhands, e.g. breaking box-shadow into --box-shadow-x, --box-shadow-y etc, but I can’t think of anything yet. Can you? 😉
Pingback: Autoprefixing with CSS Variables | PSD TO WORDPRESS()
Pingback: Autoprefixing with CSS Variables |()
Pingback: Webmasters GalleryAutoprefixing with CSS Variables | Webmasters Gallery()
Pingback: Autoprefixing with CSS Variables - p6design.net()
Pingback: Autoprefixing with CSS Variables - Codango.Com()
Pingback: Autoprefixing, with CSS variables! | Lea Verou ...()
Pingback: Autoprefixing with CSS Variables - ADD_dm()
Pingback: Autoprefixing with CSS Variables - Browser Zone()
Pingback: Dew Drop - September 9, 2016 (#2325) - Morning Dew()
Pingback: Autoprefixing with CSS Variables – ThinkTanks Blog()
Pingback: Collective #245 | PSD TO WORDPRESS()
Pingback: Collective #245 - ADD_dm()
Pingback: Collective #245 - p6design.net()
Pingback: Дайджест свежих материалов из мира фронтенда за последнюю неделю №227 (4 — 11 сентября 2016) - itfm.pro()
Pingback: Autoprefixing with CSS Variables – SourceCode()
Pingback: Front-End Development ( 11 September ) – Skokov()
Pingback: Autoprefixing, with CSS variables! | Lea Verou ...()
Pingback: 【译】巧用 CSS 变量实现自动前缀!| Lea Verou - 莹莹之色()
Pingback: Make Your CSS Dynamic with CSS Custom Properties - Cible()
Pingback: Make your CSS dynamic with CSS Custom Properties — by Nicolas Engler()
Pingback: Make Your CSS Dynamic with CSS Custom Properties - wpperosna()
Pingback: xmt85c4wx5ctwxw3tcerthve56()
Pingback: http://falschgeldkaufen.blogspot.com/2017/01/wo-kann-ich-falschgeld-kaufen.html()
Pingback: treatments for alcoholism()
Pingback: دانلود قسمت هشتم شهرزاد دوم()