Introduction to Sass for New WordPress Theme Designers
As a WordPress theme designer, you quickly learn the challenges of writing and maintaining CSS stylesheets. Even for small themes, stylesheets can become long and complex, making them difficult to navigate and update. And with the ever-expanding capabilities of CSS3 and responsive design, stylesheet complexity is only increasing.
Fortunately, there‘s a better way. Sass, which stands for "Syntactically Awesome Stylesheets", is a CSS preprocessor that extends the CSS language with powerful features that make stylesheets more maintainable and quick to write.
In this beginner‘s guide, you‘ll learn how Sass can revolutionize your WordPress theme development workflow. I‘ll explain what Sass is and why you should use it. Then I‘ll show you how to install Sass and use it in your WordPress themes, with real examples you can follow along with. Finally, I‘ll discuss best practices and more advanced features to be aware of.
Let‘s get started!
What is Sass?
Sass is a CSS preprocessor, meaning it‘s a special stylesheet language that gets compiled into regular CSS. You write your styles using Sass‘s special syntax, then use a Sass compiler to generate a normal CSS file that browsers can read.
So why would you want to learn a new syntax instead of just writing CSS directly? Because Sass adds many useful features on top of CSS that make your stylesheets more powerful, maintainable, and quicker to write.
Here are some of the key capabilities Sass brings to the table:
- Variables — Assign reusable values to variables for things like colors, font stacks, sizes, etc.
- Nesting — Nest child selectors inside parents to avoid repetition and create more logical stylesheets
- Imports — Split stylesheets into smaller files and combine them to avoid one huge file
- Extend — Share sets of CSS properties between rules without copy-pasting
- Mixins — Define reusable styles that can accept arguments, like custom functions
- Operators — Perform math and logic operations to generate dynamic property values
- Functions — Use built-in or custom functions to manipulate values and get information about the environment
If these sound confusing now, don‘t worry – I‘ll demonstrate many of them with examples shortly. For now, just be aware that with Sass, stylesheets become much more dynamic, modular, and efficient to write and maintain.
Why Use Sass for WordPress Themes?
You may be wondering if it‘s worth learning a new language just for styling WordPress themes. Here are a few key reasons why using Sass for WordPress theme development is a no-brainer:
Maintainability — Sass‘s features let you write more maintainable, DRY (Don‘t Repeat Yourself) stylesheets by minimizing repetition and splitting up styling into modular parts. This is important for WordPress themes that may need to be frequently updated.
Speed — Once you learn the basics, Sass makes it much quicker to write and change styles in your theme. You‘ll type a lot less and make updates in one place instead of many.
Power — Sass extends what‘s possible with CSS alone, letting you generate dynamic styles, reduce complexity, create responsive layouts more easily, and reuse code. You‘ll be able to write smarter stylesheets.
Convention — Sass is quickly becoming the standard for professional WordPress theme development. Major frameworks like Genesis and Underscores use it, as do respected theme shops. Learning Sass makes your skills more marketable.
In short, Sass makes better, faster stylesheet authoring possible with just a little upfront investment in learning. The time you spend learning Sass will pay dividends in productivity and quality over the life of a theme.
Installing Sass
To use Sass, you first need to install it on your local WordPress development environment. (You wouldn‘t want to be compiling Sass directly on a live server.)
There are two main ways to install Sass:
GUI application (easiest) — The simplest way for beginners is to use a free application like Koala or Scout-App to compile Sass without needing to touch the command line.
Command line (most flexible) — More advanced users can install Sass on the command line using a language like Ruby or Node.js. This is more complex to set up but allows more control over the compiling process.
For this tutorial, I‘ll assume you‘re using the GUI app Koala, which makes it dead-simple to get Sass running in any environment. To get started:
- Download and install the free Koala app for your operating system.
- Open Koala and drag your WordPress theme folder into the left sidebar to add it as a project.
- Right-click your theme folder in the sidebar and choose "Set output path" to select where Koala should generate the compiled CSS (style.css in your theme root).
- Create a sass/ subdirectory in your theme and add a styles.scss file to it. Any Sass files need to have the .scss extension.
- Start writing Sass in your styles.scss file and save it. You can click the "Compile" button in Koala and it will automatically generate your style.css file in the theme root.
That‘s it! With just a little configuration, you can now author in Sass and have Koala automatically generate the compiled CSS for your WordPress theme every time you make a change.
Let‘s look at some examples of what you can do with your new Sass superpowers.
Using Sass in a WordPress Theme
Here are some of the most common and powerful ways to use Sass in the context of a WordPress theme stylesheet.
Variables
One of the most convenient Sass features is variables. They let you assign values to reusable names so you can keep your styles DRY and consistent.
For example, say you have a few colors you reuse throughout your stylesheet, like a main brand color. With Sass, you can assign that color value to a variable with a memorable name:
$brand-primary: #A71E3D;
Then wherever you want to use that color, you can refer to the variable instead of retyping the hex value:
a {
color: $brand-primary;
}
.button {
background-color: $brand-primary;
}
This has a few key benefits:
- Your stylesheet is more readable with the color name instead of a hex value.
- If you ever need to change the color, you only change it in one place instead of everywhere.
- You can perform operations on color variables to generate variations, like lightening/darkening.
You can create variables for anything you‘d like, from fonts to sizes to media query breakpoints. Some WordPress starter themes like Underscores use variables extensively to make global stylesheet changes easy.
Nesting
Another major convenience of Sass is selector nesting. It lets you group child styles under a parent selector to create a more logical stylesheet structure and avoid repetition.
For example, say you want to style all the links inside a .post class. With vanilla CSS you‘d have to write each child selector separately:
.post { }
.post a { }
.post a:hover { }
But with Sass, you can nest the child link styles logically under the parent .post:
.post {
a {
color: $link-color;
&:hover {
text-decoration: underline;
}}
}
The & character refers to the parent selector, letting you put the pseudo-class on the a tag. This generates vanilla CSS like:
.post a {
color: #A71E3D;
}
.post a:hover {
text-decoration: underline;
}
You can nest as deep as you‘d like, but it‘s a good practice to keep nesting to just a few levels to avoid overly specific selectors.
Mixins
Mixins are one of the most powerful Sass features. They let you define a set of styles once and reuse them anywhere, even dynamically generating property values from arguments.
Let‘s say you find yourself using the same group of properties to create a button style in multiple places. Instead of repeating them each time, you can create a mixin:
@mixin button {
padding: 10px 20px;
border-radius: 5px;
text-transform: uppercase;
background-color: $button-color;
}
Now anywhere you want to apply that button styling, just use the @include directive with the mixin name:
.submit-button {
@include button;
}
.more-link {
@include button;
font-size: 0.8em;
}
This use of mixins makes your code more DRY, reusable, and semantic. You may use a dozen or more mixins in a stylesheet for things like setting flex properties, creating a grid, or styling form elements consistently.
Mixins can also take arguments that get passed into the generated properties. A common use case is vendor prefixes for CSS3 features:
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
border-radius: $radius;
}
.box {
@include border-radius(10px);
}
You can see how mixins are like custom functions for your stylesheets. They promote reusability and let you write more dynamic, efficient CSS.
Imports
In vanilla CSS, you can use the @import directive to include external stylesheets in another. But this requires an extra HTTP request to fetch the file.
Sass extends @import to let you use it with .scss or .sass files (Sass‘s two flavors of syntax). The imported file gets included in the compiled CSS output, so there‘s no extra HTTP request.
Why is this helpful? It lets you split up your theme‘s styles into smaller, more focused files and combine them into a single stylesheet for the browser.
For example, you might have individual files for variables, typography, layout, forms, etc. Then in your main styles.scss file, you can import them in the proper order:
@import "variables";
@import "typography";
@import "layout";
@import "forms";
When the Sass is compiled, the contents of those files will get output in the final CSS in the order specified. This makes your stylesheet codebase more organized and modular.
Sass also has a mechanism called partials that lets you designate which files should only be imported and not compiled on their own. Simply prefix the file with an underscore, like _variables.scss, and Sass will know to only include it when imported elsewhere.
A Note on Sass Best Practices
As you explore Sass, there are a few best practices to keep in mind for creating maintainable stylesheets:
- Don‘t nest selectors too deep (1-3 levels is ideal)
- Use variables for any values you might want to reuse or tweak later
- Create small, focused partials and import them into the main stylesheet
- Prefix partials with an underscore so the Sass compiler ignores them
- Stick to a consistent naming convention for variables and mixins
- Lint your Sass with a tool like scss-lint to avoid errors and enforce standards
Sass is a powerful tool, and like any power tool it‘s best to follow the instructions. But once you have a handle on the basics and best practices, you can accomplish some amazing things with it.
More Advanced Sass Features
What I‘ve covered so far are the core concepts of Sass that you‘ll use in almost every project. But there are more advanced features to explore as well.
Directives like @extend and @content let you create powerful placeholders and abstract patterns in your stylesheets. Built-in functions like darken() and lighten() manipulate color values. Control directives like @if, @for, @each, and @while let you create complex, programmatic styles.
On top of that, the Sass community has created a rich ecosystem of extensions, frameworks, and tools that build on the core language:
- Compass — A framework that adds a library of mixins, helpers, and patterns
- Bourbon — A lightweight mixin library for common style needs
- Susy — A layout toolkit for creating custom grids
- Sassline — A SCSS framework for creating responsive typography
- Modular Scale — A system for creating harmonious font and spacing sizes
Exploring all these Sass tools could fill a book, and you certainly don‘t need to learn them all at once. But they demonstrate how Sass is evolving into a complete ecosystem for stylesheet authoring.
What‘s Next?
My goal with this article was to explain the basics of Sass in a WordPress theme development context. If you followed along with the examples, you should be able to start using Sass in your own themes.
But there‘s much more to learn. Sass is a deep subject, and it‘s constantly evolving along with CSS itself. To dive deeper, check out these resources:
- The official Sass language guide
- Sass style guide with best practices
- A Complete Guide to Learning Sass by the Tuts+ network
- Team Treehouse‘s Sass Basics course
- CSS-Tricks‘ Sass reference guide
- Writing Your First Sass Stylesheet by Tyler Sticka
Also be sure to explore the WordPress starter themes and frameworks I mentioned that use Sass. Looking through their stylesheets is a great way to see how Sass works in the wild.
Like the wider web design world, the WordPress theming community is moving to Sass as the standard for stylesheet authoring. Getting comfortable with Sass basics now will give you a leg up in creating modern, maintainable, and efficient theme styles.
Happy Sassing!
