MDN self-hosted library · e01.org
CSSUnparsedValue / notebook

CSSUnparsedValue

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

css
--foo: 10px var(--bar, blue);
js
const value = new CSSUnparsedValue(["1em", "#445566", "-45px"]);

console.log(value.length); // 3
console.log(value[0]); // "1em"

for (const fragment of value) {
  console.log(fragment);
}
// "1em"
// "#445566"
// "-45px"

value[0] = "2em";
console.log(value[0]); // "2em"
html
<button id="btn">Styled button</button>
css
#btn {
  --unit: 1.2rem;
  padding: var(--unit);
}
js
const styleMap = document.getElementById("btn").computedStyleMap();
const unit = styleMap.get("--unit");

console.log(unit); // CSSUnparsedValue {0: " 1.2rem", length: 1}
console.log(unit[0]); // " 1.2rem"

const parsedUnit = CSSNumericValue.parse(unit);
console.log(parsedUnit.value); // 1.2
console.log(parsedUnit.unit); // "rem"