PEB

Phil Elliott Blog

How to get started with SASS

Main Article Image

SASS is a powerful CSS pre-processor that helps make writing CSS easier and more efficient. It offers features such as variables, nesting, mixins, placeholders, and functions that allow you to create DRY, concise, and well-organized code. In this tutorial, we will cover the basics of SASS syntax so you can get started using it today.

What is SASS and what are its benefits

Some of the benefits of using SASS include:

1. Increased flexibility: With SASS, you can easily change the colors, fonts, and other styling properties of your website without having to go through each individual CSS file.

2. Improved organization: By using SASS's nesting feature, you can group related CSS properties together and thereby keep your code more organized and easy to read.

3. Reduced file size: Since SASS generates clean and optimized CSS code, your website's file size will be smaller in comparison to if you had used regular CSS.

How to install SASS

To install SASS with Node, you first need to make sure that you have Node.js installed on your computer. You can download Node.js from the official website:

Once you have Node.js installed, you can install the SASS compiler by running the following command in the terminal:

npm install -g sass

After the installation is complete, you can verify that SASS has been successfully installed by running the following command:

sass --version

This should output the current version of SASS that is installed on your system.

If you don't want to install with Node, then SASS can be installed with some other options that can be found at https://sass-lang.com/install.

Using variables, nesting, mixins, placeholders, and functions in SASS

SASS variables

The first thing you need to know about SASS variables is that they are a name written after a dollar sign ($). A SASS variable can be any word, but it's best to use short, descriptive variable names.

To create a variable in SASS, you simply write a dollar sign ($) and the variable name followed by the equals sign (=) and then the value of the variable. For example, the following code sets the variable "bg-color" to be "white":

$bg-color = white

SASS nesting

Nesting is one of the features that makes SASS so powerful and efficient. Nesting allows you to group related CSS properties together so that they can be easily accessed and edited. The following code shows an example of nesting:

.main {
  color: black; 
  background-color: red;
  
  h1 {
    color: yellow;
  }
  
  p {
    color: white;
  }
}

In this code, the ".main" class has two properties: "color" and "background-color". Within the ".main" class, there are two elements that we will be adding style to.

All headings (h1) and paragraphs (p) within the main class will now have these styles applied, whereas those outside of the main class will not.

So, as you can see, nesting allows you to easily keep your CSS code tidy and well-organized.

SASS mixins

Mixins allow you to create reusable pieces of CSS code that can be called from other classes or selectors. This is helpful when you want to use the same style multiple times on your website, but don't want to copy and paste the same style over and over again.

Mixins can be seen as functions that don't have a return value. We generally pass in an argument when we use a mixin.

To create a mixin in SASS, you simply write the mixin name after the @mixin keyword and then the list of properties that you want to include in the mixin.

For example, the following code creates a mixin called "border-radius":

@mixin border-radius( $radius ) { 
  -webkit-border-radius: $radius; 
  -moz-border-radius: $radius; 
  border-radius: $radius; 
}

The "border-radius" mixin takes one parameter: $radius. This parameter represents the radius of the border that you want to create.

So, if you wanted to create a border with a radius of 5px, you would write the following code:

.element { 
  @include border-radius(5px); 
}

SASS placeholders

A placeholder is a type of mixin that is used when you want to create a class or selector that will not generate any CSS code on its own. Placeholders are helpful when you want to create a style that will be used multiple times, but don't want to add any extra CSS code to your website.

To create a placeholder in SASS, you write the placeholder name after the % symbol. For example, the following code creates a placeholder called "border":

%border { 
  border: 1px solid #000; 
}

The "%border" placeholder contains one CSS property: "border". This property will be used whenever the "%border" placeholder is called.

To use a placeholder, you write the @extend keyword followed by the placeholder name.

For example, the following code extends the "%border" placeholder:

.element { 
  @extend %border; 
}

SASS functions

Functions are a way to perform operations on SASS variables. They are helpful when you want to manipulate a variable in some way. For example, you may want to take a color and make it lighter or darker.

To create a function in SASS, you write the function name after the keyword "function". The following code creates a function called "lighten":

@function lighten( $color, $amount ) {
  @return mix( white, $color, $amount );
}

The "lighten" function takes two parameters: $color and $amount. The $color parameter represents the color that you want to manipulate. The $amount parameter represents how much you want to lighten the color. So, if you wanted to lighten a color by 10%, you would write the following code:

.nav {
  color: lighten( blue, 10% );
}

This code would return a color that is 10% lighter than the "blue" color.

Conclusion

As you can see, SASS offers a lot of powerful features that can help make your CSS code more efficient and easier to write. Now that you know the basics of how to get started with SASS, try using it on your next project!