CSS Preprocessor | SASS

SASS is the abbreviation of Syntactically Awesome Style Sheets. It is a CSS pre-processor with syntax advancements. The style sheets in the advanced syntax are processed by the program and compiled into regular CSS style sheets, which can be used in the website. It is a CSS extension that allows to use features like variables, nesting, imports, mixins, inheritance, etc, all in a CSS-compatible syntax.

Note: The link describes the installation process of SASS. Ref: https://sass-lang.com/install

There are two types of syntax available for SASS:



  • SCSS(Sassy CSS): The files using this syntax use .scss extension.
  • Indented syntax (referred to as just “sass”): older syntax, Files using this syntax use .sass extension.

Note: This example use .scss extension.

Working Steps:

  • Write the SCSS code.
  • Compile the SCSS code into CSS code using the command sass input.scss output.css. The first filename (input.scss) is the scss file that is to be compiled and the second file name (output.css) is the processed CSS file, to be included/attached in the html document.
  • Include the compiled CSS file in the html file.

Now see how to make effective use of the important features of SCSS like variables, nesting, mixins and operators.

  • The main HTML file is named index.html
  • SCSS file is styling.scss and the CSS file is style.css
  • Command to compile the SCSS file: sass styling.scss style.css

Example: File name index.html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
          
        <title>SASS</title>
          
        <meta name="viewport" content=
            "width=device-width, initial-scale=1">
              
        <link rel="stylesheet" href="style.css">
    </head>
      
    <body>
        <div id="d1">Welcome to GeeksforGeeks.
            <ul>
                <li>Algo</li>
                <li>DS</li>
                <li>Languages</li>
                <li>Interviews</li>
                <li>CS subjects</li>
            </ul>
        </div>     
    </body>
</html>                    

chevron_right


  • Variables: Variables can be used to store CSS value that may be reused. To declare a variable in SASS, ‘$’ character is used. Example: $v_name

    After compiling the CSS code save it file by style.css

  • Nesting: SASS allows CSS rules to be nested within each other, which follows the same visual hierarchy of HTML. For Example: CSS property can be used to the <li> tag nested inside the div tag.

    After compiling the CSS code save it file by style.css

    Output:

  • Mixins: Mixins helps to make a set of CSS properties reusable i.e. it save one code and use it again and again. It can be included in other CSS rules by using @include directive.

    Example:

    After compiling the CSS code becomes:

    The output of web page:

    Example: Mixins can also take variables as arguments. The values are passed while including them in the CSS rules.

    The compiled CSS code:

Final Output:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.