Arithmetic operations let you do math with numbers in your styles. This helps you create flexible and dynamic designs.
Arithmetic operations in SASS
$result: <number> <operator> <number>;Operators include + (add), - (subtract), * (multiply), / (divide), and % (modulus).
Use variables or numbers directly. Always include spaces around operators for clarity.
$width: 100px + 20px;
$half: 50% / 2;
$double: 10rem * 2;
$difference: 80px - 30px;
This example shows how to use arithmetic to set padding as half the base size, margin as base size plus 8 pixels, and width as the smaller of 100% minus 40 pixels or 600 pixels.
@use 'sass:math'; $base-size: 16px; $padding: $base-size / 2; $margin: $base-size + 8px; .container { font-size: $base-size; padding: $padding; margin: $margin; width: math.min(100% - 40px, 600px); }
Always keep units consistent when doing arithmetic (e.g., px with px, %, or rem).
Division with / can sometimes be confused with CSS slash notation; use parentheses to clarify.
You can use the math module for advanced math functions like min(), max(), and pow().
Arithmetic operations let you do math in Sass to create flexible styles.
Use +, -, *, /, and % with numbers and variables.
Keep units consistent and use parentheses to avoid confusion.
Practice
10px + 5px produce?Solution
Step 1: Understand addition of units in Sass
Sass adds numbers with the same units by adding their numeric values and keeping the unit.Step 2: Calculate the sum of 10px and 5px
10 + 5 equals 15, so the result is 15px.Final Answer:
15px -> Option CQuick Check:
10px + 5px = 15px [OK]
- Concatenating numbers as strings (getting 105px)
- Ignoring units and returning just number
- Thinking addition causes syntax error
Solution
Step 1: Identify the multiplication operator in Sass
Sass uses the asterisk (*) symbol for multiplication.Step 2: Check each option for correct syntax
8px * 3 uses*correctly; others use invalid syntax.Final Answer:
8px * 3 -> Option AQuick Check:
Multiplication uses * operator [OK]
- Using 'x' instead of '*' for multiplication
- Trying function calls like multiply() which Sass doesn't have
- Using ** which is not Sass syntax
$width: 20px; $padding: 5px; $total: $width + $padding * 2;
What is the value of
$total?Solution
Step 1: Apply operator precedence in Sass arithmetic
Multiplication (*) has higher precedence than addition (+), so calculate $padding * 2 first: 5px * 2 = 10px.Step 2: Add $width and the multiplication result
20px + 10px = 30px.Final Answer:
30px -> Option DQuick Check:
20px + (5px * 2) = 30px [OK]
- Adding before multiplying (getting 50px)
- Confusing units causing syntax errors
- Ignoring operator precedence
$height: 50px; $width: 20; $total: $height + $width;
Solution
Step 1: Check units of variables
$height has unit px, but $width is a number without unit.Step 2: Understand Sass unit rules for addition
Sass cannot add numbers with different or missing units, so this causes an error.Final Answer:
Error: Cannot add values with different units -> Option AQuick Check:
Adding px and unitless number causes error [OK]
- Assuming unitless numbers auto-convert
- Thinking missing semicolon causes error here
- Believing variable names must be uppercase
$container-width: 1000px; $padding: 20px; $usable-width: $container-width - 2 * $padding;
What is the correct value of
$usable-width?Solution
Step 1: Calculate twice the padding
2 * $padding = 2 * 20px = 40px.Step 2: Subtract twice the padding from container width
1000px - 40px = 960px.Final Answer:
960px -> Option BQuick Check:
1000px - (2 * 20px) = 960px [OK]
- Forgetting units in result (writing 960 without px)
- Adding instead of subtracting
- Syntax errors from missing spaces or operators
Ready to prove it?
Pass the test and this pattern moves unknown
