Skip to content

Instantly share code, notes, and snippets.

@influxweb
Forked from wesbos/css-variables.js
Created August 8, 2017 21:39
Show Gist options
  • Save influxweb/aca76f667363574243d4a15df0c486b2 to your computer and use it in GitHub Desktop.
Save influxweb/aca76f667363574243d4a15df0c486b2 to your computer and use it in GitHub Desktop.
Test for CSS Variables
function testCSSVariables() {
var color = 'rgb(255, 198, 0)';
var el = document.createElement('span');
el.style.setProperty('--color', color);
el.style.setProperty('background', 'var(--color)');
document.body.appendChild(el);
var styles = getComputedStyle(el);
var doesSupport = styles.backgroundColor === color;
document.body.removeChild(el);
return doesSupport;
}
testCSSVariables();
@steveosoule
Copy link

There's also the JavaScript API CSS.supports().

var supportsVariables = CSS.supports('--css-variables', 1);
var supportsFlexbox= CSS.supports('display', 'flex');

Unfortunately, is doesn't have IE support, but you can polyfill it for now.

Cool article on CSS @supports & JavaScript CSS.supports()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment