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

CSSNumericValue: toSum() method

· 52CE 我爱测

The **`toSum()`** method of the CSSNumericValue interface converts the `CSSNumericValue` into a CSSMathSum whose terms are CSSUnitValue objects in the specified units.

CSS Typed Object Model API

The toSum() method of the CSSNumericValue interface converts the CSSNumericValue into a CSSMathSum whose terms are CSSUnitValue objects in the specified units.

Syntax

toSum()
toSum(unit1)
toSum(unit1, unit2)
toSum(unit1, unit2, /* …, */ unitN)

Parameters

  • unit1, …, unitN
    • : Strings naming the units to use for the terms of the result, such as "px" or "percent".

Return value

A CSSMathSum whose terms are all CSSUnitValue objects.

Exceptions

  • SyntaxError DOMException
    • : Thrown if any of unit1, …, unitN isn't a valid CSS unit.
  • TypeError
    • : Thrown if:
      • The value can't be written as a sum of single values. For example, a term that multiplies two units, such as CSS.px(1).mul(CSS.s(1)), can't be written as a CSSUnitValue.
      • Units are passed, and the value has a term that can't be converted to any of them, such as an em term when only "px" is passed.
      • The units passed can't be added together, such as "px" and "s".

Description

The method first combines the terms of the CSSNumericValue that can be converted to the same unit, as CSSNumericValue.to() does. For example, 1px and 1in are combined into 97px.

If units are passed, the result has one term for each unit, in the order the units were passed. Each term of the value is converted to the unit it's compatible with. If no term can be converted to one of the units, the result has a term with the value 0 in that unit. For example, CSS.px(1).toSum("px", "percent") returns calc(1px + 0%).

If no units are passed, the result has one term for each unit left after combining. The terms are sorted alphabetically by unit name, where the name for % is "percent". For example, calc(10px + 2em + 5px) gives calc(2em + 15px).

Examples

Converting a sum to specific units

const value = CSS.px(100).add(CSS.percent(10)).add(CSS.in(1));
console.log(value.toString()); // "calc(100px + 10% + 1in)"
console.log(value.toSum("px", "percent").toString()); // "calc(196px + 10%)"

console.log(CSS.px(1).toSum("px", "percent").toString()); // "calc(1px + 0%)"

Simplifying a sum

When no units are passed, terms with the same unit are combined, and the terms are sorted by unit name:

const value = CSS.px(10).add(CSS.em(2), CSS.px(5));
console.log(value.toString()); // "calc(10px + 2em + 5px)"
console.log(value.toSum().toString()); // "calc(2em + 15px)"

Values that can't be converted

A sum with an em term can't be converted to px only:

try {
  CSS.px(1).add(CSS.em(1)).toSum("px");
} catch (e) {
  console.log(e instanceof TypeError); // true
}

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

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

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