Concept of Nesting & Variables in SASS
Tags: sass, css, scss
Published: 2019-08-13 12:42:27

---
title: Concept of Nesting & Variables in SASS
published: true 
description: A basic intro to sass nesting and the use of variables in SASS
tags: sass, css, scss
series: Learning SASS
cover_image: https://thepracticaldev.s3.amazonaws.com/i/dz1b4xx7pnz32fsf6bod.jpeg
---


First of let's ask ourselves what is SASS?
SASS stands for Syntactically Awesome Style Sheets, it is an extension to CSS. It comes with lots of exciting features such as variables, nesting, mixins, inline imports, built-in functions to manipulate color and other values amongst others. For the purpose of this post we will be focusing on NESTING and VARIABLES.

It also important to note to that you cannot directly use SASS on the browser since browsers are not designed to interpret SASS. To make the browser understand SASS, you need to use the SASS pre-processor to translate the SASS code into standard CSS. 
You can read more about how this works on the SASS official [website!](https://sass-lang.com/install)

## What is Nesting?

Away from the scope of this article, you may have definitely heard about the term before and in plain english it is allowing items of similar characteristics to be able to be placed or stored one inside the other.
That's pretty much it.
In SASS the concept isn't so different either. **_Nesting_** is the process of placing selectors inside the scope of another selector (_CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule set. CSS selectors select HTML elements according to its id, class, type, attribute etc._).

Usually in CSS if you want to style a parent element as well as it's child you would have something like

![css demo](https://thepracticaldev.s3.amazonaws.com/i/by3mojzpzoa4f6fsbbip.png)

But with SASS this is simplified and goes a long way in eliminating repetition in your CSS. Using the concept of nesting, the above would look a bit different and frankly a bit more exciting. It would more like

![sass](https://thepracticaldev.s3.amazonaws.com/i/k2rwjc5ge93uvu1hx6h7.png)

Of course the browser wouldn't understand this but with the help of the SASS pre-processor, this can be transpiled (the process of translating from SASS to CSS) to look like your regular standard CSS so the browser understands it better. The above transpiles to

![css demo](https://thepracticaldev.s3.amazonaws.com/i/by3mojzpzoa4f6fsbbip.png).


Moving on, it is also important to note that there's no limit to how much can be nested. What does this mean? It simply means you can nest as many times as you need to. Take a look at the image 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/w7ac8910wbbn072jt95r.png)

From the image above you can see that the _.child_ is nested inside the _.parent_ and the _.a-child-nested-in-a-child_ is nested in the _.child_ and the _.another-child-nested_ is nested in the _.a-child-nested-in-a-child_ and so on. 
The above will be transpile to your standard CSS and look a bit more like 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/e6fwa5zob80eqkzkn3ai.png)

With the help of SASS we have been able to eliminate having to write repetitive code and we have successfully been able to focus more on what we are working on rather spend time creating a structure in our CSS for nested elements in our HTML.

**Can we only nest selectors?**

Well, no! We can nest properties as well... Yes! CSS properties can be nested using SASS.
Take a look at the image below

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/3j4q56lc51tsyv3k0lxp.png)

We have been able to simplify our CSS by nesting our properties.
The above will transpile to look like

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0kh2ag5d9zzfr5g1xkjd.png)

## What are variables?

This refer to an element, feature, or factor that is liable to vary or change. Variables are one of the most popular and widely used feature in programming. As a matter fact I doubt there is any programming language that doesn't support the use of variables.
Variables can be seen as placeholders or containers that store or hold a certain piece of data.
In SASS variables are use to store reusable value for properties across your style sheet. Although this feature isn't unique to SASS (as it was recently added to standard CSS), the method of declaration and use still vary. 
Variables in SASS allow you to assign an identifier of your choice to a specific value. Usually this achieved using the dollar sign ($) followed by the name of your identifier, that is _$my_unique_identifier_ .

Let's assume that you want to define a specific box-shadow value that you need to reuse across your stylesheet or a specific color code that will be used multiple times in your stylesheet , normally you would have to rewrite it every where you need to use it, but with the use of variables, you only need to declare it once and make reference to it with its unique identifier.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/76f6jijpplgl9j0kftd6.png)

This will transpile to 

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/xi2srxapaoreffjnlmph.png)



## Summary

SASS makes writing CSS more structured with all of its many features and it creates a really fun and stress free experience that allows you focus on just what you are working on.