Post

Created by @mattj
 at November 9th 2023, 4:41:15 am.

Getting Started with SASS

SASS (Syntactically Awesome Style Sheets) is a powerful CSS preprocessor that allows you to write more efficient and maintainable CSS code. In this post, we will walk you through the process of setting up SASS in your project and cover some basic syntax and file organization techniques.

Step 1: Installation

The first step in getting started with SASS is to install it on your machine. SASS can be installed via various methods, depending on your preferred platform:

macOS / Linux:

For macOS and Linux users, SASS can be installed using the package manager, RubyGems. Open your terminal and execute the following command:

gem install sass

Windows:

Windows users can install SASS by downloading the installer from the official SASS website at https://sass-lang.com/install. Follow the installation wizard and make sure to add SASS to your system's PATH.

Step 2: File Structure

Once SASS is installed, it's time to set up your project's file structure. A common practice is to create a separate directory for your SASS files, typically named sass or scss. Within this directory, you can create multiple SASS files, each containing a specific section of your project's styles.

For example, you can create files such as variables.scss, mixins.scss, and styles.scss to separate your variables, mixins, and main styles, respectively.

It's important to note that SASS files should be saved with the .scss extension, as this is the default syntax of SASS. However, SASS also supports the older, indented syntax .sass. You can choose the syntax that you feel most comfortable with.

Step 3: Compiling SASS

Now that you have your SASS files set up, it's time to compile them into regular CSS. There are several ways you can compile SASS, but the most common approach is to use a SASS compiler.

There are many SASS compilers available, both as command line tools and as part of build systems or task runners. One popular command line compiler is the sass gem that we installed earlier. To compile a SASS file using the sass command, navigate to your project's root directory in the terminal and execute the following command:

sass sass/styles.scss:css/styles.css

This command tells the sass compiler to compile the styles.scss file and output the resulting CSS into a file named styles.css in the css folder.

Step 4: Watching for Changes

Manually running the SASS compiler every time you make a change to your SASS files can be a tedious process. Luckily, most SASS compilers provide a way to watch for changes and automatically recompile your SASS files.

To watch for changes using the sass compiler, simply add the --watch option to the compile command:

sass --watch sass/styles.scss:css/styles.css

Now, whenever you save changes to your SASS files, the compiler will automatically recompile them into CSS.

Congratulations! You have now successfully set up SASS in your project and learned the basics of compiling and watching SASS files. In the next post, we will explore some more advanced SASS techniques, such as partials, imports, extending selectors, mixins, functions, if/else statements, and loops. Stay tuned!