Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Sass supports two different file extensions. These are .sass(Indented syntax) and .scss(SCSS syntax), one is for syntactically awesome style sheet code, and the other is for Sassy CSS syntax. As we have mentioned in our introduction blog, Sass became Scss from version III, which has a very similar syntax to CSS compared to Sass, which has a different syntax.
If we have some code written as per syntactically awesome style sheets syntax and you want to convert it into Sassy CSS syntax, we can use the sass-convert command.
Let's take an example if we have Sass code in sass-example.sass file and we have scss-example.scss for Scss code, we can convert the Sass code to Scss code using the following command,
sass-convert sass-example.sass scss-example.scss
The Indented Syntax
The indented syntax is sass’s original syntax, so it uses the file extension .sass. Using this syntax, CSS can be written concisely. Indented syntax supports all the same features, same as Sassy CSS, it uses indentation instead of curly braces and semicolons to describe the document's format.
In general, you can indent one level deeper in the indented syntax any time you’d write curly braces in CSS or SCSS. And any time a line ends, that counts as a semicolon.
The syntax for Scss is similar to Cascading Style Sheet with some changes in the syntax, which further simplifies writing the code to style complex HTML structures. It's a superset of CSS, which essentially means all valid CSS is valid SCSS as well. Scss also provides us with features like operators usage, variables, many ready-made functions, etc.
This blog will focus on some important syntax changes introduced in Scss that were not available in CSS.
Comments in SCSS
The Scss supports both Single line comments // and Multiline comments */. But once the Scss or Sass file is compiled into CSS, the resulting Cascading Style Sheet will only preserve the multiline comments, not the single-line comments, because CSS doesn't support single-line comments.
In HTML, we have nested and visual hierarchies. Let's take an example, if we create a list, we use the <li> tag to create list items inside the <ol> or <ul> tag. But Cascading Style Sheets does not support such nested syntax.
In syntactically awesome style sheets syntax, we can use & symbol to represent the parent class and define further nested rules. This & symbol makes it possible to re-use the outer selector in more complex ways, such as adding a selector before the parent or adding a pseudo-class.
When we have to use pseudo-selectors like : selected, :hover etc, with the main element, this will feel handy.
a {
text-decoration: none;
color:black;
// using parent selector
&:hover {
color:red;
}
}
Output:
Nested property
Sass provides one more CSS extension to save you time and make your code more readable using nested properties.
While in CSS, There are many properties with the same prefix as font-weight, font-size, font-family, or text-align, text-transform and text-overflow, etc. We need to type the complete property name to style our HTML elements. But syntactically awesome style sheets syntax allows us to define nested properties with the prefix declared only once like this:
Sass has a unique kind of selector known as a “placeholder”.
Placeholder selector looks and acts like a class selector, but it starts with a %, and it's not included in the Cascading Style Sheet output. Any complex selector (the ones between the commas) that even contains a placeholder selector isn't included in the Cascading Style Sheet, nor is any style rule whose selectors all contain placeholders.
What is the difference between CSS and LESS? One of the best advantages of LESS is that it simplifies the management of a large volume of CSS styles and makes CSS more flexible. Moreover, this stylesheet language adds multiple dynamic features to CSS; it introduces nesting, variables, operators, mixins, and functions.
How do we nest in Sass? Nesting styles is simple enough. You can just enclose a selector inside the curly braces of another selector. Nesting can extend as many levels long as you wish. This means that you can nest elements inside of a component that is nested inside another element.
What are the differences between Leaner Style Sheets and syntactically awesome style sheets?
S.no
Leaner Style Sheet
Syntactically awesome style sheet
1
Leaner Style Sheets uses JavaScript and are processed at the client side.
Syntactically awesome style sheets is coded in Ruby and thus processed to server-side.
2
Leaner Style Sheets does not inherit multiple selectors with one set of properties.
Syntactically awesome style sheets inherit multiple selectors with one set of properties.
4. What are Sass, Stylus and Less? Why do we use them? How is Compass related to Sass?
They are Cascading Style Sheets preprocessors. They are a special syntax that compiles down into CSS. They make managing Cascading Style Sheets easier, with things like variables and mixins to handle vendor prefixes. They make doing best practices easier, like compressing and concatenating CSS.
Key takeaways
In this blog, we have discussed syntactically awesome style sheets Syntax and some significant syntax changes introduced in Scss.
If you are pursuing a new career in Web Development, we suggest you get your fundamentals crystal clear with ourFull Stack Development course. Check out this problem - Redundant Braces