1

Getting a CSS string from a CSSStyleDeclaration is made with el.style.cssText, but how can I convert a string, e.g. width:20px; height:20px; background:lime; to a CSSStyleDeclaration object?

My final goal is to merge two CSS declarations, one coming from el.style the other as a string. I thought it was easier to convert both as CSSStyleDeclaration objects to merge it. Let me know if you have a better solution.

1 Answer 1

3
const styles = "width:20px; height:20px; background:lime;"

const el = document.createElement('div');

el.style.cssText = styles;

const cssStyleDeclaration = el.style;
Sign up to request clarification or add additional context in comments.

Comments

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.