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

`<calc-sum>` CSS type

· 52CE 我爱测

The **`<calc-sum>`** CSS data type represents an expression which performs a calculation in any CSS math function. The expression executes a basic arithmetic operation of addition and subtraction between two values.

The <calc-sum> CSS data type represents an expression which performs a calculation in any CSS math function. The expression executes a basic arithmetic operation of addition and subtraction between two values.

Syntax

The <calc-sum> type defines two numeric values and one of the following arithmetic operators between them.

  • +

    • : Adds two numbers together.
  • -

    • : Subtracts the right number from the left.

Description

The operands in the expression may be any <length> syntax value. You can use <length>, <frequency>, angle, <time>, <percentage>, <number>, or <integer>.

The two operands' types must be consistent. For lengths, you can't use 0 to mean 0px (or another length unit). Instead, you must add an explicit unit: margin-top: calc(0px + 20px); is valid, while margin-top: calc(0 + 20px); is invalid. Percentage value types are resolved based on the context. For example, margin-top: calc(50% + 20px); is valid because margin-top resolves percentages to lengths.

Including CSS variables in calc-sum expressions is also allowed. The following code calc(10px + var(--variable)), is a valid expression.

The + and - operators must be surrounded by whitespace. For instance, calc(50% -8px) will be parsed as "a percentage followed by a negative length" — which is an invalid expression — while calc(50% - 8px) is "a percentage followed by a subtraction operator and a length". Likewise, calc(8px + -50%) is treated as "a length followed by an addition operator and a negative percentage".

Formal syntax

Specifications

See also

  • <calc-product>
  • <calc-value>
  • <calc-keyword>

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

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

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