Navigation

Recycling and animating icons with CSS

CSS, How To, Web Design | Sep 14, 2011

In the not so distant past, we were learning about revolutionary techniques to save HTTP Request and KBs alike through the use of image sprites. These sprites consisted of tens or even hundreds of icons arranged in an image file that was later spliced and served in a variety of ways throughout a website.

We’ve made good use of the technique, and virtually every site concerned with scalability employs it.

Thanks to the advent of CSS3’s Transform and Transition properties, we can take this a step further, and using a few concise lines of code, transform base icon templates into new icons for future use – and even throw animation into the mix for an added bonus!

The technique is as simple and intuitive as was image sprites, and allows use to rapidly deploy new icons without ever having to alter the image sprites.

 

Recycling icons with CSS

Take a look at this sprite taken from the jQuery UI library. As you browse through, you’ll notice that many of the icons listed here are actually variations on base templates. A single icon could be represented in a dozen different ways, and placed in the same file. Many icons are literally just rotated versions of their parents. The good news is while utilizing CSS we can employ the exact same technique without having to include the variations in the image sprite.

From the example above, we can take a single icon and recreate it for our own purposes, say a simple chevron from the second row down. With the transform property, we are able to rotate this chevron 45deg, 90deg, 180deg, obviously and indefinitely to create many different forms from the same template.

Base template (up arrow):

The following code will pull the chevron facing up from the image sprite, and will serve as our base template.

.icon {
 width: 20px;
 height: 20px;
 display: block;
 background: url('sprite.png') no-repeat -20px 0;
 }
upArrow

Create right arrow

Transforming our arrow 90deg will point the arrow to the right, as show below:

-webkit-transform: rotate(90deg);
 -moz-transform: rotate(90deg);
 transform: rotate(90deg);
rightArrow

Create top-right arrow

Rotate it just 45deg and you get a nice little top-right corner arrow:

-webkit-transform: rotate(45deg);
 -moz-transform: rotate(45deg);
 transform: rotate(45deg);
topRightArrow

It’s that simple. Using this method, we can start with a simple two icon sprite, and with very little effort create six times as many icons for use in our interface, which of course is just the beginning of what can be done.

A few transforms, some fancy positioning, and our icon family has grown quite a bit!

 

Adding animation to the mix

For a killer experience, we can add animation into the mix. Not only will we transform the icons, we’ll transition them to make the transformation visible to the user. Lets take a look at another example, starting with the plus sign seen above.

.icon {
 width: 20px;
 height: 20px;
 display: block;
 background: url('sprite.png') no-repeat 0 0;
 }
plusIcon

One easy 45deg rotation will transform our plus icon into a handy close icon.

.icon {
 -webkit-transform: rotate(45deg);
 -moz-transform: rotate(45deg);
 transform: rotate(45deg);
 }
closeIcon

Now that our transformation is working correctly, we can add a transition into the mix. Imagine you have a feature on your site to share the current page through a variety of social networks. Clicking the plus icon will open the list of share options, and while the list is opening, the plus transitions into a close icon through a subtle animation. The best implementation I’ve found for this is on FontBook’s iPad app. Check out their implementation:

FontBookiPad

It’s stellar. Let’s take a look at how to make this beauty come to life. Start by using our plus icon created above. To animate it, simply add the transition property into your icon. In our transition, we specify the property (transform), the duration (0.2s), and finally what timing function we want to use (linear).

.icon {
 width: 20px;
 height: 20px;
 display: block;
 background: url('sprite.png') no-repeat 0 0;
 -webkit-transition: -webkit-transform 0.2s linear;
 -moz-transition: -moz-transform 0.2s linear;
 transition: transform 0.2s linear;
 cursor: pointer;
 }

Again, it’s that simple. Not only can we create new icons for our library with only CSS, we can animate and give life to any particular element!

 

Using opacity for more variety

The final piece of icon recycling comes to play in the form of the opacity property. Duplicating your core icons for black and white will allow you to generate an infinite number of shades / variants for use all over your site or application.

A four-image variant (as seen below) of the sprite above could easily be used to create a dozen times as many icons, and by increasing or decreasing the opacity you can place them wherever needed, and still have them look great.

fullSpriteInverted

 

It’s time to go green: recycle with CSS

As CSS3 has gained traction, my copy of Photoshop CS5 has started to gather dust, and for good reason! This technique of recycling your icons allows you to continuously deploy new versions and variants to your interfaces without having to open up source files and add cumbersome icons to ever-expanding sprites.

Maintenance time goes down, and your time spent reading books like the 4 Hour Work Week goes up! It’s all gold.

Of course the most obvious downside to all this is browser support, however, with the recent push by, well, everyone to use modern browsers, we will be able to take advantage of new and exciting progressive techniques. Feel free to browse some examples of this technique.


Caleb Ogden is an accomplished designer with a passion for combining great design with web standards to create stellar experiences online. For more, you can visit his website, and follow him on twitter @calebogden.

What are some other ways you’ve been able to recycle your website’s assets?

Share this post
Comments (no login required)
  • SampeiMihira

    very interesting ! Many thanks. Reading thi kind of post I always be able to increase my knowledge about designers world.

  • http://claytonjohnston.com/ The CJ Team

    Interesting post, will try this out someday :)

  • Virtual Agents

    I try this out animating icons with css. It’s pretty cool having this advantage of new and exciting progressive techniques. Great idea I’ve make good use of your technique. Thanks!

  • http://www.facebook.com/profile.php?id=1816794006 Karol Niepsuj

    strong

  • Website designing Hyderbad

    It’s been a pleasure reading your blog. I have bookmarked your website so that I can come back & read more in the future as well. Please do keep up the quality writing.

    • http://twitter.com/calebogden Caleb Ogden

      Thanks! Appreciate it.

  • http://3circlestudio.com/ Justin Carroll

    GREAT reminder.

  • Pêşmerge Morad

     Awesome Post :)

  • http://www.facebook.com/jinsshah Jinal Shah

    doesnt work with any versions of IE

    • http://twitter.com/calebogden Caleb Ogden

      Yes it only works with the vendor specific rules setup there. It’s a progressive technique, and works for progressive browsers.

      Opera does support this technique, you can add support by using the vendor specific tag:

      -o-

  • http://twitter.com/SpinaliLuca Spinali Luca

    Great, thanks

  • http://www.perfect-icons.com Adam Chen

    Thanks! This will surely help me making more web icons,
    icons for developer and toolbar icons. 
    Now let me try some of this and see how it really works.  I’m also planning to learn css3, the latest
    coding system. 

  • http://www.toolbar-icons.com/index.htm?ref=ch Chris Hubber

    I’m s interested reading and learning this codes. Now I know
    how easy to make an icons. My toolbar icons can now be upgraded by myself, thank
    you so much for posting this few lines of codes. 

  • http://www.aha-soft.com/stock-icons/high-resolution-app-tab-bar-icons.htm?ref=ch Andrei Johnson

    Cool codes! Now I can reassemble
    my hundreds of icons for iPad. This makes the designer world more profitable
    with new exciting techniques. A big thanks to you bro!