Intro to CSS Animations
Hello world! π
Noel Cothren πΎ @noeldevelops πΎ noelcothren@gmail.com
https://github.com/noeldevelops/css-animations-slidesWhy CSS? β
1. Simple, yet powerful.
All theΒ CSSΒ everΒ written:
selector {
property: value;
}
CSS Zen Garden Resilient Web Design
2. Performance
CSS Animations are offloaded to GPU, so keep things off the main thread (Web Animations API does mimic this). "Modern browsers can animate four things really cheaply: position, scale, rotation and opacity."
3. Principle of Least Power
Q: Why Animate? πΊ
A: Better UX.
- It's a communication aid.
- Users expect it.
- It builds your brand and your site's personality.
"Animated user interfaces can be powerful β so powerful that they offer a competitive advantage to their products, regardless of platform." Rachel Nabors
Easy, lazy animations
Go from point A to point B using transitions & leverage browser's π§ to figure out the in-betweens.
Syntax:
transition: [property][duration][delay][easing] ... ;
We'll attach the change to a change in the element's state. Here's an example:
Hover Me
a.example-link { color: aquamarine; }
a.example-link:hover { color: orangered; }
Ease-y upgrade: customize the timing function using dev tools or a site like cubic-bezier.com or easings.net
Make it Hot with Pseudo Elements
Now we're cookin' with gas π₯
Hot Button π£Hey - don't put important content in a pseudo element! It's not real, it's pseudo.
Syntax ( two colons is modern :: )
.button:hover::before {targets before pseudo element on hover}
.item:hover .info {targets element with .info class when you hover on .item}
Keyframes for Complex Animations π
MDN Web Docs
.animated {
animation-name: bounce; /* from your keyframes */
animation-duration: 4s; /* s or ms */
animation-iteration-count: 10; /* infinite is an option */
animation-direction: alternate; /* or normal */
animation-timing-function: ease-in-out; /* remember cubic-beziers? */
animation-fill-mode: forwards; /* or backwards, both, none */
animation-delay: 2s; /* or ms */
}
.animated {
animation: duration | timing-function | delay |
iteration-count | direction | fill-mode |
play-state | name;
}
A Few UI Animation Best Practices π
- Don't abuse your animation powers - only write animations that are necessary and useful to our users
- Don't conflict with users expectations (e.g. a banking form that "bounces" will be distrusted)
- "Performance and accessibility goals affect all aspects of our design work, and animation is no exception there." Val Head for Smashing Magazine
- Avoid animating properties that will trigger layout or paints.
- Make sure your animations aren't disturbing users with vestibular disorders or low motion preferences. "Prefers reduced motion" media query in Chrome 74
Resources π
- All things CSS Animation from Donovan Hutchinson
- Motion in popular design systems:
- In Depth on Bezier Curves by Smashing Mag
- Still the best article on CSS Animations Performance from HTML5 Rocks
- Sarah Drasner's favorite CSS Animations on Codepen.io
- HTML+CSS Slides and Emoji URLs by Chen Hui Jing
- Emoji favicons and other cool CSS things by Lea Verou
- Easy code snippets with syntax highlights by Tania Rascia