Homepage

Generate Icon Classes from Sprite Using SASS

While we mostly use sass for it’s variables, nesting and mixins, the language itself is way more powerful. A lot of features may not be used on your everyday site, but there are times when they come in handy. Let’s take a look on how one would approach automatic class generation for any grid based sprites and how this could vastly shorten your code.

While we mostly use sass for it's variables, nesting and mixins, the language itself is way more powerful. A lot of features may not be used on your everyday site, but there are times when they come in handy. Let's take a look on how one would approach automatic class generation for any grid based sprites and how this could vastly shorten your code.

A little heads up: this won't be very useful with shorthand, no-braces sass syntax (.sass) since it doesn't support multi-line lists. For this reason all code below will actually be SCSS.

What we will be making

We will write a SASS mixin that will take a list of icons we specify and then create a set of classes for us to use in HTML. The purpose of this exercise is to learn more about SASS - it's built in functions, constructs and advanced use of variables.

Let's go.

Basic information about sprite

This is the simplest use of SASS variables - holding single value. First we create a configuration section for our generator.

tsx

With this configuration we are letting SASS know that our sprite is located in images/sprite.png and is made up of 20px by 20px grid. We also want all our classes to be prefixed with custom-icon--, ie. custom-icon--settings.

General icon styles

Now that we know how generated classes will be named, we apply some styling to all icons:

tsx

Styles for single icon

We are going to use a small mixin, that will take $row and $column at which the icon is positioned in sprite and appropriately move icon's background. To simplify, we assume that rows and columns are 0-indexed.

tsx

Because each cell of our grid has equal height and width, we can get away with this fairly simple code. If that was not the case, like when we would need to specify custom offsets for each icon, we could refactor this logic into a function, for example:

tsx

List of icons

Variables in SASS can contain lists of values separated by either spaces or commas and optionally surrounded by parenthesis. Thanks to that we can create a simple data structure that will hold information about an icon:

tsx

Extending this idea let`s us create a list of similar data structures that would describe all icons we want to define:

tsx

Please note that in this case, the outermost parenthesis are not optional.

Creating icons

We'll write a snippet that will iterate over all icons in $icon-list, pluck variables from it, create a class and call icon mixin to style it.

tsx

We need to use built-in nth function to tell sass which value form list we want, because, sadly, there are no associative lists/arrays yet.

Wrapping it all together

SASS lets us refactor a lot of repeatable code into a single list. Utilizing lists, nth, @each and mixins we were able to significantly reduce the number of code lines. Still, there is a lot room for improvement - we could account for different size icons, not square grid, scoping variables to reuse the code inside @each to create many sets of icons and so on.

Main point of this post is that SASS is so much more than just CSS that allows nesting and that we shouldn`t miss out on these exciting features.

Complete listing of code:

tsx

Let’s Create a Great Website Together

We'll shape your web platform the way you win it!

More posts in this category

  • February 05, 2025 • 10 min read

    API-first CMS: What Options You Have (Web Dev Agency's Take)

    According to the 2024 State of the API Report by Postman, 74% of organizations are adopting API-first strategies. This statistic isn’t just impressive—it signals a major shift in how businesses operate. While API-first approaches have long been a staple in software development, they're now reshaping content management as well. More and more companies are realizing that traditional CMS platforms can't keep up with the demand for flexibility, speed, and seamless integrations.

    READ MORELearn more about api first cms
  • January 23, 2025 • 15 min read

    Best CMS for SaaS: Top Cloud-Based Solutions

    Choosing the right Content Management System (CMS) is a critical decision for your SaaS business. Your unique needs require solutions that are not only flexible, scalable, and user-friendly but also tailored to meet the demands of a fast-paced, customer-focused industry. A CMS should simplify your workflows and help you deliver personalized, high-quality digital experiences.

    READ MORELearn more about best cms for saas top cloud based solutions
  • December 12, 2024 • 10 min read

    We Picked the Best (Headless) CMS for Vue

    Picture a digital experience where content effortlessly flows across platforms, development is agile, and performance is unmatched. By combining the power of Vue.js, a progressive JavaScript framework, with a modern headless CMS, you can achieve all this and more.

    READ MORELearn more about headless cms for vue