Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Reducing compiled CSS size with Sass
📖 Scenario: You are working on a website with multiple buttons that share similar styles. To keep your CSS file small and easy to maintain, you want to use Sass features to reduce repeated code.
🎯 Goal: Build a Sass stylesheet that uses variables and nesting to reduce the compiled CSS size while styling two types of buttons: .btn-primary and .btn-secondary.
📋 What You'll Learn
Create a Sass variable for the main button color
Use nesting to style .btn-primary and .btn-secondary inside a common .btn class
Use the variable for the background color of .btn-primary
Add a different background color for .btn-secondary without repeating common styles
💡 Why This Matters
🌍 Real World
Web developers often need to write CSS that is easy to maintain and small in size to improve website speed and readability.
💼 Career
Knowing how to use Sass variables, nesting, and functions is a valuable skill for front-end developers to write efficient and scalable stylesheets.
Progress0 / 4 steps
1
Create a color variable
Create a Sass variable called $main-color and set it to #3498db.
SASS
Hint
Use $main-color: #3498db; to create the variable.
2
Add base button styles with nesting
Create a .btn class and nest styles for .btn-primary and .btn-secondary inside it. Add padding: 1rem 2rem; and border-radius: 0.5rem; to .btn.
SASS
Hint
Use .btn { padding: 1rem 2rem; border-radius: 0.5rem; } and nest &-primary and &-secondary inside.
3
Add background colors using the variable and a new color
Inside .btn-primary, set background-color to $main-color. Inside .btn-secondary, set background-color to #2ecc71.
SASS
Hint
Use background-color: $main-color; for .btn-primary and background-color: #2ecc71; for .btn-secondary.
4
Add common text color and hover effect
Add color: white; to the .btn class. Then add a hover effect inside .btn-primary that changes background-color to darken($main-color, 10%) using the Sass darken() function.
SASS
Hint
Add color: white; inside .btn and use &:hover inside &-primary with background-color: darken($main-color, 10%);.
Practice
(1/5)
1. Which of the following is the best way to reduce the size of compiled CSS when using Sass?
easy
A. Write very deep nesting of selectors for clarity
B. Use variables and mixins to avoid repeating the same styles
C. Add comments in Sass files to explain styles
D. Use many separate Sass files without combining them
Solution
Step 1: Understand Sass features for reuse
Variables and mixins let you reuse style code instead of repeating it multiple times.
Step 2: Compare with other options
Deep nesting creates many selectors increasing CSS size. Comments do not reduce CSS size. Many files without combining can increase HTTP requests.
Final Answer:
Use variables and mixins to avoid repeating the same styles -> Option B
Quick Check:
Reuse styles = smaller CSS [OK]
Hint: Reuse styles with variables and mixins to shrink CSS [OK]
Common Mistakes:
Thinking deep nesting reduces CSS size
Believing comments affect compiled CSS size
Assuming splitting files always reduces size
2. Which Sass syntax correctly defines a mixin to reduce repeated styles?
easy
A. @function button-style() {\n padding: 1rem;\n border-radius: 0.5rem;\n}
B. @include button-style {\n padding: 1rem;\n border-radius: 0.5rem;\n}
C. @extend button-style {\n padding: 1rem;\n border-radius: 0.5rem;\n}
D. @mixin button-style {\n padding: 1rem;\n border-radius: 0.5rem;\n}
Solution
Step 1: Identify mixin syntax
The correct way to define a mixin is using @mixin name { ... }.
Step 2: Differentiate from other directives
@include is used to use a mixin, not define it. @function defines functions, not mixins. @extend is for inheritance, not mixin definition.