How to use SASS in your HTML code

SASS stands for Syntactically Awesome Style Sheets. It is mainly compiled to ordinary CSSCascade Style Sheets in your code.

SASS allows us to do more with CSS, such as:

  • create variables
  • mixins
  • nest CSS
  • create loops
  • reuse CSS rules

How to use SASS in HTML

The following steps tell us how to run SASS in your Visual Studio Code editor. An example of how to style a <p>: paragraph element is given later on.

Step 1

Install the Live Sass Compiler extension in the browser’s extension bar.

Here is a link to the Live Sass Compiler extension.

Step 2

Create a HTML file and name it index.html. Include the <p> element that we want to style with SASS.


Note: Please note that browsers do not recognize SASS, so a compiler needs to compile it to CSS.

  • HTML
HTML file

Step 3

Create a .scss file at the same root level as the index.html file and name it index.scss. This is our SASS file.

Step 4

Lastly, add the following code that will style your <p> element.

$red-bg : red;
p{
background-color: $red-bg;
}

Based on the code above, we want to give our <p> element a red background color.

Step 5

Add the following to the <head> tag of your HTML file.


<link rel="stylesheet" href="index.css">

The extension we installed will compile the SASS file index.scss into CSS and store the compiled code in a new CSS file, also called index.css. This is the reason for the code above, href = "index.css".

Step 6

Now, to start the compiler, click “Run”. This will compile the SASS code to CSS.

Complete code

Free Resources