Advertisement
  1. Web Design
  2. Sass
Webdesign

How to Use Sass to Build One Project With Multiple Themes

by
Difficulty:IntermediateLength:ShortLanguages:

The frontend-developer’s workflow has undergone big changes in recent years. More complexity, higher requirements and bigger projects move us toward new technologies such as preprocessors. Personally, I love my preprocessor workflow and wouldn't want to miss Sass for my projects these days - without it I would be a mess.

One situation in particular where Sass has helped me out is in creating a single solid frontend which can have different themes - changing colors, images or the fonts with ease. Today I’m going to describe my workflow, I hope you take something useful away from it.

Basic Structure

Here is the structure of an example project. You can use whatever partials you logically feel should be included. The key is in having a separate folder for themes and a new .scss file for each theme. 

Folder-Structure

Build the Base with a Main File

In the application.scss file you import all your partials, ignoring the folder _themes/ at this point. This builds the foundation upon which we can create different themes.

Configuration

Colors, Fonts and much more

Configuring your project is very important. Here variables come to the rescue, allowing us to set defaults then overwrite values at the theme level later on. Here you can see I've set some variables for colors, fonts and border settings.

For more detail on naming variables take a look at Jim Nielsen's Quick Tip: Name Your SASS Variables Modularly.

The key to this stage is in using the !default flag after the variable declarations. Doing this allows you to overwrite them within the theme .scss files; the !default effectively says “use this value if it isn’t defined elsewhere”.

Background-Images

Very often developers don't create variables for images, instead writing urls directly within selectors. An approach I like is to remove all paths within partials and put them in the configuration file as variables. This will make your life easier and the project better maintainable.

Example of a Basic Module

Here you can see some background variables in action. This is a modular partial for an accordion, making use of the global variable $sprite but also setting and using a variable $accordion-bgcolor which is specific to the module. 

How to Create a Theme

In your theme file (such as the _themes/_light-theme/light.scss from the demo structure above) import the application.scss which contains all the modules, layouts and so on. This is the basis for everything; with a theme we'll be placing a second layer over it. Nothing more. Once you’ve imported application.scss add the same variables used earlier, but define values specific to this theme. 

Using this technique, if we add new modules to the base of the project we will automatically (and harmlessly) compile them within all our themes.

Watching Multiple Files in Sass

The last step is to compile the Sass to CSS and get two different files for usage. We must compile application.scss and the light.scss. Using the command line you'd use something like:

If you use an app, such as CodeKit, or Prepros App, compiling will be a more visual process.

Include the light.css file in the head of your page and take a look at your freshened up website.

Conclusion

As you can see, it’s helpful and straightforward to have multiple theme files, all based upon one solid foundation. Feel free to ask questions, or give feedback and share your experiences with us in the comments section.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.