Skip to content

Commit

Permalink
docs(CSS): Clarify use of calc() function with percentages (mdn#22896)
Browse files Browse the repository at this point in the history
Fixes mdn#22805
  • Loading branch information
bsmth committed Dec 13, 2022
1 parent b051dcb commit 110f679
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions files/en-us/web/css/calc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ browser-compat: css.types.calc

{{CSSRef}}

The **`calc()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) lets you perform calculations when specifying CSS property values. It can be used anywhere a {{cssxref("<length>")}}, {{cssxref("<frequency>")}}, {{cssxref("<angle>")}}, {{cssxref("<time>")}}, {{cssxref("<percentage>")}}, {{cssxref("<number>")}}, or {{cssxref("<integer>")}} is allowed.
The **`calc()`** [CSS](/en-US/docs/Web/CSS) [function](/en-US/docs/Web/CSS/CSS_Functions) lets you perform calculations when specifying CSS property values. It can be used with {{cssxref("<length>")}}, {{cssxref("<frequency>")}}, {{cssxref("<angle>")}}, {{cssxref("<time>")}}, {{cssxref("<percentage>")}}, {{cssxref("<number>")}}, or {{cssxref("<integer>")}} values.

{{EmbedInteractiveExample("pages/css/function-calc.html")}}

Expand All @@ -43,12 +43,13 @@ The operands in the expression may be any {{cssxref("<length>")}} syntax v

### Notes

- The `+` and `-` operators **must be surrounded by {{Glossary("whitespace")}}**. For instance, `calc(50% -8px)` will be parsed as a percentage followed by a negative length — 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.
- The `*` and `/` operators do not require whitespace, but adding it for consistency is both allowed and recommended.
- The `+` and `-` operators **must be surrounded by {{Glossary("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".
- The `*` and `/` operators do not require whitespace, but adding it for consistency is recommended.
- Division by zero results in an error being generated by the HTML parser.
- Math expressions involving percentages for widths and heights on table columns, table column groups, table rows, table row groups, and table cells in both auto and fixed layout tables _may_ be treated as if `auto` had been specified.
- It is permitted to nest `calc()` functions, in which case the inner ones are treated as simple parentheses.
- For lengths, you can't use `0` to mean `0px` (or another length unit); instead, you must use the version with the unit: `margin-top: calc(0px + 20px);` is valid, while `margin-top: calc(0 + 20px);` is invalid.
- The `calc()` function cannot directly substitute the numeric value for percentage types; for instance `calc(100 / 4)%` is invalid, while `calc(100% / 4)` is valid.

### Formal syntax

Expand Down

0 comments on commit 110f679

Please sign in to comment.