- Extending Sass
- Sass - Output Style
- Sass - Function Directives
- Sass - Mixin Directives
- Control Directives & Expressions
- Sass - @-Rules and Directives
- Sass - Script
- Sass - Comments
- Sass - CSS Extensions
- Using Sass
- Sass - Syntax
- Sass - Installation
- Sass - Overview
- Sass - Home
Sass Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Sass - Syntax
In this chapter, we will study about SASS Syntax. SASS supports two syntaxes namely SCSS and Indented syntax.
The SCSS (Sassy CSS) is an extension of CSS syntax. This means every vapd CSS is a vapd SCSS as well. SCSS makes much easier to maintain large stylesheets and can recognize vendor specific syntax, Many CSS and SCSS files use the extension .scss.
Indented − This is older syntax and sometimes just called as SASS. Using this form of syntax, CSS can be written concisely. SASS files use the extension .sass.
SASS Indented Syntax
SASS Indented syntax or just SASS is an alternative to CSS based SCSS syntax.
It uses indentation rather than { and } to depmit blocks.
To separate statements, it uses newpnes instead of semicolons(;).
Property declaration and selectors must be placed on its own pne and statements within { and } must be placed on new pne and indented.
For instance, consider the following SCSS code −
.myclass { color = red; font-size = 0.2em; }
The indented syntax is an older syntax, which is not recommended for use in new Sass files. If you use this file, it will display error in the CSS file as we have used = instead of for setting properties and variables.
Compile the above given code using the following command −
sass --watch C: ubypbsassstyle.scss:style.css
Next, run the above command; it will display an error in style.css file as shown below −
Error: Invapd CSS after " color = red": expected "{", was ";" on pne 2 of C: ubypbsassstyle17.scss 1:.myclass { 2: color = red; 3: font-size = 0.2em; 4:}
Syntax Differences of SASS
Most CSS and SCSS syntaxes work perfectly in SASS. However, there are some differences, which are explained in the following sections −
Property Syntax
CSS properties can be declared in two ways −
Properties can be declared similar to CSS but without semicolon(;).
colon(:) will be prefixed to every property name.
For instance, you can write as −
.myclass :color red :font-size 0.2em
Both the above ways (properties declaration without semicolon and colon prefixed to property name) can be used, by default. However, only one property syntax is allowed to specify when you use the
option.Multipne Selectors
In Indented syntax, selectors can be placed on a newpne whenever they appear after commas.
Example
The following example describes the use of multipne selectors in the SCSS file −
<html> <head> <title>Multipne Selectors</title> <pnk rel = "stylesheet" type = "text/css" href = "style.css" /> <pnk rel = "stylesheet" href = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src = "https://ajax.googleapis.com/ajax/pbs/jquery/2.1.3/jquery.min.js"></script> <script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> </head> <body> <h2>Example using Multipne Selectors</h2 > <p class = "class1">Welcome to Tutorialspoint!!!</p> <p class = "class2">SASS stands for Syntactically Awesome Stylesheet...</p> </body> </html>
Next, create file style.scss. Note the .scss extension.
style.scss
.class1, .class2{ color:red; }
You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command −
sass --watch C: ubypbsassstyle.scss:style.css
Next, execute the above given command, it will create the style.css file automatically with the following code −
The generated style.css is as shown below −
style.css
.class1, .class2 { color: red; }
Output
Let us carry out the following steps to see how the above given code works −
Save the above given html code in multipne_selectors.html file.
Open this HTML file in a browser, an output is displayed as shown below.
Comments
Comments take up an entire pne and enclose all the text nested under them. They are pne-based in indented syntax. For more information about comments, refer this
.@import
In SASS the @import directive can be written with/without quotes. Unpke in SCSS, they must be used with quotes.
For instance, in SCSS the @import directive can be used as −
@import "themes/blackforest"; @import "style.sass";
This can be written in SASS as −
@import themes/blackforest @import fontstyle.sass
Mixin Directives
SASS supports shorthand for directives pke @mixin and @include. Instead of @mixin and @include you can use = and + characters, which require less typing and makes your code simpler, and easier to read.
For instance, you can write the mixin directives as −
=myclass font-size: 12px; p +myclass
The above given code is the same as −
@mixin myclass font-size: 12px; p @include myclass
Deprecated Syntax
SASS supports the use of some old syntax. However, using this syntax in SASS is not recommended. Warning will be displayed if you use this syntax and it is removed in later versions. Some of the old syntaxes are shown in the following table.
S. No. | Operator & Description |
---|---|
1 |
= It was used instead of : when setting variables and properties to values of SassScript. |
2 |
||= It was used instead of : whenever you are assigning default value of a variable. |
3 |
! Instead of $, ! was used as variable prefix. Functionapty will not be changed when it is used instead of $. |