Last active
July 13, 2016 07:45
-
-
Save aheinze/263225e99ca18e2dc2f636d49e7f6357 to your computer and use it in GitHub Desktop.
Get css variable (cross browser)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
<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