Autoprefixing, with CSS variables!

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 pr…


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou

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? ;)


This content originally appeared on Lea Verou’s blog and was authored by Lea Verou


Print Share Comment Cite Upload Translate Updates
APA

Lea Verou | Sciencx (2016-09-07T00:00:00+00:00) Autoprefixing, with CSS variables!. Retrieved from https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/

MLA
" » Autoprefixing, with CSS variables!." Lea Verou | Sciencx - Wednesday September 7, 2016, https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/
HARVARD
Lea Verou | Sciencx Wednesday September 7, 2016 » Autoprefixing, with CSS variables!., viewed ,<https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/>
VANCOUVER
Lea Verou | Sciencx - » Autoprefixing, with CSS variables!. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/
CHICAGO
" » Autoprefixing, with CSS variables!." Lea Verou | Sciencx - Accessed . https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/
IEEE
" » Autoprefixing, with CSS variables!." Lea Verou | Sciencx [Online]. Available: https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/. [Accessed: ]
rf:citation
» Autoprefixing, with CSS variables! | Lea Verou | Sciencx | https://www.scien.cx/2016/09/07/autoprefixing-with-css-variables/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.