In part one of this series, I introduced you to Sass in Shopify themes, explained variables and nesting, and discussed the limitations of the online store editor with Sass.

This article will focus on some of the more creative parts of Sass development — the things that can make your stylesheets smarter. We’ll take a look at using some of the operators built into Sass, as well as using some native Sass functions. We’ll also look at creating Sass mixins and extends.

Arithmetic operators

Sass has standard arithmetic operators baked in. These can be super helpful when trying to do calculations in your stylesheets, and have to support older browsers (IE8/IE9 reliably or Opera Mini) that don’t play nicely with the CSS calc() function. The calculations are done on compilation, and rendered as a specific value.

When Sass gets compiled, the evaluation of the math we’ve done outputs to the final values in our stylesheet. This means that these values will work in browsers that don’t support the calc() CSS function.

Note that when Sass interpolation#{} is used near operators like /, those operators are treated as plain CSS rather than math operators.

Where #{$font-size} and #{$line-height} resolve to 100%/1.5, instead of Sass trying to divide those two values by one another.

Mixins

Sometimes it can be tedious to write in CSS over and over. Mixins differ from functions in that a function’s main objective is to return a value, whereas a mixin enables the creation of a reusable chunk of CSS. Think of a @mixin like an include or snippet.

To create a mixin use the @mixin directive, and give it a name. Mixins can take multiple arguments in the form of variables, which are comma separated.

A simple example of this is to create a reusable button style. This mixin also includes a Sass function, which you can read more about in the next section of this article.

In the example above, @mixin link() is passed an argument of $color-link. Then, the $color-link argument is used throughout the mixin property values. To use the mixin you would use the @include directive, followed by the mixin name, and pass it the appropriate argument. In the case of this mixin, it’s a color value.

Below is a mixin I use to visually hide an element when I build themes, but still allows for that element to be read aloud by screen readers. Using a mixin for this makes it clear that the element is visually hidden in the styling. As a mixin, it then can be used multiple times throughout your styles, all while keeping accessibility in mind.

Functions

In Sass, you’ll hear about two types of functions, functions which you write yourself using the @function directive, and functions which are shipped with Sass.

Creating custom Sass functions

There are some pretty useful functions that you can create yourself. A function you create yourself uses the @function directive, followed by the name of the function. It must be declared first, and once declared, can be used later on in your stylesheet by calling on the name of your function.

The output of a function is a single value. This value can be any Sass data type, which includes: numbers, strings, colors, booleans, or lists. Functions can accept multiple arguments in the form of variables, which are comma separated.

I usually put custom Sass functions at the top of my stylesheet, just after variables, or in a separate file that’s imported after variables, but before all my other styles. We’ll talk more about file structure and workflow in part three of this series.

I often use an em() function from this Web Design Weekly article. The function converts px values into em values ✨ auto-magically ✨ for you, based on a set base font size.

This simple function converts to em values for you, which makes things much simpler than calculating them for yourself. Using em values for all sizing is especially helpful. If a user increases or decreases the size of their text, and you’ve used em sizing for layouts as well, things will automatically scale and flow appropriately.

Using native Sass functions

There are also a huge list of functions that ship with Sass, which come in handy when theming. These native Sass functions are super helpful when creating consistent variations for a specific color within your theme. For example, you might want to create a button, that uses your primary color as a background, but darkens that background when it’s hovered or active.

We’re going to use the Sass darken() function in the example below, to darken the $color-primary for our hover states.

The darken() function takes two parameters, a color (which can be of any format), and an amount to decrease the lightness by, which is a percentage from 0% - 100%.

darken($color, $amount)

In this example, we set the $color-primary as the background-color of the button. Using the Sass function darken(), we set the :hover and :focus state for the button to a darker background-color based on the $color-primary.

Extend

The @extend directive lets you share a set of CSS properties from one selector to another. This can be incredibly useful if you don’t have access to update or change markup, but need to style an element. Note that when one uses the @extend selector, it extends all nested selectors as well.

Notice that Sass doesn't "go get" the styles from .box, inserting them below into .block, then duplicating the styles. Instead, when Sass compiles it, the comma separates both selectors, applying the styles more efficiently.

Be careful not to abuse @extend. When you want to use @extend ask yourself if creating another class and adding it to your Liquid template might be more appropriate. There are some other gotchas that arise when you use @extend that you should be aware of, these include lengthy output, and selector order issues.

You’re on your way to making your theme smarter!

By using Sass operators, mixins, and functions, you can begin thinking about how you can make your styles more efficient. Already, you can start to see how much less typing you’ll have to do when styling.

Stay tuned next week for part three of this beginner’s guide to Sass with Shopify!

Do you know any helpful mixins or functions for Shopify Themes? Tell us about your favorite ones, and why you love using Sass in the comments below!

Email address

Password

Your store name

Let’s grow your freelance or agency business.

Get design inspiration, practical takeaways and free resources to help you find and delight clients.

Email Address

Thanks for subscribing

You’ll start receiving free tips and resources soon. In the meantime, find out how Shopify’s Partner Program can help you grow your business.

About the Author

Tiffany is a Front End Developer Advocate Lead at Shopify. She specializes in front end development, user experience design, accessibility, and is passionate about inspiring young designers and developers. Previously, she was an Ottawa Chapter Lead for Ladies Learning Code and worked as a Front End Developer for Partnerships at Shopify.