📖 Scenario: You are designing a simple webpage layout. You want to use SASS to calculate widths and margins using arithmetic operations to keep your CSS clean and easy to update.
🎯 Goal: Create a SASS stylesheet that uses arithmetic operations to calculate the width and margin of a container and a box inside it.
📋 What You'll Learn
Create variables for container width and margin
Use arithmetic operations to calculate box width
Apply the calculated widths and margins in CSS selectors
Use SASS variables and arithmetic correctly
💡 Why This Matters
🌍 Real World
Using SASS variables and arithmetic helps keep CSS consistent and easy to update when designing responsive layouts.
💼 Career
Front-end developers often use SASS to write maintainable and scalable stylesheets for websites and applications.
Progress0 / 4 steps
1
Create SASS variables for container width and margin
Create two SASS variables: $container-width set to 1200px and $container-margin set to 20px.
SASS
Hint
Use $variable-name: value; syntax to create variables in SASS.
2
Calculate box width using arithmetic
Create a new variable called $box-width that calculates the width as $container-width minus twice the $container-margin using SASS arithmetic.
SASS
Hint
Use - and * operators to subtract and multiply in SASS.
3
Apply variables in CSS selectors
Write CSS rules for a .container class with width set to $container-width and margin set to $container-margin. Then write CSS rules for a .box class with width set to $box-width.
SASS
Hint
Use the variables inside CSS properties like width: $variable;.
4
Add a background color and border to .box
Add a background-color of #f0f0f0 and a border of 1px solid #ccc to the .box class.
SASS
Hint
Use standard CSS properties inside the SASS selectors.
Practice
(1/5)
1. What does the Sass expression 10px + 5px produce?
easy
A. Syntax error
B. 105px
C. 15px
D. 5px
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 C
Quick Check:
10px + 5px = 15px [OK]
Hint: Add numbers only if units match exactly [OK]
Common Mistakes:
Concatenating numbers as strings (getting 105px)
Ignoring units and returning just number
Thinking addition causes syntax error
2. Which of the following is the correct Sass syntax to multiply 8px by 3?
easy
A. 8px * 3
B. 8px x 3
C. multiply(8px, 3)
D. 8px ** 3
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 A
Quick Check:
Multiplication uses * operator [OK]
Hint: Use * for multiplication, not x or other symbols [OK]
Common Mistakes:
Using 'x' instead of '*' for multiplication
Trying function calls like multiply() which Sass doesn't have