Skip to content

Instantly share code, notes, and snippets.

@fernandohenriques
Last active January 13, 2018 22:34
Show Gist options
  • Select an option

  • Save fernandohenriques/719ba93863f0abbdd5a8b10f63a7cb48 to your computer and use it in GitHub Desktop.

Select an option

Save fernandohenriques/719ba93863f0abbdd5a8b10f63a7cb48 to your computer and use it in GitHub Desktop.
Read CSS rule values with JavaScript
import getStyle from "./getStyleContent"; //ES6
let styleContent = getStyle('.test');
console.log(styleContent);
/*
Returns:
.test {
color: #000000;
background-color: #fff;
}
*/
export default (styleName) => {
for (let y = 0; y < document.styleSheets.length; y++) {
let styles = document.styleSheets[y].rules || document.styleSheets[y].cssRules;
for (let i = 0; i < styles.length; i++)
if (styles[i].selectorText == styleName)
return (styles[i].cssText ? styles[i].cssText : styles[i].style.cssText);
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment