Useful SCSS Mixins for WordPress Theme Development

Updated reading time

Working with SCSS Mixins will help with calculations and adding shortcuts to the CSS styling in theme development.

Use these useful ones. And write in the comments below your useful SCSS mixins!

BEM SCSS Mixin

This one was taken from Hugo Giraudel’s snippet in CSS Tricks.

It shortens the BEM syntax of your SCSS child elements into the directive @include e('elementName') {} making it a lot tidier than you would without these. You can also use @include m('modifierName') {} for the modifier CSS class.

PX to REM SCSS Mixin Function

This function mixin converts px to rem. You can then use rem(14px) that will calculate itself later against the $browser-context that you will set in this mixin. A great mixin from Chris Coyier at CSS Tricks. I have modified this slightly to use rem however the original article uses this for em.

These two mixins are the most common mixins in modern theme development.

Related:  7 Ways a Custom Wordpress Theme May Be Better

Please feel free to use these with your other mixins. And write down in the comments your mixins that you use!

How to Find Your Mixins File

You will typically find the mixins file in the same directory/level as your style.scss file.

In the Underscores theme used by Automattic — the company behind WordPress — you will find it within the sass/mixins folder as _mixins-master.scss.

Adding the Mixin to your Mixins file

You have to simply copy the mixins for example the ones mentioned in this article, and paste them directly to the bottom of your mixins file.

Why is it called mixins-master.scss

The file was called _mixins-master.scss for the sole reason that you will want to create separate files e.g. sass/mixins/_buttons.scss where you can have your own button mixins. And then you will have to add the line @import 'location-to-your-buttons-mixin' at the top of your _mixins-master.scss file.

However this is purely from an organisational perspective.

It is not required to do so, but if you happen to be having a lot of mixins, then organising them into their separate files will help a lot.

You can take an example from the Bootstrap v4 mixins folder.

The Bootstrap framework uses @import directives within their master mixin file scss/mixins.scss since they use a lot of mixins available to use for web developers.