All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Use SCSS In HTML

Last Updated : Jul 1, 2023

How To Use SCSS In HTML

In this tutorial we will show you the solution of how to use SCSS in HTML, SCSS stands for Sassy Cascading Style Sheet and is a superset of CSS (cascading style sheet).

It is a more advanced extension of CSS, developed by Chris Eppstein and Natalie Weizenbaum and designed by Hampton Catlin.

Step By Step Guide On How To Use SCSS In HTML :-

A browser cannot read a SCSS file. Hence something is required to make it readable for the browser. This is where a SCSS compiler comes to the picture.

The SCSS compiler compiles the SCSS file into a CSS file so that the browser can read it like it’s an usual CSS file. Now we will show you step-by-step on how it’s done.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>How To Use SCSS In HTML</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <p id="para">Have Fun At Talkerscode!</p>
</body>
</html>

Below is the content of style.scss file

#para {
    background-color: blue;
    color: white;
    text-align: center;
}
  1. For this tutorial we will be using Visual Studio Code.
  2. Firstly we create a .html file and write <!DOCTYPE html> at the top of the file. This is to inform the browser about the version of HTML we are using.
  3. Secondly, we use the lang attribute of HTML to specify the language of the file content. Here en stands for English.
  4. Next comes the <head> tag which includes information about the content of the HTML file. The <title> tag comes as a part of this information which shows the title of the HTML file on the browser tab.
  5. Now we are using a <link> tag inside the <head> tag to include an external CSS file. You might have a question that we are to include .scss file, then why are we writing .css file inside href attribute? Your question is valid and we are coming to this in a bit.
  6. Then begins the <body> tag to hold the body content of the HTML file.
  7. We write a short line with the help of a <p> tag. This <p> tag is given the id=para to help us easily reference it. This is also the line we are going to style using CSS.
  8. Now in the same directory as the HTML file, we will create a new file called style.scss and write the styling code as shown in the next segment of code. The styling applied would be to change the background colour, text colour and text alignment properties of the paragraph denoted by <p> tag.
  9. Let us note at this point that the <html>, <head>, <title>, <body> and <p> tags are all paired or container tags, i.e they have respective ending tags as </html>, </head>, </title>, </body> and </p>. The <link> tag however is an unpaired or singular tag and doesn’t have any ending tag.
  10. We will now run this HTML file.
  11. You found something strange right? Even though we applied styling to the <p> tag, no styling seems to be applied. Okay, You may say that we had style.scss file and we wrote style.css file in the href attribute of <link> tag, so it wouldn’t run. You’re aabsolutely right! Let’s close the browser and get back to our HTML file.
  12. Now we will press Ctrl+Shift+X and install Live Sass Compiler by Ritwick Dey from the Extensions Library.
  13. After it installs, we will go to the bottom panel of Visual Studio Code and click on Watch Sass. This will become Watching.. now.
  14. Now we will run the HTML file once again and open the browser. Voila! All our stylings have been applied now! Isn’t this awesome?
  15. Now You may be thinking that last time the href value was style.css and we added styling code in style.scss and that’s why it didn’t execute correctly. However this time also, they are different. How is it running correctly? Your question is spot on correct this time too!
  16. Internally the Live Sass Compiler is compiling the style.css file into a style.css file which the browser can understand. You can also see in the directory of your HTML file that two CSS files have been created (style.css and style.css.map). Our HTML file is reading this newly created style.css (out of Live Sass Compilation) in the href attribute of the <link> tag and due to this now we are able to see the styling in action.
  17. Just remember to click on “Watch Sass” everytime you use scss files to add styling. After You’re done with the work, You have to just click on Watching.. to get back to default state.

Conclusion :-

I hope this tutorial on how to use SCSS in HTML helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪