Deploy SCSS Variables & Save Everyday from Today🔥
Tags: css, webdev, tutorial, codenewbie
Published: 2020-11-16 16:18:06

**One** of the main task of programming is to find out a shortcut to solve **"Repetition"**. While writing code in CSS, we tend to repeat a lot of code, making our code large, hard to read & change. Out of All the solutions SCSS/SASS provides, one of them is Variables.


🔥Let's look at 4 tricks to Solve code repetition using SCSS/SASS variables.

## Table Of Contents 🔥
   * [Media Queries](#chapter-1)
   * [Responsive Design](#chapter-2)
   * [Global Scope](#chapter-3)
   * [Local Scope](#chapter-4)

<!--
*[SCSS Variables in JavaScript](#chapter-5)
-->



#**1. Media Queries :** <a name="chapter-1"></a>

Responsive Website, without Media Queries is a Joke! Let's say we have 4 block level, and 10 element level media queries(I follow BEM method). While changing the values of our media queries we have to manually read the whole thing. 
But, if we deploy SCSS/SASS variables, then we only need to look at the top of our page. ***Thank me later.

###***❌Instead Of Doing This❌***

```scss
//*******  Avoid this system  *******

@media (max-width: 1024px){
  // Contents Here
}
@media (max-width: 768px){
  // Contents Here
}
@media (max-width: 480px){
  // Contents Here
}

```

###***✔️Do This✔️***

```scss
//*******  Follow this system  *******

$laptop : 1024px;
$tablet : 768px;
$mobile : 480px;

@media (max-width: $laptop){
  // Contents Here
}
@media (max-width: $tablet){
  // Contents Here
}
@media (max-width: $mobile){
  // Contents Here
}

```



#**2. Responsive Design :** <a name="chapter-2"></a>

Designers pick 4-6 common numbers to set font size, padding, margin, etc.

Let's say, A Designer has picked 22px for heading, 16px for font size(general), padding between 2 elements of 32px, common margin of 10px, etc.

A Developer has to follow these style guides. While coding, he has to set these values in multiple places. It becomes a nightmare when there's a change in padding from 32px to 20px, and the developer has to read the whole file and change it again.

So, deploy SCSS/SASS variables and next time, look for the values at the top of the page.


###***❌Instead Of Doing This❌***

```scss
//*******  Avoid this system  *******

.container{
  color: black;
  font-size: 18px;
  padding :32px;
  margin:10px;
}


```

###***✔️Do This✔️***

```scss
//*******  Follow this system  *******

$color-1: black;
$fs : 18px;
$padding : 32px;
$margin : 10px;

.container{
  color: $color-1;
  font-size: $fs;
  padding :$padding;
  margin: $margin;
}

```

###Quick Tip 💪

You can perform **Calculations on the go** using SCSS/SASS variables, like this......

```SCSS

$x : 50px;

.div-1{
  width: ($x+$x);     // (50+50) = 100px;
}
.div-2{
  width: ($x*10)/2;   // (50*10)/2 = 250px;
}

```




#**3. Global Scope(General Purpose):** <a name="chapter-3"></a>

Variables once set, at the top of your page, you can use it anywhere as you please. Sample 👇

```SCSS 

$color-1 : white;
$color-2 : blue;
$color-3 : black;
$fs: 18px;
$padding: 20px;


.container{
  background-color: $color-3;
  font-size :$fs; 
}

```



#**4. Local Scope(Temporary Use):** <a name="chapter-4"></a>

If you want to set a value for a temporary use, then it's possible to create local scoped variables for temporary use.
**P.S : for this to work, set variables inside code blocks.
Sample 👇

```SCSS

//These are Global Scoped Variables

$color-A : white;
$color-B : black;


.div-1{

// This is Local Scoped Variable

$color-1 : green;

  background-color : $color-1;

}


.div-2{


//**** This will not work  ****
  background-color : $color-1;


//*** This will Work ****
  color : $color-A;

}


```


<!--
#**5. SCSS Variables in JavaScript:** <a name="chapter-5"></a>
-->

#**Conclusion :**

So, we're Done with the Tutorial Ladies & Gentlemen. Let me know your thoughts on the comment section.

###**Suggestions & Criticisms are Highly Appreciated ❤️**




   * **Youtube[ / Joy Shaheb](youtube.com/channel/UCHG7IJuST_BXJkne-0u0Xtw)**

   * **Twitter[ / JoyShaheb](https://twitter.com/JoyShaheb)**

   * **Instagram[ / JoyShaheb](https://www.instagram.com/joyshaheb/)**