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?

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);
Sign up to request clarification or add additional context in comments.

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

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.