Web design
Prepare a CSS content string
Escape characters when a literal belongs in a CSS string.
1. Use semantic HTML first
Do not rely on generated CSS content for essential instructions. Put the meaningful label in semantic HTML, even if a decorative version appears beside it.
2. Inspect the input
Quotes, backslashes and newlines can alter the intended string. Start from the exact desired text.
3. Generate a value
Use the CSS string utility and preserve the enclosing quotes. Do not concatenate raw untrusted CSS.
4. Test output
Inspect the rendered text and assistive-technology behavior. String escaping does not sanitize arbitrary CSS.
Preview a decorative quoted label
Enter a label containing a quotation mark, a backslash and an actual line break in CSS content string. Inspect the generated quoted CSS value and use it only as the value of content in a controlled test rule. Verify the displayed characters and compare with the underlying semantic HTML label.
Common pitfall
Do not paste an arbitrary stylesheet into a string utility or rely on generated CSS text for essential instructions. Escaping a string value does not sanitize selectors, URLs or full stylesheets.