Grab value of CSS Variable in IE10
See original GitHub issueI’m working on a light/dark theme and am trying to grab the background color to decide whether to set font color black or white. The following “getPropertyValue” (to grab the background color) isn’t working on IE10 because of the CSS Variable.
getComputedStyle(document.documentElement).getPropertyValue('--primary-color')
After I get that, would the “onSuccess” callback function be the correct place to run functions and update the color?
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Workaround for CSS variables in IE? - Stack Overflow
Yes there is a way, the same way you make any css compatible: use a specific css fallback that is supported by the...
Read more >How to use CSS custom properties (a.k.a. variables)
CSS variables allow an author to assign arbitrary values to a property with an author-chosen name. This custom property must start with --...
Read more >A Strategy Guide To CSS Custom Properties
In this example, each custom property is determined using calc() , by taking the value of the previous custom property and multiplying this...
Read more >Browser comparison | Can I use... Support tables for HTML5 ...
Select browser versions to compare. Categories. All. CSS ... IE 6, IE 7, IE 8, IE 9, IE 10, IE 11. autocomplete attribute:...
Read more >Why doesn't Internet Explorer support CSS variables? - Quora
I think you are totally on the wrong track here, if you are referring to the names of the css properties as well...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@randyrandy123 –
If you are setting a CSS property using a variable (e.g.
--testing-color
), then you must use the ponyfill to transform that variable to a static value to 1) have that value applied in legacy browsers, which will allow you to 2) detect the value usinggetPropertyValue()
.For example:
When the above code is executed in IE without the ponyfill,
bgColor
will bergb(255, 0, 0)
. This is because 1) browsers return color values as their RGB equivalent, and 2) IE doesn’t understand CSS custom properties so it ignores thebackground-color: var(--testing-color);
declaration.When the ponyfill is used, the CSS custom property declaration is transformed to a static value which IE understands:
The value of
bgColor
is nowrgb(0, 128, 0)
as expected:Note that the color test is done within the
onComplete
callback. This is because the ponyfill works asynchronously when processing<link>
and@import
stylesheets. Using the callback ensures your code is executed after the ponyfill has completed.A few other things to keep in mind:
options.variables
. This works regardless of theoptions.onlyLegacy
setting. In legacy browsers, values will be applied during the transformation process outlined above. In modern browsers, values are applied instantly and synchronously using the native setProperty() method.options.onlyLegacy
istrue
). You can force callbacks to fire in modern browsers as well by settingoptions.onlyLegacy
tofalse
, but doing so will introduce the same processing delay you’re seeing in IE in modern browsers because the ponyfill is treating all browsers like a legacy browser.In order to update a CSS custom property value using the ponyfill then execute some code based on the new value in both legacy and modern browsers you would do something like this:
Regarding the ponyfill speed, keep in mind that the less CSS the ponyfill has to process the faster it will be. The ponyfill’s CSS transformation happens quickly, but the delay you’re seeing is caused by two things: 1) the amount of time it takes to retrieve external stylesheets (
<link>
and@import
) via AJAX so they can be parsed, and 2) the time it takes for the browser to apply newly transformed styles to DOM elements. This often happens when apps load large frameworks (Bootstrap, Foundation, etc.) via CDN. You can avoid processing stylesheets using eitheroptions.include
and/oroptions.exclude
, and doing so can have a significant impact on performance.Hope this helps!
Thanks for your detailed reply! I’m using the onComplete callback to dynamically change the value of the CSS and it’s working fine.
I was able to lower the 10 second delay down to <1 second by moving the CSS variables outside of the huge CSS file into a separate one. Below is what I did in case anyone is curious.
Previously I had 2 stylesheets – • 1. Compiled CSS for the whole app (this also held CSS Variables for themeing). 32k+ lines. • 2. “Theme stylesheet” – This pulled the CSS Variables declarations/styles from the stylesheet above.
I tried using those 2 stylesheets and added exclude:
[data-exclude]
on the 1st stylesheet to force the plugin to avoid looking at it, but I didn’t have any success. So instead of that, I just deleted all the CSS Variables from the 1st stylesheet and everything worked instantly.In case anyone is wondering, I originally kept the CSS Variables in the 1st stylesheet since I was I was hoping to just have one huge stylesheet for modern browsers and have the 2nd “Theme” stylesheet only for legacy browsers.
Thanks once again!