What Is Scss And How To Use Scss In HTML? [ Guide #2023]

CSS stands for Sassy Cascading Style Sheets and it’s an extension of CSS which adds nested rules Variables, mixin, selector, inheritance and a lot more features. Now Scss is written in Ruby and it’s intended to make our lives a lot easier in writing CSS.

 

how to use scss in html

CSS or SCSS – Which is better?

CSS is the styling language that is used to style web pages and it is understandable by the browser. Using SCSS, we can add any additional functionality to CSS such as nesting, mixin, variables, and more. All these additional functionalities can make writing CSS much easier and faster as compared to writing the traditional CSS. SCSS produces a traditional CSS that the browser can understand by running the SCSS files on the server running your web app.

 

HOW TO USE SCSS

Scss helps us to organize as clutter and make it into really specific modules and 16 pieces of code. Another reason is that syntax is easy to learn especially if we already know CSS. Now there are a few specific differences with Scss like the use of indentation and dollar signs to create variables.

 

But overall the syntax is quite identical to CSS, So it’s very easy to learn. One of the most important reasons to use SAS is that with the use of variables and various other features we’re able to reuse a lot of our code. One of the things that our software developers and most are having to repeat themselves.

 

Scss is a preprocessor, It means simply a program that processes input data and produces an output and then that output will be used in another program. Sass has different syntaxes now there are two popular ones called SCSS and SAS so we have SCSS which is standing for Scssy CSS and this is a superset of the CSS3 syntax and SCSS is more like a middleman between Scss and CSS then we have an actual Scss code so Scss will just be the full-on Scss file that will be compiled into CSS.

 

HOW SCSS WORKS?

Scss file containing a bunch of variables and using a bunch of the other features that Scss uses from there it’s going to go through a processor or a compiler and that compiler will spend out CSS that the browser will be able to read so all this is going to happen behind the scenes and Scss needs to be compiled into CSS because browser actually cannot read Scss so they need to be transformed back into CSS.

 

Advantages of using SCSS?

Advantages of using SCSS

SCSS is more expressive
We can compress several lines of code in SASS syntax into much less number of lines of SCSS. In SCSS, the standard lines can also be compressed when I’m doing something complicated and can be expanded again for reference.

SCSS allows the user to write better inline documentation
SASS is flexible with comments, but any good developer will prefer inline documentation which is available in SCSS. Inline documentation makes the lines of code self-explanatory.

It encourages proper nesting of rules
If you use the comma operator at a high level, it increases the file size of the final CSS. This can result in making the code really hard to perform overriding of rules.

Integrating existing CSS tools and CSS codebase is much easier
Syntax highlighting widely used CSS tool and is supported in SCSS. SCSS allows you use the existing code, and help improve its internal structure without altering the external behavior of the code.

Compile Scss To Css By Npm
  1.  create a folder of scss and then create a file that should be style.scss
  2.  create a folder of CSS and then create a file that should be style.css
  3. Then use this cmd.
sass --watch assets\scss\style.scss:assets\css\style.css

 

Best Tools to Compile SCSS

  1. VS Code
  2. Scout-App (MAC, Windows, Linux)- Free
  3. Prepros (MAC, Windows, Linux)-Paid
  4. Koala (MAC, Windows, Linux) – Free
  5. Compass
  6. Hammer (MAC)-Paid

CODE OF SYNTAX

SASS includes two types of syntax such as  SCSS (sassy CSS) is called the compliant of CSS which uses.scss as extensions

Indented (simply called sass) it uses. Sass as its extension, it is not compliant to CSS but they are indentation rather than brackets. It is quick to write the code.

VARIABLES

Variables as a way to store information that you want to reuse throughout your stylesheet now you can store things like colors, font stack or any CSS value that we think of one.
It would reuse and to do this with Scss where we’re going to use a dollar symbol to make something a variable so here’s an example where we have a variable called control height set to 40 pixels and we use that variable for a header class and a subheader class now if we were to write this in CSS we would simply write the number itself in this case we’re only using it for two classes but in long style sheets where we use multiple 50 to 100 classes.
$red: #FF0000;
body { color:$red; }

MIXINS

Mixin makes groups of CSS declarations that we may want to reuse throughout. So we can even pass in values to make our mixin more flexible.
So let’s take a look at an example here to create a mixin we’re going to use the add sign directive and give it a name so we have a mixin here called awesomely and we included in two different elements one called the body and one is a paragraph.
so that awesome mixin can have probably or how many other declarations it might want maybe want to put a border or maybe a radius or a font color and it can insert those properties into different elements as well so it’s very useful and this would translate like this and CSS.
where it’s a little bit more declarative and we have to specify the width and the height for each element.
@mixin reset-list {
     margin: 0; padding: 0; list-style: none;
 }
nav ul { @include reset-list; }

NESTING

Nesting is a good method of reducing the code in sword braces. The amount of code you write will be reduced in Nesting. If it could not be able to execute carefully it will be termed as over-qualified CSS. The main reason to nest the CSS Selector is to imitate your HTML hierarchy.
ul{
   li{ 
       a{ padding: .25em; line-height: 0;}
   }
 }

 Prefixing parent selector references

This is the familiar way you’re probably using &:

a { &:hover { color: red; } }
/* compiled CSS */
 a:hover { color: red; }

But & can be used with a prefix just as well:

p { body.no-touch
 & { display: none; 
// hide the message if not on a touch device
 }
 }

 

/* compiled CSS */ 
body.no-touch p { display: none; }

This can be very useful when you have a deep nesting of rules, and you want to effect a change to the styling of an element based on a selector match much closer to the DOM root.

EXTENDED /INHERITANCE

Most useful features in Scss called to extend or inheritance now with extent we can share a set of CSS properties from one selected to another it’s kind of similar to the mixin but it’s not the same. Little easier to use extent to pass those properties onto different elements.
.error { border: 1px #f00; background-color: #fdd; } 
p{ @extend .error; border-width: 3px; }

Placeholder selectors

Because in the above example(s) the .animal the base class isn’t used anywhere directly (only through its child classes), we might just as well get rid of it in the CSS output. SCSS allows this with placeholder selectors. Whereas .foo signifies a class, and #foo an ID, %foo is considered a placeholder, and gets special treatment by the compiler: its styles are never output on their own, only through extension.

%animal {
    background: gray;
    // and so on...
}
.cat {
    @extend %animal;
    color: white;
}
.dog {
    @extend %animal;
    color: black;
}

 

/* compiled CSS */
.cat, .dog {
  background: gray;
}
.cat {
  color: white;
}
.dog {
  color: black;
}

 

 

OPERATORS

The ability to perform calculations in CSS allows doing more such as Convert pixel values into a percentage. It will have access to addition, subtraction, multiplication, and divisions these functions are combined to form various complex calculations.
Scss has a new inbuilt function such as percentage(), floor(), round (), and many more used to manipulate the numbers.

 

IMPORT

Import parameters are used to import the partial file into your current files, to build the current CSS file. So for every Import, an HTTP request will be generated. So while importing the file use of an extension or any underscore is not necessary.
@import 'extra'';

 

PARTIALS

These Partials are imported into the SAAS files which are the shorter versions of the saas files. The code snippets of partials will allow CSS to be now more modular and can be maintained easily. These partials are named with an underscore. Such as the _partials.scss

 

How to use scss in react js

Install node-sass

Here’s a deeper dive. In order to add Sass to a Create React App application, we need a single command:

# using npm npm install node-sass --save 
# using yarn yarn add node-sass

 

Renaming .css to .scss

Once you have that, we can rename our App.css to App.scss.

Import the .scss File

In our App.js file, we can import the Sass file using the .scss extension.

import './App.scss';

 

Once we restart our yarn start command, we can see that our Sass file is imported!

 

 

 

 

CONCLUSION

As CSS preprocessor has remodeled the frontend development, so some designers cannot be able to code without them so, bringing preprocessor would be used to bring up and integrate it up in the workflow. Scss could be a very effective way to be replaced over CSS.
As it helps to provide good results than CSS. So writing the same code, again and again, is a better way to get replace by Scss instead of CSS.

Leave a Comment