Utilizing Sass for stylesheet creation, even with its fundamental features like nesting properties or utilizing variables, significantly enhances efficiency and simplifies the workflow for front-end developers. It’s no wonder that CSS preprocessors have become the standard practice for styling websites and applications; their absence would be deeply felt.
When addressing theming, the process of modifying the visual presentation of a website while preserving its layout, Sass’s features such as mixins or functions offer a remarkably expedited approach. This SCSS tutorial will guide you through crafting a minimalist theme and leveraging SCSS to empower your CSS programming.
A Basic Approach with Mixins
Consider the following layout:
| |

Imagine you’re tasked with generating multiple themes for this layout. The theme needs to apply distinct colors to all containers (including the main container) and the button. The selection of the theme will be determined by a class assigned to either the body element or potentially an “outer” container:
| |
Let’s construct a mixin named “themable” to encapsulate our color scheme as parameters.
| |
Subsequently, utilize this mixin to produce our themes:
| |

While this method already saves considerable time, it presents some limitations:
Themes often encompass a wide range of properties beyond just colors. If we were to modify the Bootstrap theme, for instance, composing a mixin using the preceding structure would lead to maintainability issues and reduced code readability. Furthermore, this approach deviates from Sass best practices, such as directly embedding hex color codes within the mixin.
Structuring a Styles Scheme with Sass Maps
Leveraging maps, which function similarly to key-indexed arrays, enables us to establish a more semantic and comprehensible set of styles for our theme, improving maintainability and clarity for fellow developers. While lists offer an alternative, maps, with their self-explanatory keys, prove more suitable for this purpose.
Our new approach employs a nested map:
| |
To access each section within our theme-1 scheme and its corresponding sub-maps, we utilize the @each directive to iterate through them:
| |
$section will capture the key of the current section, and $map will retrieve the nested map associated with that key.
We can then access individual properties within each map, such as the background (bg) property, using the map-get function:
| |
By combining our new mixin, based on this map structure, we can effortlessly create numerous themes:
| |
Note the use of the @if directive to apply distinct properties for sections other than buttons.
| |
This structure allows us to introduce specific properties or even rules for certain sections, or differentiate between keys holding a single value and those containing a nested map.
Our theme can comprise multiple maps utilized by various mixins applied throughout our stylesheet. The complexity of the base layout and our preferred approach will determine the extent of this structure.
Further Enhancements
Sass provides built-in functions to further streamline our workflow. For example, hsl functions like lighten or darken can dynamically calculate colors, such as the color of a button on hover.
We can modify the button code to lighten its background on hover, irrespective of the original background color, eliminating the need to define an additional color for this state.
| |
Furthermore, integrating Sass modules can significantly enhance code readability and scalability. Each theme map can reside in its own module and be imported into the main stylesheet.
| |
This approach necessitates organizing the modules within the project as follows:
/ ├── _theme-1.scss ├── _theme-2.scss └── _theme-2.scss
To delve deeper into leveraging Sass for DRY (Don’t Repeat Yourself) CSS principles, refer to Toptaler Justin Brazeau’s insightful article, “Sass Mixins: Keep Your Stylesheets DRY.”