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

`sign()` CSS function

· 52CE 我爱测

The **`sign()`** CSS function contains one calculation, and returns `-1` if the numeric value of the argument is negative, `+1` if the numeric value of the argument is positive, `0⁺` if the numeric value of the argument is 0⁺, and `0⁻` if the numeric value of the argument is 0⁻.

The sign() CSS function contains one calculation, and returns -1 if the numeric value of the argument is negative, +1 if the numeric value of the argument is positive, 0⁺ if the numeric value of the argument is 0⁺, and 0⁻ if the numeric value of the argument is 0⁻.

[!NOTE] While abs returns the absolute value of the argument, sign() returns the sign of the argument.

Syntax

/* property: sign( expression ) */
top: sign(20vh - 100px);

Parameters

The sign(x) function accepts only one value as its parameter.

  • x
    • : A calculation which resolves to a number.

Return value

A number representing the sign of A:

  • If x is positive, returns 1.
  • If x is negative, returns -1.
  • If x is positive zero, returns 0.
  • If x is negative zero, returns -0.
  • Otherwise, returns NaN.

Formal syntax

Examples

Background image position

For example, in background-position positive percentages resolve to a negative length, and vice versa, if the background image is larger than the background area. Thus sign(10%) might return 1 or -1, depending on how the percentage is resolved! (Or even 0, if it's resolved against a zero length.)

div {
  background-position: calc(sign(10%) * 1px);
}

Position direction

Another use case is to control the position of the element. Either a positive or a negative value.

div {
  position: absolute;
  top: calc(100px * sign(var(--value)));
}

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

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

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