Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
- Understanding delay and fill-mode
- Dictating an animation's direction
- Using easing
- Adding keyframes
- Looping an animation
- Chaining multiple animations on one element
- Setting up the HTML structure
Skill Level Intermediate
Throughout this course we've been dealing with animations mostly in isolation. In our real world projects, however, our animations are going to have to mingle with all the other code and assets in our projects. First, let's take a second to talk about vendor prefixes. Now I won't remind you yet again why you should use them, but let's look at a couple of ways that make dealing with them a whole lot easier. All of these vendor prefixes do add an awful lot of length to your code, especially when it comes to animations. Luckily, you don't have to type it out all yourself. If you're already using a CSS preprocessor like Sass or LESS, these both have automated ways to take care of adding the prefixes for you.
And actually, if you don't use Sass or LESS already, that feature alone is worth checking them out for. If you prefer not to work with preprocessors, there are still options to save yourself some headaches. You can set up some saved code snippets in your text editor, or you could download a version of something like Prefixer, which you can actually install and use with your text editor if it's one that it has an extension for. There are also online tools like CSS3Please and the CSS3Generator. Both of these tools will let you preview your CSS in the browser and then copy and paste all the prefixes used into your own style sheet.
We'll look at these in a little more detail later to explain exactly how to use them in your work. Any of these tools I mentioned will help keep your prefix code more manageable. At the time of this recording, many of the latest modern browser versions are starting to support animation properties without prefixes. For example, IE 10, Firefox 16 and higher, as well as Opera 12 and higher support unprefixed animation properties. As this trend continues, you may be able to drop vendor prefixes for animation sometime in the near future for certain projects. But we're not quite at that point yet.
The question of where to put your CSS animation code comes up a lot. Your current workflow and the project in hand will have a lot to do with this, but here's what I would suggest. Putting all of your animation code in a separate CSS file, maybe even one called animation.css is an extremely easy way to find your animations any time you might need to edit them. It also keeps all the multiple browser prefixes out of sight and out of the way when you don't need them. An animation CSS file might look a little something like this. You can see that the animation properties have been assigned to specific HTML classes here at the top of the file.
Last but not least, what happens when a browser that lacks CSS animation support encounters your CSS animation code? It simply ignores it. So in the case of older browsers which don't support animations at all, your animations will simply be ignored. This is no big deal if your animations are part of the behavior layer and don't hold any imperative content that isn't otherwise available. However, if it's imperative that your animations are seen, even in these older or less capable browsers, you'll need to create a fall back method. Tools like Modernizr allow you to detect support for specific CSS features and provide alternate content or options if they're not supported.
Briefly, to use Modernizr you go to the download section and select the features you would like to detect for. In our case, this would be CSS animations, and you can select any other properties you may be interested in. Then you scroll down and hit the Generate button to download your own custom version of Modernizr that's only going to work for the properties you indicated. Then you can download your Custom Build. Once you have your Custom Build of Modernizr downloaded, you would include it in the head of your HTML document, something like this.