大网数据广告活动
Web 标准与前端开发

CSSUnparsedValue

· 52CE 我爱测

The **`CSSUnparsedValue`** interface of the CSS Typed Object Model API represents a property value that can't be parsed into a more specific type — typically the value of a custom property.

CSS Typed Object Model API

The CSSUnparsedValue interface of the CSS Typed Object Model API represents a property value that can't be parsed into a more specific type — typically the value of a custom property.

The object is an iterable that may contain string fragments and variable references.

The items can be accessed and set by index (unparsedValue[0]), and as an iterable it can be used with a Statements/for...of loop or the spread syntax.

Constructor

  • CSSUnparsedValue.CSSUnparsedValue
    • : Creates a new CSSUnparsedValue object.

Instance properties

  • CSSUnparsedValue.length
    • : Returns the number of items in the object.

Instance methods

Also inherits methods from its parent interface, CSSStyleValue.

  • CSSUnparsedValue.entries()
    • : Returns a new array iterator that yields [index, value] pairs for each item in the object.
  • CSSUnparsedValue.forEach()
    • : Executes a provided function once for each item in the object.
  • CSSUnparsedValue.keys()
    • : Returns a new array iterator that yields the index of each item in the object.
  • CSSUnparsedValue.values()
    • : Returns a new array iterator that yields each item in the object.

Description

A CSSUnparsedValue instance is an iterable of items, where each item is either a string representing a fragment of CSS text that wasn't further parsed, or a CSSVariableReferenceValue object representing a var function in the value (if any were specified).

For example, given the custom property:

--foo: 10px var(--bar, blue);

The CSSUnparsedValue for --foo has two items:

  • "10px ": a plain string fragment.
  • A CSSVariableReferenceValue: representing var(--bar, blue), with its own CSSVariableReferenceValue.variable ("--bar") and CSSVariableReferenceValue.fallback (a nested CSSUnparsedValue for blue).

Parsing a CSSUnparsedValue further

CSSUnparsedValue preserves the raw, unparsed text of a value: it doesn't interpret that text as a length, a color, or any other more specific type.

If you know what kind of value the text represents, you can parse it further using the static parse() method of the type you expect, such as CSSNumericValue/parse_static for a numeric value with a unit, or the more general CSSStyleValue/parse_static, which additionally requires you to specify the target CSS property name.

These parse() methods take a CSS text string, not a CSSUnparsedValue object. However since CSSUnparsedValue inherits the CSSStyleValue.toString() stringifier, it can be passed directly wherever a string is expected. Parsing throws a SyntaxError if the text doesn't match the syntax expected by the type you're parsing it as.

Examples

Creating, reading, and updating a CSSUnparsedValue

This example creates a CSSUnparsedValue, then reads its items via length, indexed access, and iteration, and finally updates one of the items by assigning to its index.

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"

Reading and parsing a custom property's value

A CSSUnparsedValue is what you get back when you read the value of a custom property from a StylePropertyMapReadOnly, since the browser cannot know in advance what kind of value the property holds. This example reads the --unit custom property from an element's computed style map, then parses it into a CSSUnitValue using CSSNumericValue/parse_static.

<button id="btn">Styled button</button>
#btn {
  --unit: 1.2rem;
  padding: var(--unit);
}
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"

See the CSSUnparsedValue section of the CSS Typed OM guide for more detail on this example.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。