Sass Variables

Sass Variables – Complete Practical Guide
Sass variables allow you to store reusable values like colors, fonts, sizes, and spacing in one place and use them throughout your stylesheet.
Think of Sass variables as constants for your CSS.
1. What Are Sass Variables?
Sass variables store values using the $ symbol.
Instead of repeating the same value everywhere, you define it once and reuse it.
Problem in CSS
If the color changes → you must update it everywhere
2. Syntax
Example
Change the variable → updates everywhere
3. Common Types of Sass Variables
3.1 Colors
3.2 Fonts
3.3 Spacing
3.4 Numbers
4. Variables in Real Use
Clean, readable, maintainable.
5. Variable Scope (Very Important)
5.1 Global Scope
Variables declared outside selectors are global.
Accessible everywhere.
5.2 Local Scope
Variables inside blocks are local.
Not accessible outside .card.
6. Default Values (!default)
Used for libraries and themes.
If user defines $theme-color earlier → Sass keeps that value.
If not → default is used.
7. Overriding Variables
Later definitions override earlier ones.
8. Using Variables with Calculations
Sass supports math operations.
9. Variables in Media Queries
Makes breakpoints reusable and consistent.
10. Variables with Colors Functions
Easy theme control
11. Partial Files & Variables
Usually variables are stored in a separate file.
_variables.scss
style.scss
Best practice for large projects.
12. Sass Variables vs CSS Variables
| Feature | Sass Variables | CSS Variables |
|---|---|---|
| Evaluated | Compile time | Runtime |
| Scope | File-based | DOM-based |
| JS Access | No | Yes |
| Media query changes | No | Yes |
- Sass variables = design-time
- CSS variables = runtime
13. Common Mistakes
Forgetting
$symbolUsing It in plain
.cssfilesExpecting Sass variables to change at runtime
Overusing variables unnecessarily
Confusing Its variables with CSS custom properties
14. Interview-Level Questions
Q: What symbol is used for It?$
Q: Are It available at runtime?
No, only at compile time.
Q: Can It be used in media queries?
Yes.
Q: Difference between Sass and CSS?
Compile-time vs runtime.
Best Practices
Use meaningful variable names
Group variables (colors, spacing, fonts)
Store variables in separate files
Use
!defaultfor themesAvoid hardcoding repeated values
Final Summary
It store reusable values
Defined using
$Make CSS cleaner and maintainable
Evaluated at compile time
Essential for scalable Sass architecture
