A mathematical expression combines values and symbols to perform a calculation and produce a result. In frontend work, the values may be numbers, variables, object properties, or function results. The input values are operands; the symbols that act on them are operators. section 1.4.1 section 2.1
The four standard arithmetic operators support common layout, typography, and pricing calculations:
- Addition (+) combines values, such as 16px + 24px = 40px. section 1.4.1 section 3.2.1
- Subtraction (-) removes one value from another, such as a discounted price or 50rem - 5rem. section 1.4.1 section 3.2.1
- Multiplication (*) scales a value, such as font-size: calc(16px * 2.5). section 1.4.1 section 3.2.1
- Division (/) distributes or derives a value, such as 100% / 3 or a JavaScript price calculation. section 1.4.1 section 3.2.1
Algebraic expressions use unknown or changing quantities. A responsive width can subtract a fixed gap from the parent width, while JavaScript can subtract a header height from window.innerHeight. The same pattern supports scroll-based opacity and motion calculated from speed and time. section 1.4.2
A responsive CSS calculation
Use width: calc(100% - 2rem); rather than placing the arithmetic directly in a declaration. CSS arithmetic expressions belong inside a math function such as calc(). Addition and subtraction require spaces around the operator, compatible units may be mixed, and parentheses can control precedence. section 3.2.1 section 3.3.2
Table 1.1 Some frontend arithmetic examples
| Task |
Code |
Operation |
| Set an element’s width in CSS. |
|
Addition ( |
| Calculate the discounted price in a JavaScript statement. |
|
Subtraction ( |
| Determine a fixed font size for an element in CSS. |
|
Multiplication ( |
| Calculate the pre-tax price in JavaScript. |
|
Division ( |
Compare arithmetic expressions used for CSS dimensions, font sizes, and JavaScript prices. section 1.4.1
CSS also evaluates expressions involving variables and Grid proportions. Multiplication or division requires a scalar operand, and two unit-bearing values cannot be multiplied or divided. In Grid, 1fr 2fr 1fr becomes x + 2x + x = w, allowing the browser to recalculate proportional tracks as available width changes. section 3.3.2 section 4.1.3
