1

I've been playing with document.styleSheets API and it mostly does what I want, but once I add a bunch of rules I want to be able to parse the shylesheet object into a valid css (so I can put it into a downloadable file), is possible with the API or just by manually looping trough it?

asked Jul 21, 2014 at 12:18
1

1 Answer 1

2

Iterate over it:

var rules = [], i, j, css;
for (i = 0; i < document.styleSheets.length; i++) {
 for (j = 0; j < document.styleSheets[i].rules.length; j++) {
 rules.push(document.styleSheets[i].rules[j].cssText);
 }
}
css = rules.join('\n');
console.log(css);
answered Jul 21, 2014 at 12:37

1 Comment

Is there an efficient way to output all styles under same selector, for example if I have rule1: header { color:red }, rule2: header { padding: 0 }, in the parsed file it would output: header {color:red; padding: 0}

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.