-
Notifications
You must be signed in to change notification settings - Fork 28
Installing Sass
Sass is a meta-language on top of CSS that's used to describe the style of a document cleanly and structurally, with more power than flat CSS allows. Sass both provides a simpler, more elegant syntax for CSS and implements various features that are useful for creating manageable style sheets. sass-lang.com
To paraphrase what the authors of Sass have told us, Sass is a lovely CSS pre-processor that allows for a more dynamic approach to writing style sheets. Sass incorporates basic elements from it's parent language Ruby. Sass offers things like variables, functions, control structures, and mixins. Mixins are similar to functions as we know them except they "return" properties, values, and sometimes entire selectors.
Let's see some Sass!
// using variables
$blue: #3bbfce;
$margin: 16px;
.content-navigation {
border-color: $blue;
color:
darken($blue, 9%);
}
.border {
padding: $margin / 2;
margin: $margin / 2;
border-color: $blue;
}
// nesting to mimic DOM
table.hl {
margin: 2em 0;
td.ln { text-align: right; }
}
li {
font: {
family: serif;
weight: bold;
size: 1.2em;
}
}
// mixins
@mixin table-base {
th {
text-align: center;
font-weight: bold;
}
td, th { padding: 2px; }
}
@mixin left($dist) {
float: left;
margin-left: $dist;
}
#data {
@include left(10px);
@include table-base;
}
To learn more about Sass, see the links below:
- Sass Tutorial on sass-lang.com
- Sass Tutorials on thesassway.com
- Sass Tutorials on the AtomicPages Blog
In order to install Sass, we need two things:
Once these are installed, we are ready to install Sass.
Note: if you are unsure if you have Ruby Gems installed then issue the gem -v
command in Terminal or Command Prompt with Ruby. If you see a result then you have it. If your OS complains then you don't and will need to install it.
Thankfully, installing Sass is very easy using Ruby Gems! Open Terminal or Command Prompt with Ruby:
sudo gem install sass
gem install sass
We can ensure Sass has been installed successfully by running the following command in our console window:
sass -v
Now that we have Sass installed let's test it! Copy & paste the following text into a file called style.scss
and then return to your console window.
// Code from sass-lang.com
// Variable Definitions
$page-width: 800px;
$sidebar-width: 200px;
$primary-color: #eee;
// Global Attributes
body {
font: {
family: sans-serif;
size: 30em;
weight: bold;
}
}
// Scoped Styles
#contents {
width: $page-width;
#sidebar {
float: right;
width: $sidebar-width;
}
#main {
width: $page-width - $sidebar-width;
background: $primary-color;
h2 { color: blue; }
}
}
#footer { height: 200px; }
Now that we have our style.scss
file we need to navigate to the directory where this is. If you're unfamiliar with directory traversal on your system here are a few good resources to learning about basic BASH and Command Prompt techniques:
# navigate to style.scss
$ cd path/to/my/style.scss
# convert to css
$ sass --update style.scss:style.css
:: navigate to style.scss
C:\Users\John Doe>cd path/to/my/style.scss
:: convert to css
C:\Users\John Doe>sass --update style.scss:style.css
Note: #
and ::
are comments and are NOT meant to be typed.
Congrats! You have written and compiled your first Sass script!
For additional information about what options you have using the sass
command, run sass --help
on in your console window.
Do you hear command line and think, "yuck!" Good news! You have options:
- CodeKit — OS X (paid)
- Compass.app — OS X, Linux, Windows (paid, open source)
- Hammer — OS X (paid)
- Koala — OS X, Linux, Windows (free, open source)
- LiveReload — OS X, Windows (paid, open source)
- Mixture — OS X, Windows (paid)
- Prepros — OS X, Windows (paid, open source)
- Scout — OS X, Windows (free, open source)