Home >Web Front-end >CSS Tutorial >What's the Difference Between Sass and SCSS?
Sass and SCSS: Which preprocessor to choose?
Core points
This article is an updated version of the article released on April 28, 2014
I have written a lot about Sass, but some of the comments I have received clearly show that not everyone knows exactly what Sass is referring to. Here are some clarifications: When we talk about Sass we usually refer to preprocessors and the entire language. For example, we would say “We are using Sass” or “This is a Sass mixin.” Meanwhile, Sass (preprocessor) allows two different syntaxes:
History of Sass
Initially, Sass was part of another preprocessor called Haml, designed and written by Ruby developers. Therefore, the Sass stylesheet uses Ruby-like syntax, without braces, semicolons, and strict indentation as follows:
<code>// 变量 !primary-color= hotpink // Mixin =border-radius(!radius) -webkit-border-radius= !radius -moz-border-radius= !radius border-radius= !radius .my-element color= !primary-color width= 100% overflow= hidden .my-other-element +border-radius(5px)</code>
As you can see, this is a big difference compared to regular CSS! Even if you are a Sass user, you can see that this is different from what we are used to. The variable symbol is ! instead of $, and the assignment symbol is = instead of :. Very strange. But that's what Sass looked like before the release of version 3.0 in May 2010, which introduced a brand new syntax called SCSS for Sassy CSS. This syntax aims to narrow the gap between Sass and CSS by introducing a user-friendly CSS syntax.
<code>// 变量 $primary-color: hotpink; // Mixin @mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; border-radius: $radius; } .my-element { color: $primary-color; width: 100%; overflow: hidden; } .my-other-element { @include border-radius(5px); }</code>
SCSS is closer to CSS than Sass. That is, Sass maintainers also strive to make the two syntaxes closer to each other by moving ! (variable symbol) and = (assign symbol) from the indent syntax to $ and : in SCSS. Now, when starting a new project, you may want to know which syntax you should use. Let me shed light on the paths and explain the pros and cons of each syntax.
Advantages of Sass Indentation Syntax
While this syntax may seem strange, it has some interesting points. First, it is shorter and easier to enter. No more braces and semicolons are needed, you don't need all of these things. better! @mixin or @include is not required, when a single character is sufficient: = and . Additionally, Sass syntax enforces clean coding standards by relying on indentation. Because the wrong indentation is likely to corrupt the entire .sass stylesheet, it ensures that the code is always clean and well-formed. There is only one way to write Sass code: a good way. But be careful! Indentation in Sass means something . When indenting the selector, this means it is nested in the previous selector. For example:
<code>// 变量 !primary-color= hotpink // Mixin =border-radius(!radius) -webkit-border-radius= !radius -moz-border-radius= !radius border-radius= !radius .my-element color= !primary-color width= 100% overflow= hidden .my-other-element +border-radius(5px)</code>…The following CSS will be output:
<code>// 变量 $primary-color: hotpink; // Mixin @mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; border-radius: $radius; } .my-element { color: $primary-color; width: 100%; overflow: hidden; } .my-other-element { @include border-radius(5px); }</code>The simple fact that pushes .element-b one level to the right means that it is a child of .element-a, thus changing the generated CSS. Please
Be very careful your indentation! By the way, I feel that indentation-based syntax might be more suitable for the Ruby/Python team than the PHP/Java team (although this is arguable, I'd love to hear the opposite).
Advantages of SCSS SyntaxFor beginners, it is fully CSS compliant. This means you can rename the CSS file to .scss and it will work properly. Making SCSS full CSS compatible has been a top priority for Sass maintainers since its release, and in my opinion, it's a big deal. Furthermore, they try to stick to what possible syntax that may become a valid CSS syntax in the future (and therefore the @ directive). Because SCSS is compatible with CSS, this means there is almost no learning curve. The syntax is already known: after all, it's just CSS with a small amount of extra content. This is important when working with inexperienced developers: they will be able to quickly start coding without knowing anything about Sass. Also, it's easier to read because it actually makes sense. When you read @mixin you know it is a mixin declaration; when you see @include you are calling a mixin. It does not use any shortcuts, and everything makes sense when read aloud. Additionally, most existing Sass tools, plugins, and demos are developed using SCSS syntax. Over time, this syntax is becoming more and more important and becomes the default choice, mainly for the above reasons.
Final Thoughts
The option is up to you, but unless you have good reason to encode using indentation syntax, I highly recommend using SCSS instead of Sass. It is not only simpler, but also more convenient. I've tried indentation syntax myself and liked it. I like how short and easy it is. Before I change my mind at the last moment, I'm actually planning to move the entire codebase to Sass at work. I'm grateful for my past self that prevented this move because it would be very difficult to work with some of our tools if we use indentation syntax. Also, note Sass never uses capital letters, whether you are talking about language or grammar. Meanwhile, SCSS always uses capital letters. Need a reminder? SassnotSASS.com can help you!
FAQs about SASS and SCSS
SASS (Synonymous Stylesheets) and SCSS (Sassy CSS) are both preprocessor scripting languages interpreted as cascading stylesheets (CSS). The main difference between the two is their syntax. SASS follows an indentation-based syntax, which means it does not require semicolons or brackets. SCSS, on the other hand, follows a syntax similar to CSS, using brackets to represent code blocks and separating lines within blocks with semicolons.
Yes, you can convert SASS to SCSS and vice versa. There are several online tools available that can help you convert your code from one syntax to another. However, it should be noted that during the conversion process, some functions may not be converted directly due to syntax differences.
It depends heavily on how familiar you are with CSS. If you are already familiar with CSS, you may find SCSS easier to learn because its syntax is very similar. However, if you are not familiar with CSS, you may find SASS' indentation-based syntax simpler and more intuitive.
Technically, you can use SASS and SCSS in the same project. However, it is often recommended to stick to a syntax to ensure consistency and readability. Mixed syntax can cause confusion and make your code harder to maintain.
SASS and SCSS both provide features that are not available in normal CSS, such as variables, nesting, mixin, inheritance, etc. These features can make your stylesheets easier to read and maintain, and also save you time and effort in writing CSS.
Although SCSS is similar to CSS in terms of syntax, they are not the same. SCSS is a preprocessor language that adds powerful features to CSS such as variables, nesting, mixin, and inheritance. These features are not available in normal CSS.
Yes, you need a compiler to convert SASS or SCSS to CSS. For this purpose, there are many tools available, such as Node-sass, Dart-sass, and Ruby-sass. These tools can be integrated into your build process to automatically compile your SASS or SCSS files into CSS.
Yes, you can use SASS or SCSS with any CSS framework. Many popular CSS frameworks, such as Bootstrap and Foundation, even offer SASS or SCSS versions of their stylesheets for easier customization.
A potential drawback of using SASS or SCSS is the need for compilation steps. This may add complexity to the build process and may require additional tools and setup. However, the benefits of using SASS or SCSS, such as improving readability and maintainability, often outweigh this disadvantage.
SASS and SCSS are both widely used, but SCSS seems to be more popular, probably because it is similar to CSS. However, the choice between SASS and SCSS usually depends on personal preferences and the specific needs of the project.
The above is the detailed content of What's the Difference Between Sass and SCSS?. For more information, please follow other related articles on the PHP Chinese website!