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

CSSStyleValue: toString() method

· 52CE 我爱测

The **`toString()`** method of the CSSStyleValue interface is a stringifier that returns the value formatted as a string of standard CSS text.

CSS Typed Object Model API

The toString() method of the CSSStyleValue interface is a stringifier that returns the value formatted as a string of standard CSS text.

Syntax

toString()

Parameters

None.

Return value

A string.

Description

The exact serialization of the object to a string depends on how the CSSStyleValue object was obtained:

  • If the object was created by parsing a CSS string, such as with CSSStyleValue.parse_static, the method returns the original string that was parsed.
  • If the object was constructed directly, such as with a CSS factory function or a subclass constructor, the returned string is generated according to serialization rules specific to that subclass.
  • If the object was read from the CSSOM, such as with Element.computedStyleMap() or HTMLElement.attributeStyleMap, the returned string follows the CSSOM serialization rules.

For more information about serialization rules see When and how values are serialized in CSS value serialization.

Examples

Basic usage

// Parsed from a string: returns the original string
const length1 = CSSStyleValue.parse("42.0px");
length1.toString(); // "42.0px"

// Constructed directly with a CSS factory function: subclass-specific serialization
const length2 = CSS.px(42.0);
length2.toString(); // "42px"

// Read from the CSSOM: follows CSSOM serialization rules
const element = document.createElement("div");
element.style.width = "42.0px";
const length3 = element.attributeStyleMap.get("width");
length3.toString(); // "42px"

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

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

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