CSS on its own can be fun, but stylesheets are getting larger, more
complex, and harder to maintain. This is where a preprocessor can help.
Sass lets you use features that don't exist in CSS yet like variables,
nesting, mixins, inheritance and other nifty goodies that make writing CSS
fun again.
Once you start tinkering with Sass, it will take your preprocessed Sass
file and save it as a normal CSS file that you can use in your website.
The most direct way to make this happen is in your terminal. Once Sass is
installed, you can compile your Sass to CSS using the `sass` command.
You'll need to tell Sass which file to build from, and where to output CSS
to. For example, running `sass input.scss output.css` from your terminal
would take a single Sass file, `input.scss`, and compile that file to
`output.css`.
You can also watch individual files or directories with the `--watch`
flag. The watch flag tells Sass to watch your source files for changes,
and re-compile CSS each time you save your Sass. If you wanted to watch
(instead of manually build) your `input.scss` file, you'd just add the
watch flag to your command, like so:
sass --watch input.scss output.css
You can watch and output to directories by using folder paths as your
input and output, and separating them with a colon. In this example:
= partial 'code-snippets/homepage-sass-watch'
:markdown
Sass would watch all files in the `app/sass` folder for changes, and
compile CSS to the `public/stylesheets` folder.
%section.sl-l-section.sl-l-section--small#topic-2
:markdown
## Variables
Think of variables as a way to store information that you want to reuse
throughout your stylesheet. You can store things like colors, font stacks,
or any CSS value you think you'll want to reuse. Sass uses the `$` symbol
to make something a variable. Here's an example:
<div id="tabs">
<ul>
<li><a href="#tabs-1">First</a></li>
<li><a href="#tabs-2">Second</a></li>
<li><a href="#tabs-3">Third</a></li>
</ul>
<div id="tabs-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div id="tabs-2">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.</div>
<div id="tabs-3">Nam dui erat, auctor a, dignissim quis, sollicitudin eu, felis. Pellentesque nisi urna, interdum eget, sagittis et, consequat vestibulum, lacus. Mauris porttitor ullamcorper augue.</div>
</div>
- example do
:plain
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
===
$font-stack: Helvetica, sans-serif
$primary-color: #333
body
font: 100% $font-stack
color: $primary-color
:markdown
When the Sass is processed, it takes the variables we define for the
`$font-stack` and `$primary-color` and outputs normal CSS with our
variable values placed in the CSS. This can be extremely powerful when
working with brand colors and keeping them consistent throughout the site.
%section.sl-l-section.sl-l-section--small#topic-3
:markdown
## Nesting
When writing HTML you've probably noticed that it has a clear nested and
visual hierarchy. CSS, on the other hand, doesn't.
Sass will let you nest your CSS selectors in a way that follows the same
visual hierarchy of your HTML. Be aware that overly nested rules will
result in over-qualified CSS that could prove hard to maintain and is
generally considered bad practice.
With that in mind, here's an example of some typical styles for a site's