Typed arithmetic and compatibility
Definition and purpose
CSS math functions operate on typed numeric values, not arbitrary strings. A calculation must resolve to a type accepted by the receiving property. A length and percentage can combine where the property defines a length-percentage context.
Mixed length-percentage
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { width: calc(50% + 10px); } </style> </head> <body> <div id="parent" style="width:240px"> <div id="a">Panel</div> <div id="b">Other</div> </div> </body></html>Half of 240px plus 10px gives 130px.
Syntax and mental model
Use spaces around binary + and -. Multiplying a length by a number yields a length. In implementations supporting typed division, dividing two lengths produces a number, even with different units. Test support with @supports (opacity: calc(1px / 2px)) before relying on this newer syntax. Adding seconds to pixels is incompatible; dimensional cancellation must leave a valid final type.
Typed quotient
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #a { opacity: 0.5; } @supports (opacity: calc(1px / 2px)) { #a { opacity: calc(20px / 80px); } } </style> </head> <body> <div id="parent" style="width:240px"> <div id="a">Panel</div> <div id="b">Other</div> </div> </body></html>The tested browser supports length division; older engines keep the fallback.
Common mistakes
Do not assume every mathematically valid expression is a valid CSS property value. A number is not a length, except for special grammar cases such as literal zero.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Formal value definition syntax. Next lesson: Trigonometric and exponential functions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Typed arithmetic and compatibility — knowledge check
Five questions on typed arithmetic and compatibility.
What type is 2 * 12px?
What is the type of 20px / 80px with typed division?
Is calc(1s + 2px) a compatible sum?
What spacing does binary subtraction require?
How can you guard newer division syntax?