How does Sass work?
Knowing how Sass works is a matter of understanding several basic technological concepts:
- Sass can be based on two different technologies: Ruby or LibSass. In this book, we're going to use Sass based on Ruby.
- Sass is a Ruby gem. Gems are packages for use in Ruby. A Ruby gem is a software that runs only on Ruby. Ruby is a programming language, just like PHP, .NET, Java, and so on.
- We can make Sass run via the command line, but we can also run Sass with a third-party application, making the use of the command line unnecessary.
- Sass is a programming/scripting language used to create CSS.
- CSS is a very repetitive language. Sass allows authors to optimize those repetitive tasks and create CSS faster and more efficiently.
- Part of the Sass workflow is when Sass is watching an SCSS file, for example,
book-styles.scss
. When it detects a change in that SCSS file, it then compiles it into a CSS filebook-styles.css
.
Tip
Watching an SCSS file means that the Sass watcher is running in the background looking over the SCSS file(s) for any changes.
Installing Sass
Here are the steps we're going to follow:
- Download the Ruby installer
- Open the command line
- Install the Sass gem
Downloading the Ruby installer
Windows: Download the Ruby installer from the following link:
http://rubyinstaller.org/downloads/
Mac: Ruby comes preinstalled on all Macs, so there's no need to download anything.
Opening the command line
Windows and Mac: Open the command line.
Tip
Windows Tip!
Press Windows + R, type CMD
, and then press Enter.
Installing the Sass gem
Type the following command into the command prompt (it doesn't matter which folder you're in):
Windows, use the following command:
gem install sass
Mac, use the following command:
sudo gem install sass
It'll take a few seconds to install Sass.
Tip
At the time of writing, the latest version of Sass was 3.4.14. The version/revisions might be different by the time the book comes out.
That's it! Sass is now installed on your machine.
Using Sass
What I'm about to show you is completely different to what any other Sass tutorial out there tells you to do. Most of those tutorials complicate things too much. This is the simplest way to use Sass you'll ever read.
The following screenshots are on Windows, but the process can be applied exactly the same regardless of platform.
In the following steps, you will see examples of how the necessary folders and files look after being created, not how to create them:
- Create a
/Demo
folder anywhere on your drive: - Inside that folder, create two subfolders,
/css
and/scss
: - Create a
.scss
file. Go into the/scss
folder and create a file calledstyles.scss
:Tip
Notice the file extension
.scss
? This is your Sass file. Yes, right now there's nothing in it, it's empty. - Go back to the command line for a minute and follow these steps:
- In the command line, type
cd <space>
- A space after
cd
means Change Directory. From your file manager, drag and drop the/Demo
folder into the command prompt/terminal window and press Enter. - You should be in the
/Demo
folder now.
- In the command line, type
- Make Sass watch your
/scss
and/css
folders by typing this in the command line:sass --watch scss:css
- Make Sass watch the
/scss
and/css
folders.That's it! You are now using Sass!
Tip
The
--watch
flag tells Sass to pay attention to the/scss
and/css
folders so that when we make a change to a.scss
file (in our case,styles.scss
), Sass will detect the change and compile the SCSS into the final CSS file we're going to use in our website or app. - Edit the
.scss
file and watch Sass compile it into a.css
file:- Open your text editor (I use Sublime Text).
- Open the
styles.scss
file. - Add some CSS to it.
- Save the
styles.scss
file. - From your command line/terminal, verify that the compiling was successful.
- Open your
styles.css
file and enjoy your new creation.