Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS Advanced Sass Advanced Variables, Mixins, Functions, and Placeholders Scoping and !default Variables

Alice Mello
Alice Mello
1,591 Points

Global and Scoped variables

Hello, In the example you gave, why then not just leave the exception with the normal CSS declaration (color: red) instead of using the variable; I mean, why use a variable "$text-color" with a diferent color?

3 Answers

Iulia Maria Lungu
Iulia Maria Lungu
16,935 Points

It should be noted that in Sass 3.4.21 (Selective Steve), this scoping works like JavaScript, exactly as the deprecation warning shown in the terminal in this video.

This :

$text-color : blue;

h1 { $text-color: red; 
    color: $text-color;

}

h2 { color: $text-color; }

compiles to this:

h1 { color: red; }

h2 { color: blue; }

so $text-color remains in the global scope where it was first defined

idan ben yair
idan ben yair
10,288 Points

The reason you would want to do that is because if you want to use that same color in other places in your code you wouldnt have to memorize the hex value you would just use $whichever-color everytime. plus if you work in a team its easier as well. Sass is really useful you should look in to mixins as well you can put a bunch of values in one mixin and then implement it in to your code. For example:

@mixin text-style {

      color: red;
      font-size: 16px;
      background-color: blue;

}


/* use in css */

P {

     @include text-style;

}

Now you dont have to worry about memorizing or copy pasting pieces of code you already wrote. Let me know if that helped you. :)

Anthony Attard
Anthony Attard
43,915 Points

The purpose of that was purely as an example of how variables scope in Sass. This would not be used in real projects.