Speed Up Your Web Development: Sass
Tags: webdev, productivity, css, opensource
Published: 2018-09-06 22:30:03

---
title: Speed Up Your Web Development: Sass
published: true
description: Learn how to improve your CSS code, making it more maintainable and easier to read and write in a blaze with Sass.
tags: webdev, productivity, css, opensource
canonical_url: https://blog.kzsoftworks.com/speed-up-your-web-development-sass/
cover_image: https://blog.kzsoftworks.com/content/images/2018/09/cabecera-sass.png
---

This is the first of a series of posts that aim to make your life easier and enhance your productivity while developing websites, by giving you useful and industry proven tools and tips. 
___

The scenario is simple, you're developing a website or web app, and for sure you want it to look as good as you can, which is obviously accomplished by using CSS. Then you find yourself repeating tag names, properties, colors, and having a gigantic main.css file (or maybe a lot of  `<src rel="stylesheet">` tags in your HTML header?). And what if you want to change something as the main color of your website? Dozens of lines changed just because `#7a19a8` seemed slightly better than `#7211a0`.

![Imgur](https://i.imgur.com/6o46QvA.png)
*Is there even a difference?*

Well, to solve all of this and more, in 2006 the Syntactically awesome style sheets were created, Sass for the friends. Sass is a preprocessor scripting language that is interpreted or compiled into simple CSS. What all of this means is that Sass extends the CSS syntax, using a similar structure, with a lot more fuctionality.

###Usage

All installation steps are covered in [Sass' official install guide](https://sass-lang.com/install). In this post, I'm going to show you some basic syntax so you can make your CSS better since day one.

####*Variables*
Variables are a core part of Sass, the premise is that you declare any data in the form of a [valid data type](https://www.sitepoint.com/data-types-in-sass/) in Sass, and you can use it all along your stylesheet. Here is the syntax:

![Imgur](https://i.imgur.com/Ul2b42X.png)


####*Nested Selectors*

With Sass Selector nesting we can transform this:

```scss
div {
  // Some properties for a div
} 
div span {
  // Some properties for a span tag inside a div
}
div p {
  // Some properties for a p tag inside a div
}
```

Into: 
```scss
div {
  // Some div properties
    
    span {
      // Some properties of a span tag inside a div     
    }
    
    p {
      // Some properties of a p tag inside a div
    }
}
``` 
Basically, you can nest selectors within selectors to avoid repeating tags each time you may need to go deeper in order to win specificity.

You can also add pseudo elements and modify parent selectors inside themselves with the **&** symbol, this way:

```scss
div {
  // div properties
  &.steve {
    // properties of a div with steve class
  }
}
```

####*Mixins*
A mixin is a Sass feature that allows you to define reusable pieces of code, and even allows parameters, just as functions, in order to modify its behavior without having to draw upon semantic classes as `.margin-box-size-1`. Let's see them:

```scss
@mixin box-size($size: 1em) {
    margin: $size;
    padding: $size;
}
```

You define a mixin by using `@mixin`, then you name it, in this case is `box-size` and write down the parameters it will receive, if any. In this case we also have a default parameter `$size: 1em`. The variables will be replaced with their value at build time, and everything inside the mixin will be placed wherever is used across the stylesheet. You call the mixin function by using **@include** followed by the mixin name.

```scss

div {
    @include box-size(3em);
}

// Will be replaced by

div {
    margin: 3em;
    padding: 3em;
}
```


####*Control Directives*
With Sass we can bring common programming sentences into stylesheets, with control directives as `@if`, `@for`, `@each` and `@while`, we can sort out, repeat and take from a map or list any piece of code. Lets take a brief look over each syntax.
#### `@if`
The `@if` directive takes any [SassScript](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#sassscript) truthy or false expression and executes whichever code sections applies:

```scss
@if (true) {
    // This code will always run
} @else {
    // this code will never run
}
```
#### `@for`
The `@for` directive is pretty straight forward, it runs the code inside the braces for a determined amount of times. The syntax is as follows:

```scss
// We declare a variable $i for the iterator, which starts at [from] and ends in [trough] values
@for $i from 1 through 6 {
    margin: $i+px;
}
```
#### `@while`
The `@while` directive works pretty much as the `@for` directive, with the difference that the condition of the loop to execute is entirely on us, and not only an incremental operator.

```scss
$execute: true;

@while ($execute) {
    @debug($execute);
}
```
#### Tip
`@debug()` will print out any value to the console while building, as its name says, its awesome for debugging!


#### `@each`
Last but not least, `@each` works slightly different from the other directives, as it goes trough a collection such a map or a list instead of simply a condition, and exposes the current value for you to use.

```scss
$colors: red green blue;

@each $color in $colors {
    background: $color
}
```
____

And that's it! With this easy tool you can start to write cleaner and more maintainable CSS from now on.

If you have any doubts you can find me on twitter as [@stiv_ml](https://twitter.com/stiv_ml). I will be more than pleased to answer any question!

This post was originally published in [Kaizen Softworks Blog](https://blog.kzsoftworks.com/), that also happens to be where I work at! If you want to check more of my content you can click [here](https://blog.kzsoftworks.com/author/pablo/).