Numbers in Sass help you control sizes, spaces, and colors. Units tell the browser what those numbers mean, like pixels or percentages.
Number types and units in SASS
$variable: 10px; $percentage: 50%; $number: 1.5; $em-size: 2em;
Numbers can have units like px, %, em, rem, vh, vw, etc.
Unitless numbers are just plain numbers without any unit.
$width: 100px;
$opacity: 0.5;
$font-size: 1.2rem;
$margin: 10%;
This Sass code sets base font size and scales the heading size using numbers and units. It uses rem for spacing and pixels for font size calculation.
@use 'sass:math'; $base-font-size: 16px; $scale-factor: 1.25; body { font-size: $base-font-size; margin: 2rem; } h1 { font-size: math.round($base-font-size * $scale-factor * 10) / 10; margin-bottom: 1.5rem; } p { font-size: 1rem; line-height: 1.5; margin-bottom: 1rem; }
Always use units when needed to avoid errors in calculations.
You can do math with numbers and units in Sass, but units must be compatible.
Use unitless numbers for things like opacity or z-index where units don't apply.
Numbers in Sass can have units like px, %, em, rem, or be unitless.
Units tell the browser how to interpret the number.
Use numbers and units to control sizes, spacing, and other styles precisely.
Practice
Solution
Step 1: Understand Sass number units
Sass supports standard CSS units likepx,%,em,rem, etc.Step 2: Check each option for valid unit
12pxuses a valid unitpx. Others use invalid or misspelled units.Final Answer:
12px -> Option BQuick Check:
Valid CSS unit = 12px [OK]
- Confusing 'percent' with '%' unit
- Using non-standard unit names like 'pixels'
- Misspelling units like 'ptx'
Solution
Step 1: Check spacing between number and unit
Sass numbers must have no space between the number and unit.Step 2: Validate each option
5emis correct with no space. Others have spaces or invalid units.Final Answer:
5em -> Option AQuick Check:
No space between number and unit = 5em [OK]
- Adding space between number and unit
- Using plural forms like 'rems'
- Confusing unit syntax with CSS property values
$width: 10px * 2;
.result {
width: $width;
}Solution
Step 1: Multiply number with unit by a unitless number
Multiplying10pxby2doubles the value but keeps the unit.Step 2: Resulting value and unit
The result is20px, which is valid CSS for width.Final Answer:
width: 20px; -> Option CQuick Check:
10px * 2 = 20px [OK]
- Concatenating number and unit as string
- Losing unit after multiplication
- Expecting unit to change or disappear
$height: 50px + 10;
.box {
height: $height;
}Solution
Step 1: Check operation between numbers with and without units
Sass cannot add a unitless number to a number with units directly.Step 2: Identify the error cause
Adding50pxand10(unitless) causes a unit mismatch error.Final Answer:
Adding a unitless number to a number with unit causes error -> Option AQuick Check:
Unit mismatch in addition = error [OK]
- Assuming variable names can't start with $
- Forgetting semicolons (Sass allows optional semicolons)
- Thinking units are case sensitive
Solution
Step 1: Define variables with units included
Variables must include units like2remand5%directly.Step 2: Use variables correctly in media queries
Apply$small-paddingnormally and override with$large-paddinginside media query.Step 3: Avoid invalid syntax
The other options misuse units or try invalid operations like adding different units.Final Answer:
Defines variables with units and uses media queries -> Option DQuick Check:
Variables with units + media query = responsive padding [OK]
- Trying to add numbers with different units
- Writing units outside variables incorrectly
- Using calc() with Sass variables without interpolation
Ready to prove it?
Pass the test and this pattern moves unknown
