Automagic Retina-Ready Sprites with Compass and SASS
Sprites are a classic trick for speeding up a web page load time. Why make a request for dozens of dinky images when you can grab one sprite sheet and position it accordingly with CSS? Traditionally though, making sprites by hand is a tedious pain requiring Photoshop, pixel counting, and copying/pasting. Ew. And nowadays with all these higher resolution “retina” displays floating around, that means double the spriting is required.
Luckily for us Ruby users, the Compass CSS Framework gives us the ability to automatically generate sprite sheets in our Sprockets asset pipelines. But how can we handle retina versions without? Some Googling yielded this gist which I SASS-ized and modified slightly to handle retina background size calculations and devices with pixel ratios of 1.5. First, make sure
sass (or their
-rails versions) are in your
Gemfile, dump all your normal sprited images in one folder and @2x versions in another.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
Just like that – sprites are compiled, dimensions and positioning are calculated with Ruby, and its all dumped to a CSS file with media queries for denser displays. Photoshop can stay closed and reserved for more exciting things like meme-ing images.