SASS (Syntactically Awesome Style Sheets) is a powerful and popular CSS preprocessor that extends the capabilities of standard CSS. It offers numerous features such as variables, nested rules, mixins, and functions, making CSS more maintainable, reusable, and easier to write. Sass files are compiled into standard CSS for browser interpretation. SASS helps streamline your workflow and keeps your stylesheets organized and efficient.
Since browsers are unable to read a SASS file, so, we are required to use a SASS compiler that converts its file to a normal CSS file. It also helps reduce the overall length of the code by discarding the repeated CSS code and therefore saves time. It was designed by Hampton Catlin and developed by Natalie Weizenbaum in 2006.
What is SASS?
SASS (Syntactically Awesome Style Sheets) is a powerful and popular CSS preprocessor that extends the capabilities of standard CSS. It offers numerous features such as variables, nested rules, mixins, and functions, making CSS more maintainable, reusable, and easier to write. SASS helps streamline your workflow and keeps your stylesheets organized and efficient.
Why Use SASS?
- Variables: SASS allows the use of variables to store values such as colors, fonts, or any CSS value you need to reuse. This makes it easy to maintain and update styles across a project.
- Nesting: SASS supports nesting of CSS rules, which follows the same visual hierarchy of HTML. This makes your CSS more readable and maintainable.
- Partials and Imports: You can break down your CSS into smaller, modular pieces using partials and import them into a main stylesheet. This modular approach keeps your code organized.
- Mixins: Mixins allow you to create reusable chunks of CSS, avoiding repetitive code. You can pass variables to mixins for dynamic styling.
- Functions and Operations: SASS provides built-in functions and the ability to define your own, enabling complex calculations and transformations directly within your stylesheet.
- Inheritance: The @extend directive allows you to share a set of CSS properties from one selector to another, promoting DRY (Don't Repeat Yourself) principles.
The conversion of one syntax to another can automatically be done using the sass-convert command-line tool in the file. SASS can be implemented in 5 ways:
- Using the import statement
- Using the node & npm
- Using the Command-line tool
- Using the Standalone-Ruby module
- Using installing the Plugin
SASS Tutorial
Installation Step
We will follow the below steps to install the SASS through Package Manager.
Step 1: To install SASS, first make sure that node and npm are already installed in the system. If not, then install them using the instructions given below.
- First, download the latest version of a node in the system and install it.
- Now go to the command prompt and address the folder where you want to install SASS.
- After that, you have to create a package.json file. It manages the dependencies of our project.
- Use the command written below that will ask for the package name of the user’s choice and the description. Some more formalities are there, just press enter for that and your package.json file will be created.
npm init
Step 2: Now to install SASS one simple command is used:
npm install node-sass --save
Note: – –save in the above command is used to save the SASS in dependencies of JSON file. Now SASS has been installed in your system successfully.
Step 3: To work with SASS, go to the package.json file in your project, i.e. if you are working with VSC, open your project there and then open the package.json file.
You will get a package.json file like:
{
"name": "sass-ex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"author": "",
"license": "ISC"
}
Remove the “test” script and add your own script of name compile: sass (any other name can be chosen), give the link of your sass file as a target. package.json should look like this:
{
"name": "sass-ex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"compile:sass": "node-sass scss/style.scss css/style.css"
},
"author": "",
"license": "ISC"
}
Now go back to the command prompt and run the command:
npm rum compile:sass
Or just add a node-sass script like this:
Open the package.json file in your text editor of choice, and add the following line inside the “scripts” object:
"scss": "node-sass --watch assets/scss -o assets/css"
package.json file looks like this:
{
"name": "sass-ex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"scss": "node-sass --watch assets/scss -o assets/css"
},
"author": "",
"license": "ISC"
}
Save the file and close it. Now, in the root of the project directory, run the command (as given below) to start watching for any changes to your .scss files.
npm run scss
Approach
- Write the SASS code.
- Compile the SASS code into CSS code using the command sass style.scss result.css . The first filename (style.scss) is the scss file that is to be compiled and the second file name (result.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 style.scss and the CSS file is result.css
- Command to compile the SCSS file: sass style.scss result.css
Example: The below example describe the SASS code which is converted to a CSS file, which is then utilized in the index.html file.
index.html
<!DOCTYPE html>
<html>
<head>
<title>SASS Tutorial</title>
<link rel="stylesheet" href="result.css">
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>SASS</h3>
<p>This example illustates the use of
<a href="https://www.geeksforgeeks.org/sass-introduction/">SASS
</a>
</p>
<p>
<a href="https://www.geeksforgeeks.org/">
GeeksforGeeks
</a>
- A Computer Science portal for geeks.
</p>
</body>
</html>
style.scss
$bg: #909290;
$textColor: #ffffff;
$align: center;
$font: sans-serif;
$decoration: none;
$text: green;
body {
background: $bg;
color: $textColor;
text-align: $align;
font-family: $font;
}
h1 {
color: $text;
}
a {
text-decoration: $decoration;
color: $text;
}
result.css
body {
background: #454745;
color: #ffffff;
text-align: center;
font-family: sans-serif;
}
h1 {
color: green;
}
a {
text-decoration: none;
color: green;
}
Output:
Advantages of SASS
- Using SASS, we can write clean, well-structured & organized having less CSS in a programming construct.
- It is more powerful, stable & elegant which helps the developer to design & work efficiently, as it is compatible with all the CSS versions.
Similar Reads
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
CSS Tutorial
CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
6 min read
CSS Frameworks
Bootstrap Tutorial
Bootstrap is a popular front-end framework for building responsive and mobile-first websites. It provides pre-designed CSS, JavaScript components, and utility classes to quickly create modern and consistent user interfaces.It Includes pre-built responsive grid systems for mobile-first design.Offers
4 min read
Bootstrap 5 Tutorial
Bootstrap 5 is a front-end framework that helps developers create responsive and visually appealing websites quickly and efficiently. Bootstrap 5 simplifies the process of web development with its intuitive design system and extensive components. This version includes improved responsiveness, stream
6 min read
Tailwind CSS Tutorial
Tailwind CSS is a utility-first CSS framework that makes it easy to build custom designs without writing custom CSS. It allows you to apply individual utility classes directly in your HTML, which helps create fully customized layouts with minimal effort.Tailwind provides many utility classes for bui
7 min read
Bulma Tutorial
Bulma is an Open source CSS framework developed by Jeremy Thomas. This framework is based on the CSS Flexbox property. It is highly responsive and minimizes the use of media queries for responsive behavior.BulmaWhy Bulma?It is easy to learn.It is used to create responsive design for all devices (des
2 min read
Pure CSS
Pure CSS is a CSS framework. It is a set of free and open-source tools for building responsive websites and web applications. Yahoo developed this, which is used to make websites that are quicker, more elegant, and more responsive. It is a viable alternative to Bootstrap. Pure CSS is designed with k
2 min read
Materialize CSS
Materialize CSS is a design language that combines the classic principles of successful design along with innovation and technology. It is created and designed by Google. Googleâs goal is to develop a system of design that allows for a unified user experience across all its products on any platform.
2 min read
Foundation CSS Tutorial
Foundation CSS is an open-source, responsive front-end framework developed by ZURB in September 2011. Itâs a powerful tool that simplifies the creation of visually appealing responsive websites, apps, and emails that function seamlessly on any device. Renowned companies such as Facebook, eBay, Mozil
6 min read
Semantic UI
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. It uses a class to add CSS to the elements. Semantic UIWhy Semantic UI?Semantic UI is
2 min read
Primer CSS
Primer CSS is a free open-source CSS framework built with the GitHub design system to support the broad spectrum of Github websites. It creates the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure that our patterns are steady and intero
3 min read
Spectre CSS
Spectre CSS is a lightweight, responsive, and modern CSS framework that allows for speedier development and extensibility. It provides the essential elements and typographic stylings, as well as a responsive layout system based on the flexbox feature. It was created by Yan Zhu and first introduced i
4 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side : On client sid
11 min read
JavaScript Libraries
Lodash
Lodash is a JavaScript library that works on the top of underscore.js. It helps in working with arrays, strings, objects, numbers, etc. It provides us with various inbuilt functions and uses a functional programming approach which that coding in JavaScript easier to understand because instead of wri
7 min read
D3.js
D3.js, short for Data-Driven Documents, is a powerful JavaScript library used to create dynamic and interactive data visualizations in web browsers. Developed by Mike Bostock in 2011, it leverages HTML, CSS, and SVG for visualization. In this D3.js tutorial, you'll delve into creating dynamic and in
6 min read
Vue.js Tutorial
Vue.js is a progressive JavaScript framework for building user interfaces. It stands out for its simplicity, seamless integration with other libraries, and reactive data binding.Built on JavaScript for flexible and component-based development.Supports declarative rendering, reactivity, and two-way d
4 min read
Underscore.js
What is Underscore.js? Underscore.js is a lightweight JavaScript library and not a complete framework that was written by Jeremy Ashkenas. It provides utility functions for a variety of use cases in our day-to-day common programming tasks. Underscore.js provides a lot of features that make our task
4 min read
Introduction To TensorFlow.js
TensorFlow.js is the popular library of JavaScript that helps us add machine learning functions to web applications. Tensor is the datatype which is used in the TensorFlow.Now, let us understand the TensorFlow.js and its components.What is TensorFlow.js?TensorFlow.js is the JavaScript library that a
4 min read
Collect.js
Collect.js is the javascript library for collecting data from tree-based structures. This library is used on JavaScript Array and Objects. Collect.js TutorialWhy Collect.js? It is a fluent and convenient wrapper for working with arrays and objects. It provides us with different functions that help i
1 min read
Fabric.js
Fabric.js is an open-source JavaScript canvas library, that provides the interactive object model built on top of the canvas element. It is also an SVG-to-canvas & canvas-to-SVG parser. It was made by Juriy Zaytsev and was first launched in 2010. Object-based drawing is one of the sophisticated
3 min read
p5.js
p5.js is a JavaScript library for creative coding, with attention to making code accessible and inclusive for artists, designers, educators, beginners, and anyone else. It is a free and open-source library i.e. it can be accessible to everyone. In this p5.js tutorial, we will learn the essential kno
2 min read
ReactJS
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable.Applications are built using reusable compon
8 min read
Next.js Tutorial
Next.js is a popular React framework that extends React's capabilities by providing powerful tools for server-side rendering, static site generation, and full-stack development. It is widely used to build SEO-friendly, high-performance web applications easily.Built on React for easy development of f
6 min read
React Material UI
MUI or Material UI is an open-source React Components library that is based on Google's Material Design and provides the predefined UI components for React.Material UI is a UI library that provides predefined react components implementing Google's Material Design. Material UI is a design language bu
5 min read
React Bootstrap Tutorial
React-Bootstrap is a popular front-end framework that combines the power of React with the simplicity of Bootstrap. It provides a modern way to build responsive and mobile-first web applications with prebuilt React components styled using Bootstrap.Easy to use React components for Bootstrap-based de
4 min read
React Suite
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. The input component allows the user to allow the user to create a basic widget for getting the user input in a text field. We can use the following approach in R
2 min read
Ant Design
Ant Design is a React UI development system that empowers designers and developers to build beautiful, modern products with flexibility and ease. It offers a robust design system with a rich set of pre-built components, extensive customisation options, and a focus on efficiency, making it easy to us
4 min read
ReactJS Reactstrap
Reactstrap is a React component library for Bootstrap. Reactstrap is a bootstrap-based React UI library that is used to make good-looking webpages with its seamless and easy-to-use component. Reactstrap does not embed its style, but it depends upon the Bootstrap CSS framework for its styles and them
2 min read
BlueprintJS
BlueprintJS is a React-based UI toolkit for the web. It is written in Typescript. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications that run in a modern web browser. It is optimized for building complex data-dense interfaces for d
2 min read
jQuery
jQuery Tutorial
jQuery is a lightweight JavaScript library that simplifies the HTML DOM manipulating, event handling, and creating dynamic web experiences. The main purpose of jQuery is to simplify the usage of JavaScript on websites. jQuery achieves this by providing concise, single-line methods for complex JavaSc
8 min read
jQuery UI
jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether youâre building highly interactive web applications, or you just need to add a date picker to a form control, jQuery UI is a perfect choice. This JavaScript
3 min read
jQuery Mobile
jQuery Mobile is an HTML5-based user interface system designed to make responsive websites and apps that are accessible on all smartphone, tablet, and desktop devices. Why jQuery Mobile? jQuery Mobile framework takes the âwrite less, do moreâ mantra to the next level. Instead of writing unique appli
2 min read
jQWidgets
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful and optimized framework, platform-independent, and widely supported. It has more than 60 UI widgets that help to create attractive UI designs. Download and Install jQWidgets Downloa
2 min read