Skip to content

Instantly share code, notes, and snippets.

@sergeysova
Last active September 6, 2020 18:53
Show Gist options
  • Select an option

  • Save sergeysova/f1f4d34c59514201494f99c98291185e to your computer and use it in GitHub Desktop.

Select an option

Save sergeysova/f1f4d34c59514201494f99c98291185e to your computer and use it in GitHub Desktop.
Read CSS variable from JS
getVariable(domElement, '--example')
// 'hello'

getVariable(domElement, '--example', (v) => parseInt(v, 36))
// 29234652
function getVariable(element, variable, reader = String) {
const style = window.getComputedStyle(element)
return reader(style.getPropertyValue(variable).trim());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment