Sass (or Syntactically Awesome Stylesheets) is a CSS preprocessor that adds features to CSS to make it more powerful and expressive. It allows you to do things like variables, nested rules, mixins, and functions, which can make your code more maintainable and reusable.

In this guide, we'll cover the basics of SCSS syntax, as well as some best practices for using it. We'll also show you some examples of how you can use SCSS to improve your CSS code.

What is SCSS?

Sass is a preprocessor for CSS. This means that it's a tool that converts SCSS code into CSS code that can be used by browsers. SCSS code is written in a way that makes it easier to write complex CSS code, and it also provides some features that aren't available in regular CSS.

SCSS Syntax

SCSS syntax is very similar to CSS syntax. The main difference is that SCSS uses the @ symbol to introduce special constructs, such as variables, nested rules, mixins, and functions.

Variables

Variables are a way to store values that can be used throughout your SCSS code. To create a variable, you use the @variable syntax:

@color: red;

You can then use the variable in your CSS code by using the var() function:

.my-element {
  color: var(--color);
}

Nested Rules

Nested rules allow you to nest CSS rules within each other. This can be useful for organizing your code and for making it easier to reuse code.

To create a nested rule, you simply add a new rule within the opening curly brace of an existing rule:

.my-element {
  color: red;
  .my-child-element {
    font-size: 16px;
  }
}

Mixins

Mixins are a way to reuse code. To create a mixin, you use the @mixin syntax:

@mixin my-mixin() {
  color: red;
  font-size: 16px;
}

You can then use the mixin in your CSS code by using the @include function:

.my-element {
  @include my-mixin();
}

Learn more about Mixin.

Functions

Functions are a way to create reusable code that takes arguments. To create a function, you use the @function syntax:

@function my-function($color) {
  color: $color;
}

You can then use the function in your CSS code by using the my-function() function:

.my-element {
  color: my-function(red);
}

Responsive Layouting

Sass can be used to create responsive layouts. Responsive layouts are layouts that adapt to different screen sizes. This means that your website will look good on all devices, from desktop computers to mobile phones.

To create a responsive layout, you can use Sass's @media queries. @media queries allow you to specify different styles for different screen sizes.

For example, you could use the following @media query to style a container to be 100% width on desktop computers, and 768px wide on mobile phones:

nav {
  background-color: blue;

  ul {
    list-style-type: none;
    margin: 0;
    padding: 0;

    li {
      a {
        display: block;
        font-size: 16px;
        color: white;

        &:hover {
          text-decoration: underline;
        }
      }
    }

    @media (max-width: 768px) {
      ul {
        display: flex;
      }

      li {
        a {
          font-size: 14px;
        }
      }
    }
  }
}

This code would create a navigation bar with a blue background and a list of links. The links would be displayed in a single column on wider screens, but they would be displayed in a flex layout on narrower screens.

Pseudo Elements

.button {
  border: 1px solid black;
  padding: 10px;
  cursor: pointer;

  &::after {
    content: "";
    border-bottom: 4px solid green;
    display: inline-block;
    width: 100%;
    height: 4px;
    margin-top: -2px;
  }

  &:hover::after {
    border-bottom-color: red;
  }
}

This code will create a button with a solid black border and 10px padding. When the button is hovered over, the border will change from green to red. The ::after pseudo element is used to create a small underline below the button. The :hover pseudo class is used to change the color of the underline when the button is hovered over.

Here is an explanation of what each line of code does:

  • .button { - This line creates a new CSS rule with the selector .button.
  • border: 1px solid black; - This line sets the border of the button to 1px solid black.
  • padding: 10px; - This line sets the padding of the button to 10px.
  • cursor: pointer; - This line sets the cursor to a pointer, which means that the user can click on the button.
  • &::after { - This line creates a new pseudo element with the selector .button::after.
  • content: ""; - This line sets the content of the pseudo element to an empty string.
  • border-bottom: 4px solid green; - This line sets the border of the pseudo element to 4px solid green.
  • display: inline-block; - This line sets the display of the pseudo element to inline-block.
  • width: 100%; - This line sets the width of the pseudo element to 100%.
  • height: 4px; - This line sets the height of the pseudo element to 4px.
  • margin-top: -2px; - This line sets the margin-top of the pseudo element to -2px.
  • &:hover::after { - This line creates a new pseudo class with the selector .button:hover::after.
  • border-bottom-color: red; - This line sets the border-bottom color of the pseudo element to red.

Learn more about pseudo-elements.

Best Practices

Here are some best practices for using SCSS:

  • Use variables to store values that are used throughout your code. This will make your code more maintainable and reusable.
  • Use nested rules to organize your code and to make it easier to reuse code.
  • Use mixins to reuse code that is used in multiple places.
  • Use functions to create reusable code that takes arguments.
  • Use comments to explain your code and to make it easier to understand.
  • Use a consistent coding style. This will make your code easier to read and maintain.

Examples

Here are some examples of how you can use SCSS to improve your CSS code:

  • You can use SCSS variables to store colors and other values that are used throughout your code. This will make your code more maintainable and reusable.
  • You can use SCSS nested rules to organize your code and to make it easier to reuse code.
  • You can use SCSS mixins to reuse code that is used in multiple places.
  • You can use SCSS functions to create reusable code that takes arguments.
  • You can use comments to explain your code and to make it easier to understand.
  • You can use a consistent coding style. This will make your code easier to read and maintain.