Table of contents
1.
Introduction
2.
What are Sass Scripts?
3.
Sass Expressions 
3.1.
Interactive Shell
3.2.
Variables
3.3.
Data Types
3.4.
Operations
3.5.
Parentheses
3.6.
Functions
3.7.
Interpolation
3.8.
& in SassScript
3.9.
Variable Defaults
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Sass Scripts

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

SASS (Syntactically Awesome Style Sheets) is an interpreter/compiler language compiled into CSS (Cascading Style Sheets). SASS Script constructs CSS. With Sass, you can write CSS that's more compact and easier to read.

What are Sass Scripts?

The Sass transpiler is capable of much more. The language isn't a pretty full-fledged programming language. It lacks some of the constructs we expect to find in modern languages. Nevertheless, it provides valuable functions such as arithmetic operators, the flow of control directives, and some convenient functions.

Using Sass expressions, Sass Script calculates a CSS selector, property, or value.  In this blog, we'll look at sass expressions.

Sass Expressions 

Sass uses the following CSS extensions:

Interactive Shell

It's convenient to run expressions without transpiring when you're using Sass Script. You can work quickly with Sass Script by using the interactive shell with the SASS command line along with the i option.

$ sass -i

Variables

Variables are used in programming to represent data, such as characters, numeric values, or memory addresses. Variables are useful because you can reuse the values they store throughout the stylesheet.

$variable_name : some value;

Data Types

Data Type is a type of information that requires declaring data type for every data object. 

$Variable_name: "text";

tagname.#{$variable_name} {
  Data_type: value;
}

Operations

There are some operations provided by SASS, such as number operations, color operations, string operations, Boolean operations, and list operations. 

$variable_name: value;

tag_name {
   font-size: $size (operator) 5;
}

Parentheses

In mathematical notation, parentheses represent a symbolic logic that affects the order of operations. They are usually delimited by round brackets ( ) or square brackets [ ], depending on the type of sign used.

tag_name {
   font-size:  12px + [2px * 4];
}

Functions

SASS supports functions by providing some keyword arguments specified using regular CSS function syntax.

tag_name {
   data_type: rgb(255, 255, 255); 
}

Interpolation

It provides SassScript variables in selectors and property names using #{ } syntax. You can specify variables or property names within the curly braces.

#{$variable_name}

& in SassScript

It allows the nesting of properties into another property, leading to a group of other related codes.

Variable Defaults

A default value for a variable is set by adding a !default flag to the end of the variable value. If the value has already been assigned to the variable, it will not re-assign it.

$variable1: null;
$variable1: "SassScripts" !default;

p:after {
   content: $variable1;
}

FAQs

Q1. What language is SASS coded in?

Ans: Originally written in Ruby, Sass has been ported to many languages, including C, C++, Java, JavaScript, Net, Perl, PHP, Python, and Scala.

 

Q2. Is SASS better than CSS?

Ans: In a programming construct, Sass allows you to write clean, easy, and less CSS. With less codes, CSS can be written more quickly. Because CSS is extended, it is more stable, robust, and elegant. As a result, designers and developers are able to work faster and more efficiently.

 

Q3. How many syntaxes that SASS support?

Ans: Sass supports two different syntaxes which are sass and scss. Each one can load the other, so it's up to you and your team which one to choose.

  • The sass syntax removes semicolon and curly brackets and adds a colon before every property tag.
  • The scss syntax is the same as a CSS syntax with its own modification.

 

Let’s see how we can convert the SASS file and SCSS file.

SASS file to the SCSS file:

sass-convert filename.sass filename.scss

 

SCSS file to the SASS file:

sass-convert filename.scss filename.sass

 

Q4. Is it possible to mix CSS and SCSS?

Ans: It is possible to import sass and CSS stylesheets into one file using SASS import. It is more difficult to manage code when a lot of stylesheet files are used. Using Sass @import, we can break files down into smaller parts and import them later.

 

Q5. The difference between SASS comments and normal CSS comments.

Ans: A CSS comment begins with '/**/', whereas SASS has two commands. There is a single-line comment beginning with // and a multiple-line comment beginning with '/**/'.

Key Takeaways

As we have seen, Sass is different from CSS as you can write dynamic code that is compiled into CSS, making it more like a scripting language. Furthermore, we have also discussed Sass Expressions along with examples.

Check out this article - Balanced Parentheses

If you are new to Web development and interested in learning about other areas as well. Then, you can follow our guided path, which is absolutely free. 

Live masterclass