Video: Organizing animation code for semantics and fallbacks

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.

This course presents a short series of CSS animation techniques, such as looping, playing, and pausing, and puts them together in a small project: an animated infographic. Author Val Head also addresses using CSS preprocessors, adding transitions, handling vendor prefixes, and understanding the best uses for CSS animations. Plus, discover how to measure the performance and current level of browser support for CSS animations and how we can expect the technology to change.

Organizing animation code for semantics and fallbacks

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 waysthat 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 automatedways 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 aversion of something like Prefixer, which you can actually install and use with yourtext 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 pasteall 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 startingto 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 sometimein 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.cssis 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 classeshere at the top of the file.

And these classes can be used to apply the animation to various elements.Each set of properties includes all the necessary vendor prefixes too.The second part of the CSS animation file contains all the @keyframe rules.Splitting the animation properties and the @keyframe rules like this isn't required,but it makes for a nicely organized and easy file to navigate.Defining your animation properties and individual classes like this file shows at the top,can also be very useful to get more mileage out of your animations.You can apply these classes dynamically with JavaScript for even greater flexibility.

Last but not least, what happens when a browser that lacks CSS animation support encountersyour 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 holdany imperative content that isn't otherwise available.However, if it's imperative that your animations are seen, even in these older or less capablebrowsers, you'll need to create a fall back method.Tools like Modernizr allow you to detect support for specific CSS features and provide alternatecontent 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 versionof 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 headof your HTML document, something like this.

And then you would just create a simple check for Modernizr.cssanimations.And then within that, you can select what to do if CSS animations are supported or not.For example, if they're not supported, you may include alternate CSS or perhaps a JavaScript fallback.And if they are included, you can be pretty happy and just go on with your CSS animations.For a more detailed look at how Modernizr works, check out their extensive documentation section.If supporting older browsers is vital, you should consider creating your animations usingjQuery or another JavaScript only method instead.

Personally, I find it's more work than its worth to maintain both a CSS and JavaScript version of the same content.In this situation stick with JavaScript for now and consider moving to a CSS animationsolution in the future, when there's a wider support in your audience.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.