Setting Up SASS/SCSS in Your Project

Tutorial 2 of 5

Introduction

In this tutorial, we will guide you on how to set up a SASS/SCSS environment in your project. This will enable you to compile your SASS/SCSS code into CSS, which is essential for styling your website.

You will learn:
- How to install Node.js and npm
- How to install SASS/SCSS
- How to compile SASS/SCSS into CSS

Prerequisites:
- Basic knowledge of HTML and CSS
- Basic understanding of the command line interface (CLI)

Step-by-Step Guide

1. Install Node.js and npm

Node.js is a JavaScript runtime that allows you to run JavaScript on your server. npm (Node Package Manager) is a package manager for JavaScript, which comes with Node.js when you install it.

To install Node.js and npm, visit the official Node.js website and download the installer.

2. Install SASS

After installing Node.js and npm, open your CLI and type the following command to install SASS globally.

npm install -g sass

This command installs SASS globally, which means you can use it in any of your projects.

3. Create a SASS file

Create a new .scss file in your project directory. You can name it whatever you want, but for this tutorial, we'll call it style.scss.

body {
  background-color: #f0f0f0;
}

This is a basic SASS/SCSS code that sets the background color of the body element to #f0f0f0.

4. Compile SASS into CSS

To compile your SASS/SCSS code into CSS, use the following command in your CLI.

sass style.scss style.css

This command compiles style.scss file into style.css file.

Code Examples

Example 1: SASS Variables

SASS allows you to use variables, which CSS doesn't support. Here's an example:

$bg-color: #f0f0f0;

body {
  background-color: $bg-color;
}

In this example, $bg-color is a variable holding the value #f0f0f0. This value is then assigned to the background-color property.

Example 2: SASS Nesting

SASS enables you to nest your CSS selectors in a way that follows a similar visual hierarchy of your HTML.

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  li { display: inline-block; }

  a {
    display: block;
    padding: 0 10px;
    color: #000;
  }
}

In this example, ul, li, and a are nested inside nav. This code compiles into the following CSS:

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  display: inline-block;
}

nav a {
  display: block;
  padding: 0 10px;
  color: #000;
}

Summary

In this tutorial, you have learned how to set up a SASS/SCSS environment in your project. Now you can compile your SASS/SCSS code into CSS. You've also learned about SASS variables and nesting.

For further learning, you can explore more about SASS, such as mixins, inheritance, and operators.

Practice Exercises

  1. Exercise 1: Create a .scss file with a variable for the color property and compile it into a .css file.

  2. Exercise 2: Create a .scss file with nested selectors and compile it into a .css file.

Solutions:
1. Solution for Exercise 1:

$main-color: #ff0000;

body {
  color: $main-color;
}
  1. Solution for Exercise 2:
header {
  nav {
    a {
      color: #000;
    }
  }
}

Continue practicing to improve your SASS/SCSS skills. You can create complex projects and try to style them using SASS/SCSS.