Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE11 - does a polyfill / script exist for CSS variables?

I'm developing a webpage in a mixed web browser environment (Chrome/IE11). IE11 doesn't support CSS variables, is there a polyfill or script that exists that would allow me to use CSS variables in IE11?

like image 686
R. StackUser Avatar asked Sep 26 '17 15:09

R. StackUser


People also ask

Does IE 11 support CSS variables?

Note: CSS variables are not and won't be supported in IE11. You can either create a static stylesheet for all UA browsers or decide to leverage them in most UA browsers + use a JS fallback for IE11 if you want to support this browser – you can test for CSS variables support in JS.

Does Microsoft EDGE support CSS variables?

CSS Variables (Custom Properties) element is not supported by Microsoft Edge browser 12 to 14.

What is polyfill CSS?

A polyfill is a piece of code (usually JavaScript on the Web) used to provide modern functionality on older browsers that do not natively support it.

Do all browsers support CSS variables?

CSS variables are currently supported for 93 percent of users globally. If a browser doesn't support CSS variables, it also doesn't understand the var() function, and doesn't know what its second argument means. Instead, we need to use the cascade, as we do for every new CSS feature.


1 Answers

Yes, so long as you're processing root-level custom properties (IE9+).

  • GitHub: https://github.com/jhildenbiddle/css-vars-ponyfill
  • NPM: https://www.npmjs.com/package/css-vars-ponyfill
  • Demo: https://codepen.io/jhildenbiddle/pen/ZxYJrR

From the README:

Features

  • Client-side transformation of CSS custom properties to static values
  • Live updates of runtime values in both modern and legacy browsers
  • Transforms <link>, <style>, and @import CSS
  • Transforms relative url() paths to absolute URLs
  • Supports chained and nested var() functions
  • Supports var() function fallback values
  • Supports web components / shadow DOM CSS
  • Watch mode auto-updates on <link> and <style> changes
  • UMD and ES6 module available
  • TypeScript definitions included
  • Lightweight (6k min+gzip) and dependency-free

Limitations

  • Custom property support is limited to :root and :host declarations
  • The use of var() is limited to property values (per W3C specification)

Here are a few examples of what the library can handle:

Root-level custom properties

:root {     --a: red; }  p {     color: var(--a); } 

Chained custom properties

:root {     --a: var(--b);     --b: var(--c);     --c: red; }  p {     color: var(--a); } 

Nested custom properties

:root {     --a: 1em;     --b: 2; }  p {     font-size: calc(var(--a) * var(--b)); } 

Fallback values

p {     font-size: var(--a, 1rem);     color: var(--b, var(--c, var(--d, red)));  } 

Transforms <link>, <style>, and @import CSS

<link rel="stylesheet" href="/absolute/path/to/style.css"> <link rel="stylesheet" href="../relative/path/to/style.css">  <style>     @import "/absolute/path/to/style.css";     @import "../relative/path/to/style.css"; </style> 

Transforms web components / shadow DOM

<custom-element>   #shadow-root     <style>       .my-custom-element {         color: var(--test-color);       }     </style>     <div class="my-custom-element">Hello.</div> </custom-element> 

For the sake of completeness: w3c specs

Hope this helps.

(Shameless self-promotion: Check)

like image 98
jhildenbiddle Avatar answered Oct 28 '22 05:10

jhildenbiddle