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.