CSSNumericValue: toSum() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Want more browser support for this feature? Tell us why.

Note: This feature is available in Web Workers.

The toSum() method of the CSSNumericValue interface converts the object's value to a CSSMathSum of CSSUnitValues using only the specified units, if possible. If called with no units, it simplifies the value into a minimal sum of CSSUnitValues instead.

Syntax

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

Parameters

unit1, …, unitN Optional

The units to convert to.

Return value

A CSSMathSum.

Exceptions

SyntaxError DOMException

Thrown if any of unit1, …, unitN is not a valid unit identifier.

TypeError

Thrown if:

  • The value can't be expressed as a sum of CSSUnitValues — for example, because one of its terms has a compound unit (such as px * s) that can't be represented by a single CSSUnitValue.
  • One or more units were passed to the method, and the value includes a term whose unit isn't compatible with any of them.

Examples

Basic usage

js
let v = CSS.px("23").add(CSS.percent("4")).add(CSS.cm("3")).add(CSS.in("9"));
v.toString(); // => "calc(23px + 4% + 3cm + 9in)"
v.toSum("px", "percent").toString(); // => "calc(1000.39px + 4%)"

Specifications

Specification
CSS Typed OM Level 1
# dom-cssnumericvalue-tosum

Browser compatibility