CSS Animation Entrance Effects in Email
Introducing
CSS Animation
Entrance Effects
|
Entrance effects using CSS animation can add some flair to your email. Unlike more ambitious kinetic email designs that may involve a lot of development time including the production of both kinetic and fallback versions of an email, entrance animation effects leveraging CSS 2D transforms can deliver some pizzaz without a lot of extra effort.
For this post, we’ll cover two kinds of entrance effects, fly-in and pop-in.
Read this article for more background on CSS animations in email including support in email clients.
Entrance Effects
Everyone has seen entrance effects used in slides and presentations where content slides in or fades into the presentation. Although CSS animation support is limited to more modern email clients, these entrance effects are relatively simple to implement and may provide a cool effect to your email depending on your audience.
The key to these examples below is that you can just code your emails the way you’re used to and only need to wrap the elements you want the effect with a div to have them come alive in email clients that support CSS animation.
Fly-in
(Animation above repeats for illustration purposes)
The fly-in or slide-in effect allows your content to fly-in from any direction. We’re going to use the CSS 2D Transform translate()
function to achieve this effect.
Although you can use other methods such as margin and absolute positioning to perform the fly-in animation, translate allows an element to be moved relative to its original position without affecting the elements around it.
With 2D Transforms the element can grow, move or shrink and the elements around it behave as if the element has not moved. This is great since you don’t have to design your emails any differently to accommodate entrance effects.
Note we’re using -webkit-
vendor prefixes because as of this posting that is the only kind of animation declaration supported by email clients that support CSS animations. Add these in a <style>
block:
.fly-in-left{ -webkit-animation: flyinleftanim 1s ease; } @-webkit-keyframes flyinleftanim { 0% {-webkit-transform: translateX(-1000px);} 100% {-webkit-transform: translateX(0px);} }
Then we wrap any element we want to “fly-in” with a div with the class “fly-in-left”. Now email clients that don’t support CSS animations will just display the content statically and clients that support CSS animations will display the content flying in from the left.
<div class="fly-in-left">Fly from left</div>
Adding a delay
What if we want to add a delay before having the element “fly-in”? The obvious technique would be to add an animation-delay
property. However that would mean the content would be displayed for a brief moment before the fly-ing in – not what we want.
We could address the issue above by styling it so the element is hidden before executing the animation, but the animation would revert to being hidden after it completes!
We could address the next issue with animation-fill-mode: forwards;
. Which would leave the element visible at the end of the animation… BUT… this isn’t supported by some old Android clients (specifically 2.x – if you don’t need that support you can use this method).
Our solution to the delay is to let the animation “do nothing” for a percentage of the time. The following 3 second animation starts the 1 second long fly-in at the 2 second (66%) mark.
.fly-in-left{ -webkit-animation: flyinleftanim 3s ease; } @-webkit-keyframes flyinleftanim { 0% {-webkit-transform: translateX(-1000px);} 66% {-webkit-transform: translateX(-1000px);} 100% {-webkit-transform: translateX(0px);} }
Bounding the animation
By default the elements will fly-in from the edge of the email. However if you’d prefer to have the fly-in effect from the edges of say a hero box, then add overflow: hidden
to the bounding area.
On iOS, if you want an element to fly in from the right, you would need to wrap the animation within a bounding area with overflow set to hidden as well. Otherwise iOS will zoom out the email to display the initial location of the element “flying in” from the right.
Pop-in
The pop-in effect is a combination of a fade-in and a quick grow and shrink animation. It uses the opacity style to fade in and the scale 2D transform to start the scaling from 75% of its size to 120% back to 100% to deliver the “pop”. Using the scale 2D transform vs using widths and heights – as mentioned in the last section allows the element to grow and shrink without affecting its surrounding elements.
As you can see from the code below, there’s a significant time where the animation does “nothing” from 0-93%. That is so that we can spring the animation at the close to 2 second point. Modify these values to suit your needs. You can also use the animation-delay property subject to the caveats mentioned in the previous section.
.pop-in{ -webkit-animation: popinanim 2s ease; } @-webkit-keyframes popinanim{ 0% { opacity:0; -webkit-transform: scale(0.75); } 93% { opacity:0; -webkit-transform: scale(0.75); } 98% { opacity:1; -webkit-transform: scale(1.2); } 100% { opacity:1; -webkit-transform: scale(1); } }
Where to learn more
Aside from getting some of the basics of using animation in email in the
last article, here are some other resources you can use.
- CSS Animation Reference
- CSS Transforms
- Daniel Eden’s Animate.CSS Entrance Effects
- CSS Transforms Demos at CSS Tricks
- Feeling ambitious? 3D Transforms!
GREAT article! Thank you!
Nice tutorial! Have you ever done any animation in combination with CSS/HTML on click using psuedo classes such as :active :hover or :target? Just curious if this can be done now with webkit clients.
Interesting you mentioned. There’s going to be an article next week just this topic :)
When I insert the pop-in styles, my animation is still coming in from the left? Any ideas why that is happening?
I read your article thoroughly and I want to say that CSS animation support is limited to more modern email clients, and these entrance effects are relatively simple to implement and may provide a cool effect to your email depending on your audience.