CSS Preprocessor SASS (original) (raw)

Last Updated : 09 Jan, 2025

Sass (Syntactically Awesome Style Sheets) is a CSS preprocessor that enhances standard CSS by introducing features like variables, nesting, imports, mixins, and inheritance, all while maintaining compatibility with all CSS versions.

Key Features of Sass:

Installation:

Sass Syntax Variants:

Workflow Steps:

**Example: File name _index.html

HTML `

Welcome to GeeksforGeeks.

`

**Variables: Variables can be used to store CSS values that may be reused. To declare a variable in SASS, the ‘$’ character is used. For eg, $v_name.

$fs: 30px; $bgcolor: #00ff40; $pd: 100px 350px; #dl { font-size: $fs; color: $bgcolor; padding: $pd; }

This fig. shows the same code:

After compiling the CSS code, save it in file by _style.css.

#dl { font-size: 30px; color: #00ff40; padding: 100px 350px; }

**Nesting****:** SASS allows CSS rules to be nested within each other, which follows the same visual hierarchy of HTML. For eg. CSS property can be used to the

  • tag nested inside the div tag.
  • $fs: 30px; $bgcolor: #00ff40; #col2: #ff0066e1; $pd: 100px 350px; #dl { font-size: $fs; color: $bgcolor; padding: $pd; li { color: $col2; } }

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

    #dl { font-size: 30px; color: #00ff40; padding: 100px 350px; } #dl li { color: #ff0066e1; }

    **Output:

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

    **Example: This example describes the use of @mixin & @include.

    $fs: 30px; $bgcolor: #00ff40; #col2: #ff0066e1; $pd: 100px 350px; @mixin font_style() { font-family: sans-serif; font-size: $fs; color: blue; } #dl { @include font_style(); padding: $pd; }

    After compiling the CSS code becomes:

    #dl { font-family: sans-serif; font-size: 30px; color: blue; padding: 100px 350px; }

    **The output of the web page:

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

    $fs: 30px; #col2: #ff0066e1; $pd: 100px 350px; @mixin font_style() { font-family: sans-serif; font-size: $fs; color: blue; } @mixin list_style($size, $color) { font-size: $size; color: $color; } #dl { @include font_style(); padding: $pd; li { @include list_style(20px, red); } }

    The compiled CSS code:

    #dl { font-family: sans-serif; font-size: 30px; color: blue; padding: 100px 350px; } #dl li { font-size: 20px; color: red; }

    **Final Output:

    Best Practices