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

CSSNumericValue: min() method

· 52CE 我爱测

The **`min()`** method of the CSSNumericValue interface returns the smallest of the `CSSNumericValue` and the supplied values.

CSS Typed Object Model API

The min() method of the CSSNumericValue interface returns the smallest of the CSSNumericValue and the supplied values.

Syntax

min()
min(number1)
min(number1, number2)
min(number1, number2, /* …, */ numberN)

Parameters

  • number1, …, numberN
    • : Either a number or a CSSNumericValue. A number is converted to a CSSUnitValue whose unit is "number", so 2 is treated as CSS.number(2).

Return value

A CSSUnitValue if the CSSNumericValue and every supplied value have the same unit. Otherwise, a CSSMathMin.

Exceptions

  • TypeError
    • : Thrown if the values have incompatible types, such as a length and a time.

Description

If the CSSNumericValue and all the supplied values have exactly the same unit, the method compares them and returns the smallest as a single CSSUnitValue. For example, CSS.px(10).min(CSS.px(25)) returns 10px.

Otherwise, the method returns a CSSMathMin, which represents a CSS min() function, and keeps each value as a separate term. The browser works out which term is smallest when the value is used. For example, which of 100px and 50% is smaller depends on the size of the element's container. Units aren't converted, so comparing a cm length with an in length also gives a CSSMathMin, even though both are fixed lengths.

If the CSSNumericValue is already a CSSMathMin, the supplied values are added as extra terms of it, rather than the existing CSSMathMin becoming one term inside a new one. The terms of the result are in order: first the CSSNumericValue (or, if it's already a CSSMathMin, its terms), then the supplied values in the order they were passed.

The values must have types that can be compared, such as two lengths, or a length and a percentage. Comparing a length with a time, or with a plain number, throws a TypeError.

Examples

Finding the smallest of values with the same unit

When every value has the same unit, the result is a single CSSUnitValue:

const smallest = CSS.px(10).min(CSS.px(25), CSS.px(5));
console.log(smallest.toString()); // "5px"

Comparing values with different units

When the units are different, the result is a CSSMathMin, even if the values could be converted to the same unit:

const atMost = CSS.px(100).min(CSS.percent(50));
console.log(atMost.toString()); // "min(100px, 50%)"

const lengths = CSS.cm(1).min(CSS.in(0.393701));
console.log(lengths.toString()); // "min(1cm, 0.393701in)"

Comparing values that can't be compared

Comparing a length with a time throws a TypeError:

try {
  CSS.px(1).min(CSS.s(1));
} 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

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

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