I’ve been learning some Sassy CSS and wanted to talk about three awesome features that it provides: variables, nesting and mixins.
At a high level, Sass is simply a CSS preprocessor with two syntaxes:
The most commonly used syntax is known as “SCSS” (for “Sassy CSS”), and is a superset of CSS3’s syntax. This means that every valid CSS3 stylesheet is valid SCSS as well. SCSS files use the extension
.scss. The second, older syntax is known as the indented syntax indented syntax</a> (or just
.sass). Inspired by Haml’s terseness, it’s intended for people who prefer conciseness over similarity to CSS.
I prefer using SCSS because it uses brackets and semi-colons, just like CSS. It also doesn’t care about indentation levels or white-space, like the second syntax. SCSS contains all the features of CSS, but has been expanded to include the features of Sass as well.
You will need to get Sass setup on your project before you can start using it. A lot of the new frameworks come with Sass pre-installed. Here are some great resources to help you:
- Install Sass.
- Check out the sassy documentation.
- The Rails asset pipeline supports SCSS.
- Middleman comes with SCSS by default.
I was recently pairing with my mentor Stephanie and we decided on the blue hex
#25a8e0 for our links and buttons. Now try remembering that hex number each time you need to use it. Hard, right? We kept forgetting and had to stop and look it up.
This is where variables becomes a huge time saver:
Variables allow you to re-use colors, sizes, and other values without repeating yourself. This means that changes that should be small, such as tweaking the coloring or the sizing, can be done in one place, not all over the stylesheet.
SCSS allows you to set variables that can be used throughout your stylesheet. Variables start with
$ and are declared just like properties. You can use any value that the CSS property allows such as colors, numbers or text.
Pretty awesome right? I just set my variables and label them. Now I only have to remember
$blue rather than
Repeating yourself in your CSS is annoying and makes your files unnecessarily long. SCSS allows you to avoid this by nesting child selectors within parent selectors.
With CSS, you might be writing something like this:
&is a Sass special character. In a selector,
&will be replaced verbatim with the parent selector.
Let’s take the previous example and nest them:
Much cleaner! Now let’s look at an example for a navigation:
That’s a lot of repeating. Instead, let’s nest this bad boy:
Boom! This is a great example of how to DRY up your CSS code. It does take some getting use to, but after a short while you’ll never want to go back to plain ole CSS.
Mixins are one of Sass’s most powerful features. They allow re-use of styles without having to memorize or copy and paste them.
Mixins are defined using the
@mixindirective, which takes a block of styles that can then be included in another selector using the
So let’s say I want to add a nice fade transition to my link and button classes:
That’s a lot of repetition! Let’s use
@mixin directive and set a name of fade:
Pretty handy right? Let’s get more complicated with some properties that would be harder to remember:
Now we can reuse
box-shadow on other properties quickly without all the mess.
Give SCSS a try! It makes your front-end coding easier and faster. While SCSS is not an extension of the CSS standard itself, it gives you powerful new features that CSS lacks. Thanks for reading!