automatically css sprite.
I have written posts “css sprite best practices” to introduce the idea that the css_sprite gem follows.
What css_sprite does?
css sprite generates css_sprite image and css files automatically for you follow the conventions as follows.
|images under css_sprite directory||class name in css_sprite css|
|widget/icons/twitter_icon.png||.widget .icons .twitter_icon|
css_sprite directory is the directory whose name is "css_sprite" or
"css_sprite" suffixed under
css_sprite image is the image file automatically generated under
css_sprite css is the css file automatically generated under
css_sprite depends on the
css_sprite also depends on the
optipng tool as default image optimization, but you can use any other image optimization tool, check the Configuration section.
If you use the default optipng tool, please make sure it is successfully installed on your system.
install css_sprite as a gem:
gem install css_sprite
1. create a directory whose name is css_sprite or ends with css_sprite (e.g. widget_css_sprite) under
2. if you css_sprite in rails2 projects, you should add css_sprite task in Rakefile
If you use rails3, you can skip this step too
config/css_sprite.yml, it is not necessary by default.
4. start css_sprite server
5. put any images which you need to do the css sprite under the css_sprite directory, then you will see the automatically generated css sprite image and css files.
6. include the stylesheet in your view page
<%= stylesheet_link_tag 'css_sprite' %>
You can stop the css_sprite server by
You can restart the css_sprite server by
Or you can just do the css sprite manually by
There is no need to do any configurations by default for Rails 3.1 or higher projects. If you want some customizations as follows, you need to define
Example for rails 2.x or 3.0
engine: css image_path: public/images stylesheet_path: public/stylesheets css_images_path: images source_images_path: app/sprites
Destination Image Format
css_sprite saves the css sprite image as a png file by default. You can change it to gif or any other format like
background: url('css_sprite.png') no-repeat by default, if you prefer
background: asset-url('css_sprite.png') no-repeat, you can change it as follows
css_sprite generates css.scss file by default. You can change it to pure css or sass as you like.
engine: css engine: css.sass
css_sprite allows to do optimization to generated css_sprite.png, you can use default optipng with optimization level 2.
Or you can change it to any image optimization command.
optimization: optipng -o 7
For css or scss
suffix: button: | text-indent: -9999px; display: block; cursor: pointer; font-size: 0; line-height: 15px; border: 0; outline: 0; icon: | text-indent: -9999px; cursor: pointer;
engine: sass suffix: button: | text-indent: -9999px display: block cursor: pointer font-size: 0 line-height: 15px border: 0 outline: 0 icon: | text-indent: -9999px cursor: pointer
engine defines css.scss (default), pure css or sass file to generate.
suffix defines the customization styles for specified images.
The customization above means if your image filename is button suffixed (e.g. post_button.png), the corresponding class .post_button has the additional style with (outline: 0; border: 0; and so on), if your image filename is icon suffixed (e.g. twitter_icon.png), the correspondiing class .twitter_icon has the additional style with (text-indent: -9999px; cursor: pointer)
css_sprite follows the conventions that images are under
app/assets/images directory and css files are under
app/assets/stylesheets, but you can change them.
image_path: public/images stylesheet_path: public/stylesheets source_images_path: app/sprites
By default, source_images_path and image_path are
I built an example
images are under
generated css sprite image is at
genereated css file is at
$ cd example $ rake css_sprite:build $ open index.html
Copyright (c) 2009 - 2013 [Richard Huang] firstname.lastname@example.org, released under the MIT license