Using the Sass interactive mode and SassScript
In the Using Sass on the command line recipe of this chapter, you had to create a Sass sample as an input for the Sass compiler. In this recipe, you will be introduced to the interactive Sass mode and SassScript. SassScript is a small set of extensions of the plain CSS property syntax. You can use SassScript to set property values using variables, arithmetic, and extra functions. The interactive mode enables you to enter and test your SassScript code directly in the console. SassScript can also be used to generate selectors and property names via string interpolation. You can read more about interpolation in the Interpolation of variables recipe of Chapter 3, Variables, Mixins, and Functions.
Getting ready
The interactive mode of Sass comes with the Sass gem. You can use it after installing Sass. In the Installing Sass for command line usage recipe of this chapter, you can read how to install Sass.
How to do it...
Take a look at the following steps to understand how to use the Sass interactive mode:
- You can start the interactive shell by running the following in your console:
sass -interactive
- Now, you can enter the following code into the shell:
>> 1px + 1px
- The preceding command will give the following output:
2px
- Also, notice that you can only enter a valid code. Consider the following code:
>> 1px + 2em
- Because Sass cannot sum different units, the preceding code will generate the following error:
SyntaxError: Incompatible units: 'em' and 'px'.
- Also, Sass built-in function can be evaluated. Enter the following code into the shell:
change-color(#ff0000, $alpha: 0.5)
- You will find that this returns the following output:
rgba(255, 0, 0, 0.5)
How it works...
As it has already been made clear in the introduction of this recipe, you can use the interactive shell to test the property values set by SassScript. The interactive shell does not allow you to use variables or mixins, but you can make use of Sass's built-in functions. Chapter 5, Built-in Functions, of this book will introduce you to the useful built-in functions of Sass. The function used in the fourth step of this recipe is an example of a built-in function.
To quit once you have entered the interactive mode, just type Ctrl + D.
There's more...
The Compass gem has an interactive shell that is available too. You can start Compass's interactive shell by running the following command in your console:
compass interactive
In the interactive shell, you can run SassScript and you can also test the results of the Compass helper functions. Try the following command to evaluate the append-selector()
helper function:
>> append-selector("p, div, span", ".bar")
The preceding command will give the following output:
"p.bar, div.bar, span.bar"
In your Sass code, you should use the preceding result inside an interpolation escape: #{ }
.