Sass Basics
Introduction
Sass (Syntactically Awesome Stylesheets) is a powerful CSS preprocessor that enhances CSS with variables, nesting, mixins, functions, and more. It helps developers write cleaner, more maintainable, and more efficient styles.
In this guide, we will cover the basics of Sass, including installation, syntax, and essential features.
Step 1: Installing Sass
Before using Sass, you need to install it. You can install Sass globally on your system or use it with Node.js.
1.1 Install Sass via npm (Recommended)
If you have Node.js installed, run the following command:
1.2 Install Sass via Homebrew (For Mac users)
1.3 Check Sass Version
To verify Sass is installed, run:
Step 2: Sass Syntax (SCSS vs. Indented Syntax)
Sass comes in two syntaxes:
- SCSS (Sassy CSS) – Uses curly braces
{}
and semicolons;
(similar to CSS). - Indented Syntax (Sass) – Uses indentation instead of brackets and semicolons.
👉 SCSS Example:
👉 Indented Syntax Example:
💡 SCSS is more popular since it’s closer to traditional CSS.
Step 3: Using Variables in Sass
Variables store values like colors, fonts, and sizes for reuse.
Step 4: Nesting in Sass
Sass allows you to nest selectors inside one another, making the code more readable.
Step 5: Partials and Importing
You can split your styles into smaller files (partials) and import them into a main stylesheet.
5.1 Create a Partial File
Save a file starting with an underscore _variables.scss
:
5.2 Import the Partial into Another File
Step 6: Mixins for Reusability
Mixins allow you to reuse styles with parameters.
Step 7: Using Extend to Avoid Repetition
The @extend
feature lets one selector inherit another's styles.
Step 8: Compiling Sass to CSS
To convert .scss
files into .css
, run:
For automatic compilation:
Conclusion
Sass simplifies and improves CSS with variables, nesting, mixins, and more. Mastering Sass will enhance your styling workflow and improve project maintainability.
Try Sass in your next project and make CSS development more efficient! 🚀
Would you like any modifications or additions? 😊