DEV Community

Cover image for STOP writing CSS, 10 reasons why
Aspiiire
Aspiiire

Posted on • Edited on • Originally published at priiimo.com

STOP writing CSS, 10 reasons why

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.

Syntax of css compared with sass

When you write nested selectors you will have an easier time watch this 😁️

Clean parent to selector sass compared to css

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.

Take a look here πŸ‘‡
Variables in sass compared to css

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

Mixins in sass

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!!

The use of imports in sass

5 ~ Maths

With sass you can do pretty any operation you want, that was a big limitation with only css, now no more!

Math operations with css

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.

Example of is statement with sass

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)

Collapse
 
shadowtime2000 profile image
shadowtime2000

CSS has variables though.

Collapse
 
gsarig profile image
Giorgos Sarigiannidis • Edited

Actually, I believe that CSS variables are better than SASS's, for two main reasons:

  1. They can be modified from within media queries. For example, you write some css which use a specific value stored in a variable and then, on the media query, you don't need to repeat the same code if all you need is a few calculations based on a different value. You can just declare the variable again, with the new value.
  2. They can be manipulated with JavaScript, which is a big thing.

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).

Collapse
 
shadowtime2000 profile image
shadowtime2000

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.

Collapse
 
aspiiire profile image
Aspiiire

Thanks for sharing you knowledge Giorgos, I totally agree, I didn't know about the root thing... and thanks for the article :)

Collapse
 
denvercoder1 profile image
Jonah Lawrence

CSS has math too

height: calc(100vh - 50px);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
aspiiire profile image
Aspiiire

Yes i knew that comment was coming hahha, but are not the same as SASS one

Collapse
 
sno2 profile image
Carter Snook

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:

$myColor: #fff;
html
  color: $myColor
Enter fullscreen mode Exit fullscreen mode

and the compiled file:

html {
  color: #fff;
}
Enter fullscreen mode Exit fullscreen mode

As you can see, we don't have to use the CSS variables which aren't supported by all browsers.

Collapse
 
drazik profile image
drazik • Edited

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)

Collapse
 
aspiiire profile image
Aspiiire

The title was only a way to improve my "clickbait" skills πŸ˜…οΈ

 
ucvdesh profile image
Daniel Silva

$color: #000

Is cleaner than

:root {
--epic-var: #000
}

To me at least

Thread Thread
 
shadowtime2000 profile image
shadowtime2000

There are tools to make it much "cleaner" for creating CSS variables. SwordCSS is an example.

Collapse
 
mmcshinsky profile image
Michael McShinsky • Edited

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.

Collapse
 
sno2 profile image
Carter Snook

Me too and the {} are called curly braces

Collapse
 
mmcshinsky profile image
Michael McShinsky

☝️

Collapse
 
aspiiire profile image
Aspiiire

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 πŸ˜‚οΈ

Collapse
 
piotrlewandowski profile image
Piotr Lewandowski • Edited

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)

Thread Thread
 
aspiiire profile image
Aspiiire

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!

 
ucvdesh profile image
Daniel Silva • Edited

I believe that what he means is that CSS variable are not as clean as SASS ones...

Thread Thread
 
aspiiire profile image
Aspiiire

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 :)

Collapse
 
promikecoder2020 profile image
ProMikeCoder2020

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

Collapse
 
ecyrbe profile image
ecyrbe

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.

Collapse
 
joachimzeelmaekers profile image
Joachim Zeelmaekers

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! πŸ’ͺ

Collapse
 
synapse709 profile image
Tim Titus • Edited

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).

Collapse
 
trollboy_j profile image
Jacko

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!

Collapse
 
aspiiire profile image
Aspiiire

I absolutely agree the same applies to html if you use pug or js if you use react!