Skip to content

Instantly share code, notes, and snippets.

@aheinze
Last active July 13, 2016 07:45
Show Gist options
  • Save aheinze/263225e99ca18e2dc2f636d49e7f6357 to your computer and use it in GitHub Desktop.
Save aheinze/263225e99ca18e2dc2f636d49e7f6357 to your computer and use it in GitHub Desktop.
Get css variable (cross browser)
/**
<style>
.var-test:before {
content: 'hello';
}
</style>
<script>
var value = getCssVar('test'); // gets 'hello'
</script>
**/
window.getCssVar = function(name) {
var ele = document.createElement('div'), val;
ele.classList.add('var-'+name);
document.documentElement.appendChild(ele);
val = window.getComputedStyle(ele, ':before').content;
document.documentElement.removeChild(ele);
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment