Prefer video?
In this brief article a want to give you some reasons why you should stop writing CSS and start writing SASS.
At the end the output is the same a cool and well minified .css file.
The order of the list isn't important!
10 ~ Syntax
When you write SASS code you will see the difference instantly when you see that the code is a lot cleaner without brackets and semicolons, that matters here are tabs.
When you write nested selectors you will have an easier time watch this ποΈ
9 ~ Variables
That thing was really needed in CSS... the use of variables that will help you a lot to manage your code and even for future refactoring.
For example you can create a palette.sass file and store all your colors and then call that variable inside your buttons or divs...
The most beautiful thing in my opinion is that just changing one line of code (where you have defined the color variable) you can change the color of your entire application.
8 ~ Mixins
Really cool stuff, mixins help you to define reusable portions of code, you can think of them like functions, you can pass a parameter and get back you block dynamically written
7 ~ Functions
mmm.. yes mixins and functions are really similar... but, but with functions you can return something with mixins you have only a predefined block of code
6 ~ Imports
Instead of having a super big main.css file with 200000 lines of code split by strange ### signs with titles or worst stuff you could import and export you sass files.
It is not the same thing as the link rel thing cause when you do that you are making an HTTP request :D
You will have a beautiful structure of your code for example with
- header.sass
- menu.sass
- profile_image.sass
- footer.sass
You can even import CSS files!!
5 ~ Maths
With sass you can do pretty any operation you want, that was a big limitation with only css, now no more!
4 ~ Logic
Another thing missing in CSS is the logic, with SASS you have the possibility to use if statements, for while.
That will help you to write less code with more ease.
3 ~ Clean and easy to read code
Maybe i have said it a lot of times but really, with SASS you will see a beautiful code.
Using the combination of mixins imports variables no way that is like css, you code will be really clear to understand.
The only fact that you have different files that contains different parts of you code it will help you when you want to change something in the future.
For example image that you want to change the main color with css... or the font family
2 ~ Faster development
Clean code means fast development, one of the things that IMHO slowed me a lot was semicolons, and another thing... semicolons, i really hated semicolons...
You can use mixins without having to repeat over and over parts of code respecting the dry principle.
1 ~ Production ready
You will get as output a clean css file the way that you want, if you want multiple css files is up to you.
You CSS will be minimized and ready to be imported...
That's it
I hope that you enjoyed this article, it is my first article, i hope that that stuff my help someone and thanks for reading...
Let me know if you want to know how to install SASS with a youtube video or things like that...
If you enjoyed my style check out my new Youtube Channel
Thank you for your time, have a good day ππ
Top comments (43)
CSS has variables though.
Actually, I believe that CSS variables are better than SASS's, for two main reasons:
Personally, I like SASS and I use it all the time, but I've started using native variables for the aforementioned reason.
Here is a small pen that demonstrates how those two can work together and how easy it is to manipulate them with JS (and a more detailed explanation here).
Yeah, I agree they are better than SASS variables. I am also pretty sure that some CSS frameworks are using CSS variables to allow you to customize the theme colors which is a bonus when you don't want to style stuff but you want to dynamically change the theme colors.
Thanks for sharing you knowledge Giorgos, I totally agree, I didn't know about the root thing... and thanks for the article :)
CSS has math too
Yes i knew that comment was coming hahha, but are not the same as SASS one
Sass variables are definitely not the same as CSS variables. Sass is a pre-compiled language; therefore, you never run sass in the browser. Sass compiles your code into regular css. It doesn't use CSS Variables. Here is an example sass file:
and the compiled file:
As you can see, we don't have to use the CSS variables which aren't supported by all browsers.
I don't think writing sass is not writing css. In the end you are using another syntax but all css problems are still here.
The syntax is a matter of taste. Nesting is a very bad idea as you have to mentally rebuild the full selector in your mind.
CSS has custom properties, that are way more powerful than sass static variables.
Math is possible via calc, which works with custom properties and in which you can mix units so it's also more powerful than sass maths functions.
Most of your points are not valid and are easily replaced with native CSS feature. Only logic and function are really useful, but should be used carefuly. Because you can have easy to read sass code that generates garbage css (for example nesting that encourages looooooong selectors that could actually have been a single class)
The title was only a way to improve my "clickbait" skills π οΈ
$color: #000
Is cleaner than
:root {
--epic-var: #000
}
To me at least
There are tools to make it much "cleaner" for creating CSS variables. SwordCSS is an example.
I prefer to have the brackets (curly braces). That to me is cleaner and easier to understand the scope and intent of a given block of css/scss/sass.
Me too and the
{}
are called curly bracesβοΈ
I have used scss for a while but right now i find myself loving sass, i think the thing that i hated the most was without doubt semicolons ποΈ
The thing with Sass syntax is that it's not a valid CSS, while SCSS syntax is a superset of CSS. You can copy and paste any CSS and it'll work immediately in SCSS, without any need of reformatting, removing curly braces, etc... And let's face it, SCSS syntax is more popular than SASS (just look at the most popular libraries using sass)
Imho it's not about popularity, but about how the help me with my work, I've really enjoyed writing PUG code and then copy and pasting it in SASS and that helped me a lot!
I believe that what he means is that CSS variable are not as clean as SASS ones...
As far as i know the don't work in the same way, cause the Sass variable convert for example the color to the hex code... anyway calling $color1 is not like calling var(--color1)... at the end this is only my opinion :)
Lots of amazing reasons! But remember that CSS also has math which is more advanced since it can do calculations between different units. About variables: yes CSS does have variables that are much more powerfull with default values and dynamic characteristics. But this has a performance overhead unlike sass variables that are compiled into normal values at compile time
If you are using a component based framework, you should forget css preprocessors altogether.
Css in Js frameworks are the future. They allow you to use conditions, Js variables, composition, scoping, theming, etc.
If you do REACT take a look at JSS, styled components, emotion, etc.
They still generate css in the end.
Very nice list! Iβm a big fan of SASS. The only danger in SASS is that it can get pretty messy to refactor. If you donβt take on a certain structure like BEM it can be challenging in large codebases.
Nicely done! πͺ
I prefer the SCSS version to SASS. I've noticed other devs prefer it, too. For some reason I find it easier to read. Also, mixins are overrated and hard to manage (the same reason Vue did away with them in js for Vue 3.0).
While I do agree that SASS is a new and trending software that makes CSS easier in some fields, I don't think you should particularly stop learning CSS, as that's the most widely used and is the foundation of SASS itself. I probably copied someone else's comment on accident. If so, sorry about that.
These are good points to get beginner web developers to start learning SASS though!
I absolutely agree the same applies to html if you use pug or js if you use react!