Discover how simple units can save your layout from chaos and make your designs shine everywhere!
Why Number types and units in SASS? - Purpose & Use Cases
Imagine you are designing a webpage and you want to set the width of a box. You write the width as just a number, like 100, without any unit.
Or you try to mix different units like pixels and percentages manually for different elements.
Without specifying units, browsers don't know if 100 means pixels, ems, or something else, causing your layout to break.
Mixing units manually is confusing and can lead to inconsistent sizes, making your page look messy on different screens.
Number types and units in Sass let you write numbers with units clearly, like 100px or 50%, so the browser understands exactly what you mean.
Sass also helps you do math with these units safely, so you can combine and convert sizes without errors.
width: 100; // Missing unit, causes issues height: 50%; margin: 10px + 5; // Mixing units manually
width: 100px; height: 50%; margin: 10px + 5px; // Units match, math works
You can create flexible, consistent layouts that adapt well to different screen sizes and devices.
When building a responsive navigation bar, using units like rem and % with Sass math ensures the bar looks good on phones, tablets, and desktops without extra work.
Number types with units tell browsers exactly how to size elements.
Sass helps you do math with units safely and clearly.
This makes your designs flexible and consistent across devices.
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
