Open In App

Getting started with Sass

SASS (Syntactically Awesome Stylesheets) is a CSS preprocessor that adds features such as variables, nested rules, and mixins (reusable blocks of styles) to the basic language. It is a scripting language that is interpreted or compiled into CSS. SASS code is more readable and maintainable than plain CSS, and it can be more efficient to write styles in SASS and then convert them to CSS for use on a website. The Preprocessor is a program that inputs data to produce output that is used as input to another program. The output of sass acts as input for CSS. SASS uses the below fundamental concepts:

Generally, SASS is preferred to be used instead of CSS, as Sass is much rich in features than CSS, like variables, nested rules, mixins, imports, inheritance, built-in functions, and other stuff. Writing all the styling code in a file makes it difficult to maintain and find bugs. SASS allows to make of many stylesheets for different components of the webpage and makes it easier to maintain styling. Some of the main reasons for the Sass being preferred over CSS, which is described below:



We often see two types of file extensions while working with sass, ie, Style.sass & Style.scss. Sass is an older syntax, which relies on indentation and lines for syntax rather than semicolons and brackets.

SASS syntax:



/* Sass syntax */
body 
    box-sizing: border-box
    font-size: 100%
div
    background-color: green

SCSS syntax: As its name suggests, it uses every valid CSS3 styling in SCSS as well.

/* Scss syntax */
body { 
    box-sizing: border-box;
    font-size: 100%;
}
div {
    background-color: green;
}

Generally, it is preferred to use the .scss file extension because the syntax is the same as css3 while in the .sass extension syntax depends on indentations.

Sass Variables Sass variables let you reuse the styling. With Sass, you can store information in variables. Sass uses the $ symbols, followed by a name of a variable.

Syntax:

$variableName: propertyValue;

Example:




$myFont: 'Rubik', sans-serif;
$myColor: green;
  
body{
  font-family: $myFont;
  color: $myColor;
}

Sass Variable Scope: Sass variable scope are valid at the nesting level where they are defined. We can change the value of a variable inside the nesting of any property and Sass variables that were declared inside any tag has local scope whereas variables declared outside the tag has global scope.

Example 1: This example describes the basic usage of the SASS Variable & its scope. In SCSS, we need to declare the variable only once which contains multiple values & that variable can be utilized throughout the code. Whereas, in CSS, we need to declare it wherever that variable gets used.




$myFont: 'Rubik', sans-serif
$myTextSize: 25px
$myColor:green;
body{
  $myFont: 'Roboto', sans-serif;
  font-family: $myFont; 
  font-size: $myTextSize;
  color:$myColor;
}
  
h1{
  font-family: $myFont;
}




body {
     font-family: 'Roboto', sans-serif;
     font-size: 25px;
     color: green;
}
 h1 {
     font-family: 'Roboto', sans-serif;
}




<!DOCTYPE html>
<html>
  
<head>
    <title>SASS Concept</title>
  <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <h1>GeeksforGeeks Learning</h1>
</body>
  
</html>

Output:

 

It’s the default behavior of sass variable scope, to change the local scope behavior we use !global.

Sass !global: The !global is used to change the scope of variables from local to global.




/* This variable has global scope */
$myFont: 'Rubik', sans-serif;
/* This variable has global scope */
$myColor: green
  
body {
   /* This variable has local scope */
  $myFont: 'Roboto', sans-serif !global; 
    
  /* Roboto font is used */
  font-family: $myFont; 
  color: $myColor;
}
  
h1{
  /* Roboto font is used because 
  overwrites the variable in body tag */
  font-family: $myFont;
}

Compiled CSS:




/* This variable has global scope */
/* This variable has global scope */
 body {
    /* This variable has local scope */
     font-family: 'Roboto', san-serif;
    /* Roboto font is used */
     color: green;
}
 h1 {
     font-family: 'Roboto', san-serif;
    /* Roboto font is used because overwrites
   the variable in body tag */
}

Example 2: This is another basic example that describes the basic implementation of the SASS Concept.




<!DOCTYPE html>
<html>
    
<head>
    <title>SASS Concept</title>
      <link rel="stylesheet" href="style.css">
</head>
<body>
    <h1>Hello Geeks</h1>
</body>
    
</html>

We can import the sass file using the <link> tag into your HTML file. Sass code that displays the text “Hello Geeks” at the center of the screen with a green background:




$background-color: green
  
body {
  background-color: $background-color;
  display: flex; 
  align-items: center
  justify-content: center
}
  
h1 {
  font-size: 36px
  color: white
  text-align: center
}




body {
     background-color: green;
     display: flex;
     align-items: center;
     justify-content: center;
}
 h1 {
     font-size: 36px;
     color: white;
     text-align: center;
}

Output:

 

Conclusion: This article has covered the important concept of sass like Sass VS SCSS, and sass variables. The use of SASS over regular CSS can provide several advantages that can help improve the development process and the final product. In summary, SASS offers a set of advanced features that makes it much more powerful than CSS alone, It helps to make the development process more organized, efficient, and maintainable.


Article Tags :