Use Sass (SCSS) in a WordPress theme
Sass is a CSS preprocessor: it adds variables, nesting, mixins and partial files to CSS, and compiles everything into one normal CSS file. The SCSS syntax is a superset of CSS, so any valid CSS file is already valid SCSS and you can start using Sass gradually. In a theme you write SCSS sources and enqueue the compiled CSS.
Problem: The theme’s CSS repeats the same colours and breakpoints everywhere and has grown into one long file that is hard to maintain.
Solution: Install the sass package with npm, split the styles into partials, compile them into one minified file, and enqueue that file with its modification time as the version.
Installing the compiler
npm init -y npm install --save-dev sass
Add two scripts to package.json:
{
"scripts": {
"css": "sass scss/style.scss css/style.css --style=compressed",
"css:watch": "sass scss/style.scss css/style.css --watch"
}
}
npm run css:watch recompiles on every save during development, npm run css builds the final file. The sass package is Dart Sass; the older node-sass compiles the same code but needs native binaries that break on Node.js updates.
Partials
Files whose name starts with an underscore are not compiled on their own, they are imported into the main file:
scss/ ├── style.scss # imports everything ├── _variables.scss # colours, fonts, breakpoints ├── _mixins.scss ├── _base.scss ├── _header.scss ├── _footer.scss └── _woocommerce.scss
style.scss:
@import 'variables'; @import 'mixins'; @import 'base'; @import 'header'; @import 'footer'; @import 'woocommerce';
The order matters: variables and mixins must be imported before the files that use them.
Variables, nesting and mixins
// _variables.scss
$color-primary: #3498db;
$color-text: #333;
$font-base: 'Open Sans', sans-serif;
$bp-md: 768px;
// _mixins.scss
@mixin below( $bp ) {
@media (max-width: $bp) { @content; }
}
// _header.scss
.btn {
background: $color-primary;
color: #fff;
font-family: $font-base;
&:hover {
background: darken( $color-primary, 10% );
}
&--outline {
background: transparent;
border: 2px solid $color-primary;
color: $color-primary;
}
}
.nav-menu {
display: flex;
@include below( $bp-md ) {
flex-direction: column;
}
}
Keep nesting shallow, two or three levels: deep nesting produces long selectors with high specificity that are hard to override later.
Enqueueing the compiled file
<?php
add_action( 'wp_enqueue_scripts', function () {
$file = get_template_directory() . '/css/style.css';
wp_enqueue_style(
'mytheme-style',
get_template_directory_uri() . '/css/style.css',
array(),
filemtime( $file )
);
} );
The theme’s own style.css in the root must still exist with the theme header, WordPress reads the theme name from it. The same approach works with LESS, see importing Font Awesome in LESS.
Update (2019): Dart Sass 1.23 introduced the module system: @use 'variables' as *; instead of @import, with namespaces and without loading the same file twice. @import still works but is planned to be removed, so new projects should use @use.
NOTE: Commit the SCSS sources and decide consciously about the compiled CSS. If the theme is deployed by copying files, the compiled css/style.css must be in the repository or built during deployment, otherwise the live site has no styles.