Sass addresses many of the maintainability problems we typically experience when writing CSS, and makes writing styles fun again! This basic course will help you make the most out of this awesome preprocessor.
1 — Sass Fundamentals
In this course, we’ll start with regular CSS, and quickly layer on new capabilities and tools that will change the way you think about your app’s styles. Quickly, after moving on from the basics, we’ll start to see how style can be parameterized and re-used, avoiding repetition and redundancy, while keeping everything readable and maintainable.
Next, we’ll look at directives that bring imperative code concepts into stylesheets, like looping, conditional blocks and more. Finally, we’ll experiment with building our own Sass extension, where we can add new vocabulary and capabilities to the way we express styles.Agenda
The preprocessor revolution
We’ll look at some of the common CSS pitfalls that motivated the invention of these technologies, and highlight how much easier things become in the Sass world.
When it comes to transforming Sass files to CSS, we have a few options. We’ll learn about options you can use from the command line, and in a node build tool.
EXERCISE 1 - Using the Sass CLI
Build a shell script that transforms a Scss file into two CSS files: one that’s human-friendly for a development environment, and one that’s smaller for a production environment. How much of a file size savings did we get for this optimization?
Nested & Modular Styles
Partials and the
@import directive allow us to write stylesheets in a modular and maintainable way. We’ll look at the pitfalls of the CSS
@importat-rule, and how Sass provides similar capabilities while addressing some common problems.
Exercise 2 - DRY Styles
Sticking to “the inception rule”, refactor the Scss file you’re given to take advantage of Sass nesting features
Exercise 3 - Partials and import
@import directive, combined with partials allow us to break our stylesheets up into modular units. Make all of your failing tests pass, while avoiding any new redundancy.
SassScript: Variables & Operators
Being able to store and re-use values is a game-changer, in terms of reducing redundancy and improving consistency and maintainability throughout your styles. We’ll discuss variable best practices, unit conversions and more!
EXERCISE 4 - Variable Math
Make the failing tests pass, by substituting literal values with SassScript expressions. Keep units in mind, and avoid any fudge factors!
Break for lunch.
Sass has a treasure trove of built-in functions that allow us to expressively manipulate colors, numbers, lists, maps and more! We’ll look at what’s available, and then learn some best practices for practical and maintainable usage.
Mixins allow us to re-use basic or parameterized chunks of style, via the
@extend directive, without having to introduce tons of complexity into your HTML.
EXERCISE 5 - DRY Buttons
.button class in our app is available in a variety of colors, but there’s a lot of repeated style between them. Using a combination of nested styles, color functions and mixins, design a means of generating a button of an arbitrary color (or set of colors).
@extend directive is a powerful tool, we can use to “inherit” styles, but if over-used it has the potential to increase the size and complexity of the compiled CSS. We’ll take a look at all this feature of Sass can do, some limitations as to where and how it can be applied, and some patterns for responsible use.
EXERCISE 6 - Links as Buttons
Many UI frameworks represent links with a particular class and the
<button> with similar visual styles. Refactor and make use of the
@extend directive to accomplish this. Your change must result in a net reduction in LOC of Scss, and no more than a 2% increase in compressed CSS filesize.
One of the most powerful aspects of Sass is the ability to add control flow to our sales. With the
@while directives, we can create powerful, concise and expressive styles, which are far easier to maintain and tweak than their CSS counterparts.
EXERCISE 7 - Grid Generator
Using control directives, build a mixin that generates a grid with an arbitrary number of columns. As an extra challenge, try to make the grid responsive!
We’ll recap everything we’ve learned today