- View Offline
Want to create your own icon fonts? Check out James' companion course, Creating Icon Fonts for the Web.
- Finding icon fonts
- Ensuring consistent styling
- Exploring class-based solutions for deployment
- Deploying with the data-icon attribute
- Aligning icons
- Animating icons
- Styling multicolored glyphs
Skill Level Intermediate
In this exercise, I want to take a look at extending the styling of our icon fonts past just sort of basic visual styling into something a little bit more complex, like animating it through CSS. A lot of icon sets have icons that are designed to animate. Most notably, loader icons like the one you are seeing here at the bottom of the page. So what we're going to do is we're going to animate our loader icon by making it spin. And thus, simulate the loading of a page or element. Or widget or something like that. Now, I want to point out that as we are going through and styling this, I want you to notice that CSS animation support is pretty good right now.
However, you do have to work hard sometime to make sure that it works correctly across multiple browsers. Okay so here we are in the same file within the code editor. This the animate.htm. I want to scroll down and show you guys kind of how the icon is being displayed. It's right there in the paragraph. And we're not doing anything different from the way we normally display our icons, so we're still using data icon attribute, we're still using aria hidden. I've got some alt text there that of course we're hiding for accessibilities sake.
So really we don't need to make any changes as far as our deployment strategy goes. Okay I'm going to switch over to my CSS and I'm going to scroll all the way down to the bottom where we have our icon font styles. Again so this is just sort of our global icon style. The first thing that I want to do anytime that I'm doing animation Is I have to set the animation up before hand. And we do that using an at key frames rule. So I'm going to type in at key frames. And then you have to name your animation, whatever you want to call it.
And I think naming it loading makes a little bit of sense, since that is what we're simulating. And then inside of that, there are a couple of different ways this syntax works. Essentially You just basically say, hey, it looks this way at this state, and then this way at this state. And you can have as many states in there as you want, so you can create steps for your animation, or key frames, if you will. So it's a pretty cool way of working. I'm just going to go from 0% of the animation done, and then inside of that. And I always like sort of typing in the opening and closing curling braces to start, because when you start nesting them like that it's really easy to get lost.
Okay so I'm going to do a transform here and I'm going to transform this by rotating it. And I'm going to rotate it zero. Meaning it looks exactly the way it did when it started. So when it's at zero It's essentially at the start position. Now again, I love copying and pasting. So I'm going to copy this. Paste it. And this one is going to be 100% of the animation, and that's the animation being finished. So it's going from zero, start. To 100% finished and if we wanted to we could have multiple sets here, you have zero, 50, 100, anyway that we wanted to do this.
Okay, so since we wanted to spend all the way around we're going to give it a 360 degree rotation and for that transform value We do 360 and then DEG for degrees. All right now. Let me save this. Now at this point, animating it should be pretty easy. All I've go to do, is go down into the element that's controlling my icon and call the animation. So I'm going to go right down after line height and I'm going to type in animation. And the name of this animation is loading. Remember, we named it right up top.
So I'm pulling the name from there. Then we have to say how long we want the animation to take. And I want it to complete its rotation in one second, which will be pretty fast. How many times do I want it to rotate? I want it to rotate an infinite number of times. I don't want it to stop. So you can say, you know, we want it to rotate twice, or three times. In this case I'm going to say infinite. And then we have to tell it what kind of timing we want for the animation. TIming is basically things like easing. For easing you can have things accelerate at the beginning and slow down at the end. Slow down at first and then accelerate towards the end.
I just want linear because I want one nice constant speed the whole way through. Now if I save this, I go into one of my browsers and refresh it, I get absolutely zilch, nothing. Well, we've gotta remember what type of element we're inside of. Right now, we're in a span, which is in-line element. And animation is not going to work on in-line element, so I'm going to type in display. Inline_block. Save that.
Preview it, okay great. Now it's, it's spinning that's exactly what I wanted. So one of the things we have to remember is that if you do have an in line element, and icons are typically inline depending upon where you're using em. You at least have to make it into an inline block element in order to actually transform it, so that has everything to do with the properties that you're actually trying to animate, in this case, we're trying to animate the transform property. Okay, now you may have noticed that I was testing that in Firefox and throughout this entire course I've been testing it in Chrome, right? So what happens if I go over to Chrome, with the same page, and test it? Absolutely nothing.
And the reason for that, is the animation syntax has not been unprefixed yet. What does that mean? It means that a lot of browsers support animation, but they only support it through vendor prefixes. And Web Kit is certainly one of those. So, what I'm going to do, is I'm going to take my animation right here, I'm going to copy that. And then I'm going to paste a copy of it before that. We now have to add another version of our animation, this targeted specifically to WebKit. We do that by saying at _ webkit-keyframes.
So we've gotta change that. Then we have to remember that the transform property is also prefixed inside WebKit browsers. So now I have to prefix that and I have to do that twice, which makes me unhappy. There we go, we'll save that. And then finally, we're not quite done yet. I still go down and I have to call the animation again. So I'm going to copy this Paste it. Then I have to prefix that with webkit-animation. I'm going to go ahead and save that now, because I know this will do your heart a world of good.
There are also prefixes for Opera, there are prefixes for Mozilla for older versions of Firefox, and I think there are prefixes for IE. I'm pretty sure there are. All right. So I'm going to save that, go back out to Chrome, refresh and now I have my animated icon. So although animation can add a lot of visual punch to our using icon fonts, one of the things you want to be very careful with it is to make sure that you're testing it across multiple browsers and that you're really familiar with the syntax, so that you'll know when vendor prefixes are required.
And when to provide a fallback in case animation is not supported.
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.