SpyreStudios

Web-Design and Development Magazine

  • Design
  • Showcase
  • Inspirational
  • Tutorials
  • CSS
  • Resources
  • Tools
  • UX
  • More
    • Mobile
    • Usability
    • HTML5
    • Business
    • Freebies
    • Giveaway
    • About SpyreStudios
    • Advertise On SpyreStudios
    • Get In Touch With Us

When To Use JavaScript vs. CSS3 for Animation

January 26, 2015 by Jake Rocheleau

Animated web content has been around for years in the form of GIFs and Flash content. But as those older methods fall to the wayside newer techniques have grown in their place. Back in the early 2000s JavaScript was widely-regarded as the best solution for animated web content. Then an update to CSS created the CSS3 specification with all sorts of fancy new properties.

Among these new properties are some amazingly powerful codes for animated content on the page. It’s now possible for frontend web developers to craft a dynamic website using only HTML and CSS code. But is this really the most viable option?

featured javascript codes sample

I’d like to cover a few talking points related to website animation comparing the differences between JavaScript and CSS3. If you’re a developer then it’s worth learning both languages to empower yourself and your projects. If you’re a designer it’s still good to understand these concepts to better communicate your ideas with developers. Web animation is most certainly here to stay – the only question is, where will it advance from here?

Speed & Efficiency

By skimming this wonderful post about CSS vs JS animation speed it’s easy to see that CSS3 is a preferred natural option for supported web browsers. Regular-old JavaScript is also a great option if you want to hardcode all of that content. But it turns out that jQuery itself is a little slower – not super noticeable but still there.

jQuery wasn’t created to be an animation library. In fact, a secondary library jQuery UI was created with some animation functionality, but that still wasn’t the overall goal. I would still honestly suggest using jQuery for simple stuff because it’ll be much more supported in comparison with CSS3 transition effects.

But JavaScript as a whole is tricky business. Unless you have some advanced development skills you’re really gonna need to work hard to get the animations you desire. It seems that CSS3 is growing much faster with both transitions and keyframe animation. The biggest downside is a serious lack of backwards compatibility.

So when it comes to speed I would say both CSS3 and typical JavaScript are good options. It really depends how much browser support you need. In fact you may even try out a pre-built library for these effects. For CSS3 there’s the very popular Animate.css while JavaScript has Velocity.js(among others).

Levels of Control

From a standpoint of objective control JavaScript seems to have greater jurisdiction over the municipality of Animationville. With JavaScript it’s possible to create any type of easing effect paired with any element on the page. Rotation, scale, explosions, anything you can imagine is possible with enough code.

But CSS3 is enticing because the animation properties are added directly onto the element.

css3 css coding development graphics

The biggest downside with CSS3 is a lack of trigger events. JavaScript contains a panoply of events such as mouseover, right-click, click-and-drag, or even targeting specific keypresses on the keyboard. CSS3 animation is handled through a change in class or pseudo-class. This relies on selectors like :hover or :active to perform the animation.

So when it comes to absolute power I would have to say JavaScript offers much more control. This is simply because JavaScript has been around longer and has been gestating for years with updates and code releases. CSS3 is still a newer invention that has been gaining a lot of momentum, but still has a long way to go.

Practical Usage

Many times you’ll find animation effects online that just seem ridiculous. It’s really cool to build an animated printer or snowfall effect – but this kinda stuff doesn’t have much practical value. Web design is always about the user first. Therefore your animation should enhance the user experience and not just add pretty frills onto the page.

So what does have value? Well for starters you might try loading animations like spinners and animated graphics. These elements denote when a page is in the middle of loading or preloading resources. You might also want to create dynamic effects in a carousel or perhaps a contact form with label hints for each field.

Getting down into the meat & potatoes there is a lot of pragmatic stuff you can build with animated effects. For example, dropdown menus and tabbed widgets are built upon hidden content. When a user interacts with the menu or tab links the invisible content is swapped into view.

css3 animated dropdown accordion menu

My honest recommendation when it comes to animated effects is to keep it all simple. If you’re gonna go overboard make sure that it really fits into the layout style. For example, a cartoony layout can get away with exaggerated animation effects to replicate the squash and stretch effect found in cartoons. Try out lots of different ideas and see what type of animation you like the most.

CSS3 Keyframes

If you’re adamant to learn in-depth CSS3 animation then you’ll have to get into keyframing. The generic transition effects can be useful for simple stuff, but they really just scratch the surface. Keyframe animation gives the developer so much more control over which elements are moving, how they’re moving, and where they end up.

CSS-Tricks has an excellent guide on keyframe syntax which I recommend you attempt to memorize. There are a few browser prefixes which can be difficult to retain in your head, so alternatively you might store this code as a snippet. Also check out the Mozilla @keyframes reference documentation for in-depth info.

mozilla developers network css3 keyframes

Frontend development has advanced so quickly over the past few years. I never would have imagined something like keyframes being implemented into CSS when I first began studying web design in 2005. So much has changed and if you want to keep up with the times, you’ll have to at least try to change your methods as well.

Routines for Practicing Frontend Development

Additionally if you want to push your CSS skills to the next level it’s worth learning about SASS. That stands for Syntactically Awesome StyleSheets and it basically allows developers to compile CSS code much like a programming IDE. If you take the time to learn SASS it will save you hours of copying & pasting code. The Animate.css library even has a SASS version to give you a starting point.

Final Thoughts

So between CSS and Javascript, which is the better choice? As with all these fascinating conundrums the answer is kinda lame: it really depends on the circumstances. My personal preference is using CSS3 for nonessential animation like a button hover effect. JavaScript feels a bit cleaner for integrated animation like dropdown menus and carousel rotators only because of the wide browser support.

Ultimately it is your decision and there’s no wrong answer. Take this as an exploration into modern web animation and mix it up with each project. Don’t be afraid of pushing boundaries to see how far you can go.

Author: Jake Rocheleau

Jake is a creative designer, illustrator, and web developer. He frequently writes articles involving new-age design concepts and freelance management skills. You can find him in Google or follow his tweets @jakerocheleau

Filed Under: UX Tagged With: animation, css3, javascript, web design

SpyreStudios © 2019