concept
Css injection — CSS reference
Untrusted input must not be concatenated into stylesheets or style attributes. CSS can alter important interface presentation and request resources; treat it as code, not plain text. Map user choices to a short allowlist of known values and apply a narrowly scoped property. The example maps an unknown option to a safe default; it does not demonstrate exfiltration or claim general CSS sanitization.
Formal Syntax & Structure
css syntaxcss
element.style.color = approvedColor;Working Example
Example Snippetcss
<!doctype html><html lang="en"><meta charset="utf-8"><title>css-injection</title><style>body{margin:8px;font:16px/1.5 sans-serif;color:black;background:white}#a{box-sizing:border-box}
</style><body><p id="a" data-choice="unknown">Text</p><script>const approved={blue:"#1d4ed8",black:"#000"};const choice=a.dataset.choice;a.style.color=Object.hasOwn(approved,choice)?approved[choice]:approved.black;</script></body></html>