Introduction to Sass

In a previous post, I’ve introduced using Sass through theย WordPress Customiser and Jetpack. In this post, I want to quickly explain what exactly Sass is and where to get started.

Sass stands for “Syntactically awesome style sheets”, and is basically CSS with superpowers — or “CSS on crystal meth” as I described it in one of my more dark moments. The best way to explain what those superpowers are is to show it.

Variables

The main green I use on my own website, currently has aย HEX value of #78bf1a.ย With CSS, I would have to mention that value every time I want to use it.

That’s not the case with Sass. With Sass, I can define any value that I’m expecting to reuse beforehand, so I don’t have to repeat myself. The very nice thing is that whenever I plan to update my theme colours, I can just update it once where the variables are defined.

In it’s simplest form, variables are defined as $variable: value;.ย For a color that would be $colour-primary: #78bf1a;, but you can use this for anything that is repeated. The below example is one for fonts and shows how to call the value.

sass-variable.png
Define $font-mainย variable and use for h1ย element

Nesting

A next major thing is that with Sass, you can follow the structure of your HTML by calling selectors within selectors. So while you’d have to target h1,ย h1 aย and h1 a:hoverย separately in CSS to define their values, you can do that all in one go, as demonstrated below.

sass-nesting
Left: plain CSS | Right: Sass nesting

Mixins

A next step would be to look at mixins. A mixin is a group of CSS declarations that you want to reuse throughout your website. You’d define the mixins at the top of your stylesheet starting with @mixin nameย where “name” can be replaced by anything that’s meaningful. To use it for the selectors, you use @include nameย to use the mixin, this will apply the mixin with the name “name” to the selector you’re using.

In both the examples below, the Sass looks longer than the original CSS, but that’s because we’re only showing the mixin applied to a single selector. The strength of mixins is that you can reuse and reapply them on as many selectors.

For example, media queries are mostly written at the bottom of the CSS style sheet, but with Sass you can make it contextual and set the variations for your different queries within your selectors rather than splitting them up.

sass-mixinmedia
Left: CSS with media queries | Right: media queries defined as aย mixin and applied within a selector.

Another example wouldย apply to not repeating browser compatibility issues with CSS. Instead of defining a -webkitย and -mozย for each time you use border-radius, you define it beforehand so you don’t have to repeat it.

sass-mixinborderradius
Left: CSS with browser compatibility | Right: Sass with mixins setting up browser-compatibility and global “border-radius” value.

In the example above, we’ve also already defined the value of the border-radius, but if you leave that open, you can just set that value within the selector if you’re planning to have different radiuses on different selectors. So then you’d delete the : 2emย to get @mixin border-radius ( $value )ย and set the value in the selector to @include border-radius (50%);.ย Also, note that you don’t need to use the name “border-radius” for the mixin.

Functions

A last thing I want to show in this introduction are the built-in Sass functions. In many cases, these don’t really have an equivalent in CSS.

For example, one of the functions is lighten(#000, 10%)ย where you first set a colour and then make it 10% lighter. This is great if you’re using a clear brand colour and you want to use different accents of that colour for the rest of the website’s styling.

This is not the same as opacity. With opacity, it defines the level to which you can see what is behind through it, while this just changes the actual colour. You could combine the two by for example using rgba( lighten(#000,10%), 20% )ย where the colour is 10% lighter than black, and there’s an opacity of 20%.

sass-function
Applying the “lighten” function to hovering over an anchor element.

A list of available Sass-functions can be found here.

Next steps

When you get started with Sass, I’d suggest following the order of this tutorial. Start with variables before you move on. The first six months I used Sass, I only used it for variables and not having to go through my whole stylesheet just to change the colour of my branding; with Sass I update it at one place and the whole site adopts that colour.

But this is just an introduction. Sass is open source and has an active community. Just searching for other cool Sass tweaks, will give you a wide array of options. For example, have a look at the @extendย options, or operators.

Resources

This blog post is a summary of a workshop I gave together with Gareth at WordCamp Cape Town 2017, called “Sass to the reCSSue”. You can first find some other resources and then our slides below that.


Comments

2 responses to “Introduction to Sass”

  1. I finally got around to playing with this a bit over the weekend. Took some getting used to, but it’s so much easier than regular CSS!

  2. […] Finally, I wanted to try a few new things with Sass. I’ve been a big fan since I did some courses in CSS and its advanced version, Sass (I took those courses on Treehouse by the way). Last year, at WordCamp Cape Town, I even gave an introduction to Sass. […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from job.blog

Subscribe now to keep reading and get access to the full archive.

Continue reading