Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Organizing animation code for semantics and fallbacks

From: CSS: Animations

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.

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.

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.

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 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.

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 using jQuery 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 animation solution in the future, when there's a wider support in your audience.

Show transcript

This video is part of

Image for CSS: Animations
CSS: Animations

29 video lessons · 11713 viewers

Val Head
Author

 
Expand all | Collapse all
  1. 4m 47s
    1. Welcome
      41s
    2. What you should know before watching this course
      58s
    3. Vendor prefixes and browser support
      2m 11s
    4. Using the exercise files
      57s
  2. 28m 16s
    1. Animation basics
      6m 32s
    2. Exploring animation delay and the fill-mode property
      6m 6s
    3. Working with animation direction
      4m 49s
    4. Understanding easing
      8m 47s
    5. Challenge: Adding keyframes to our animation
      49s
    6. Solution: Adding keyframes to our animation
      1m 13s
  3. 47m 37s
    1. Creating a seamlessly looping animation
      6m 27s
    2. Animating an element into place
      5m 43s
    3. Playing and pausing an animation on hover
      6m 20s
    4. Animating 3D transforms
      8m 6s
    5. Preparing a sprite image for animation
      4m 55s
    6. Animating the sprite image with steps
      5m 34s
    7. Chaining multiple animations on one element
      8m 2s
    8. Challenge: Adding a third animation to the chain
      46s
    9. Solution: Adding a third animation to the chain
      1m 44s
  4. 19m 54s
    1. A preview of what we'll be creating
      1m 10s
    2. Setting up the HTML structure
      1m 48s
    3. Adding the header animation
      5m 57s
    4. Animating the data
      6m 8s
    5. Adding interactivity with JavaScript
      4m 51s
  5. 12m 44s
    1. Why and when to use CSS animations
      4m 50s
    2. Organizing animation code for semantics and fallbacks
      4m 51s
    3. Crafting effective animations
      3m 3s
  6. 3m 0s
    1. Helpful online tools for creating and building animations
      3m 0s
  7. 24s
    1. Next steps
      24s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

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.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Animations.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

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

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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:

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:

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

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.