Sprite sheets images building for web solutions is an ancient technique, but web sites without any sprite sheet are ubiquitous. I think one of the reasons for that is missing knowledge about creating those. Today I would like to share with you how simple CSS sprite sheets may be.
We are going to use SCSS and Compass framework.
Compass Framework
The official compass documentation is here. What is the compass framework? It is a package of really useful mixins and methods which makes your frontend development process faster. For example, all you need to create a sprite sheet from a directory of images is that:
I’ll show you how to use that later in this article. There are two requirements: ruby and compass gem. You should install ruby first, and then you can install Compass like that:
gem install compass
Project structure
To make something -> we need a project. That would be our compass project structure for today:
- configs/_compass.scss
- configs/_sprites.scss
Sprites creation scripts, we will back to that later.
- modules/_icons.scss
Here we are going to create our CSS classes with sprites.
- app.scss
The entry point of our SCSS front-end project
- config.rb
How may we run the project?
TIP - by default compass would compile each of .scss files to CSS file with the same name unless its name begins with _ underscore. It is a reason why some files have a name with _ at the beginning.
Create the sprite sheet
In case of our example, we are going to create icons sprite sheets. Two important things before we can start are: images_dir and sprite_load_path configuration variables. You should find them inside your config.rb file. The first one images_dir tells compass where it should store generated images the second one sprite_load_path tells where it should look for images.
Let’s go to the configs/_sprites.scss file and initialize icons sprite sheet:
If you would like to customize one of the images for example add no-repeat to some icon background property you need to put additional arguments to the sprite-map function. Let’s say you have icon image calls arrow.png
Now we can use $icons inside our SCSS blocks.
Compass brings us a few functions/mixins to deal with sprite maps.
sprite($sprite_map, $icon_name)
It puts the background property with correct URL, background position and repeat values.
Okay so now we can create our first sprite sheet packaged icon
Mixins
Good point here is to create some helpers which allow us to use sprite sheets faster without copy&paste same code lines again and again. We can add them inside the configs/_sprites.scss file.
sprite-box-image
One of the most common usages of images in our application will be probably the creation of image boxes. We can create mixin which creates a box image using a given sprite sheet.
Great! now we can use that mixin in an effortless way
Example compiled css:
sprite-bg
The second usage which I have in mind is to add a background to the block. The following mixin helps us with that
Simple example:
Example compiled css:
Automatic generated classes
Compass gives us one great feature - generate CSS classes for given images folder automatically. Let’s say we have a directory with three images: img1.png, img2.png and img3.png. Now we would like to create a sprite sheet that contains those images and then create a class with an image setup for each image.
With compass it is straightforward, we are going to use the … sprites mixin.
sprites($sprite_map, $sprite_class_suffix)
We can use that mixin, but the faster way is to use @import keyword. Let’s say we’ve put following code inside modules/_icons.scss
Example of compiled CSS:
Example of usage in HTML
There is one tricky case here - if you would like to change the prefix you are pushed to use sprite mixin without magic import. One disadvantage here is you need to put an array of images names as the second argument - not ‘all’ option here by default.
Example of compiled CSS:
The last thing, usage in html:
Credits
I use Compass a lot, and it is one of my favourite CSS power-up solutions. I hope the sprites creation will be easy for you after this day :)
The official Compass sprites documentation can be found here: Compass sprites