
Posted on • Originally published atauroratide.com
A (more) realistic card flip animation
There are a lot of cards on the internet, and a lot of them flip, revealing double-sided content. But the way so many of them flip is not...anatomically correct.
My old writing classes told me to show rather than tell: Which one of these cardsfeels better?
The first card, a Pichu, does a normal 180-degree turn. The second card, a Raichu, lifts up off the page while doing a 180-degree turn.
The Pichu card is how basically every tutorial tells you how to make a card flip animation. Thing is, real-life cards just don't work that way...
Cards cannot flipinto the table.
The Raichu card realizes two subtle details:
- A real-life card needs to be lifted to be flipped.
- A real-life card has thickness.
Subtlety is the difference between something feelinggood and something feelingsatisfying.
- Level -1: Cheating with a component
- Level 0: The Basic Card
- Level 1: Verticality
- Level 2: Thickness
- Level 3: Round Corners + Thickness
- Level 🧠: Keep Accessibility in mind!
Level -1: Cheating with a component
I was originally going to just write about how to make a cool flip animation, but I got alittle carried away and ended up creating a fully reusable component. Oops 🙂
Install@auroratide/flip-card and you get aweb component which can be used in any framework, be it React, Svelte, Vue, or vanilla.
<flip-card><sectionslot="front"><p>The front!</p></section><sectionslot="back"><p>The back!</p></section></flip-card>
Web components create new semantics through custom HTML elements. If you ever told yourself, "Dang, I wishthis were just a regular HTML element," then web components let you do just that: make it one!What is a web component?
Level 0: The Basic Card
The basic card we know and love relies on a few key CSS features:
- Absolute positioning
- 3D transformations - specificallyrotateY andperspective
- Backface visibility
It's really just two same-sized section boxes on top of each other, with one flipped 180 degrees and its backside invisible. Then rotate their container to reveal the second box's content.
A ton of tutorials already go into detail about how this works, so I'll just link all the appropriate documentation and throw some annotated code here. This'll serve as the base for climbing the Ladder of Card-Flip Enlightenment.
HTML
<articleclass="perspective-container"><divclass="card"><sectionclass="front face"aria-hidden="false"></section><sectionclass="back face"aria-hidden="true"></section></div></article>
CSS
.perspective-container{perspective:100em;/* creates an illusion of depth */perspective-origin:center;}.card{width:15em;aspect-ratio:5/7;position:relative;transform-style:preserve-3d;}.card.face{/* Hide the backside of the element, for when it is rotated */backface-visibility:hidden;}.card.back{/* The front and back elements occupy the same space */position:absolute;inset:0;transform:rotateY(180deg);}.cardsection{width:100%;height:100%;}/* Later sections will add code to actually flip the card *//* We'll be applying transformations to the .card class mainly */
Level 1: Verticality
When you flip a real-life card, you have to lift it off the surface first, otherwise the card melds into the table. Or more realistically the cardbends, cursing you for 7 years.
The traditional approach uses thetransition
CSS property, but all it's able to do is smoothly take you from one state (rotateY(0deg)
) to a different state (rotateY(180deg)
). In the case of lifting a card, the start and end states are the same. We need amiddle state where the card is lifted vertically, therefore we need a more powerful CSS tool.
Let's use@keyframes andanimation!
CSS JavascriptGive me annotated code!
@keyframesflip-to-front{0%{transform:translateZ(0em)rotateY(-180deg);}50%{transform:translateZ(var(--flip-height))rotateY(-270deg);}100%{transform:translateZ(0em)rotateY(-360deg);}}/* I'm using a second animation for two reasons: * 1. It allows the card to always flip in one direction. * 2. The renderer only plays an animation if it changes. */@keyframesflip-to-back{0%{transform:translateZ(0em)rotateY(0deg);}50%{transform:translateZ(var(--flip-height))rotateY(-90deg);}100%{transform:translateZ(0em)rotateY(-180deg);}}.card{--flip-height:17.5em;animation-duration:0.75s;animation-fill-mode:both;animation-timing-function:linear;/* NOTE: We're NOT setting the animation with CSS *//* By using Javascript, it's easier to prevent the animation from playing as soon as the page loads. */}
functionflipCard(card){card.classList.toggle("facedown")constisFacedown=card.classList.contains("facedown")card.style.animationName=isFacedown?"flip-to-back":"flip-to-front"}
Level 2: Thickness
Real-life cards have small, albeit non-zero, thickness. And with the power of 3D CSS, we can give our virtual cards thickness too! The effect is subtle but makes the rotation feel much more physical.
The strategy here is to assemble four emptydiv
blocks, representing the card's edges. We'll make them as wide/high as the card's thickness,position
them along the card's borders, and thenrotateY
them into the page.
HTML CSSGive me annotated code!
<divclass="card"><sectionclass="front face"aria-hidden="false"></section><sectionclass="back face"aria-hidden="true"></section><!-- NEW! We need divs that represent the 4 edges --><divclass="top edge"></div><divclass="right edge"></div><divclass="bottom edge"></div><divclass="left edge"></div></div>
.card{--card-depth:0.25em;/* Without special corner logic, a card with thickness cannot have border radius */border-radius:0;}.card.back{/* Push the back of the card backward to give space for the edges to live */transform:translateZ(calc(-1*var(--card-depth)))rotateY(180deg);}.edge{position:absolute;background-color:black;}/* All of this code is aligning the edges, rotating them into the page */.right,.left{width:var(--card-depth);height:100%;inset-block:0;}.right{inset-inline-end:0;transform:rotateY(270deg);transform-origin:rightcenter;}.left{inset-inline-start:0;transform:rotateY(90deg);transform-origin:leftcenter;}.top,.bottom{width:100%;height:var(--card-depth);inset-inline:0;}.top{inset-block-start:0;transform:rotateX(270deg);transform-origin:centertop;}.bottom{inset-block-end:0;transform:rotateX(90deg);transform-origin:centerbottom;}
Level 3: Round Corners + Thickness
Ever tried making a rotating cylinder with CSS? Turns out you can't, because CSS doesn't have 3D curved surfaces. The most reasonable way is tosimulate a cylinder with a bunch of thin flat surfaces.
Once our card acquires thickness, any rounded corners suddenly become quarter-cylinders. Therefore, we needadvanced magic math to make them look correct!
The strategy is to simulate each rounded corner as a series of small, flatdivs
arranged into quarter-circles whose radii are equal to the card's border radius. The number ofdivs
we use is what I'm calling the--corner-granularity
. Higher corner granularity means a smoother corner, but moredivs
being used.
HTML CSSGive me annotated code!
<divclass="card"><!-- ...front, back, sides... --><divclass="top-right corner"><divstyle="--i: 0;"></div><divstyle="--i: 1;"></div><divstyle="--i: 2;"></div></div><divclass="bottom-right corner"><divstyle="--i: 0;"></div><divstyle="--i: 1;"></div><divstyle="--i: 2;"></div></div><divclass="bottom-left corner"><!-- ... --></div><divclass="top-left corner"><!-- ... --></div></div>
.card{/* The number of faces used to simulate a round corner. More faces means more smooth. */--corner-granularity:3;--border-radius:1.5em;border-radius:var(--border-radius);}.corner>*{background-color:black;}/* We have to override the edges so they do not overlap the corners */.right,.left{inset-block:var(--border-radius);height:calc(100%-2*var(--border-radius));}.top,.bottom{inset-inline:var(--border-radius);width:calc(100%-2*var(--border-radius));}.corner{--n:var(--corner-granularity);--r:var(--border-radius);position:absolute;transform-style:preserve-3d;}/* A corner is composed of a finite number of flat faces that, when arranged in just the right way, looks rounded. *//* We need to do it this way because curved 3D surfaces do not exist in CSS. */.corner>*{position:absolute;inset-block-end:0;width:var(--card-depth);height:calc(2*var(--r)*sin(45deg/var(--n)));transform-origin:bottomcenter;/* This math constructs a single corner. *//* I derived it on a paper somewhere and threw it away, *//* so you'll have to derive it yourself if you want to understand what's happening (: */transform:translateZ(calc(var(--r)*cos(var(--i)*90deg/var(--n))))translateY(calc(-1*var(--r)*sin(var(--i)*90deg/var(--n))))rotateX(calc(45deg*(2*var(--i)+1)/var(--n)));}/* The rest of this code slots the corners where they belong. */.top-right{inset-block-start:0;inset-inline-end:0;transform:rotateY(90deg)translateZ(calc(-1*var(--r)))translateY(var(--r));}.bottom-right{inset-block-end:0;inset-inline-end:0;transform:rotateY(90deg)rotateX(270deg)translateZ(calc(-1*var(--r)))translateY(var(--r));}.bottom-left{inset-block-end:0;inset-inline-start:0;transform:rotateY(90deg)rotateX(180deg)translateZ(calc(-1*var(--r)))translateY(var(--r));}.top-left{inset-block-start:0;inset-inline-start:0;transform:rotateY(90deg)rotateX(90deg)translateZ(calc(-1*var(--r)))translateY(var(--r));}
Level 🧠: Keep Accessibility in mind!
Accessibility is the practice of considering all the people who might use your website and making it usable by as many of them as possible. Flippy cards can create a few pitfalls if we're not careful!
- What if the person cannot use a mouse? Is hover the only way to flip your card?
- What if the person uses theTab key to navigate? Will they run into a button hidden on the backside of your card?
- What if the person uses ascreenreader to read the page's content aloud? Will it read content hidden on the backside of the card?
- What if the person prefers less animation? Will the card's flip animation be jarring to them?
While it isn't the point of this article explore accessibility, these are nonetheless important questions to consider. Here are some tools you can use to address them.
Obligatory Conclusion
Ok I admit, nothing's actuallywrong with the normal card flip and the tutorials that teach it ❤️ I mean, why can't it represent a card flip happening in mid-air?
I just wanted to share something I tried and liked, and if you like it, feel free to use it too!
- @auroratide/flip-card web component
- Card Flip Codepen Collection
Top comments(4)

- Email
- LocationEarth
- EducationSelf-taught
- WorkFreelance Programmer | Fullstack Dev
- Joined
This is awesome! I love animating with CSS and JS. Very cool project!

- LocationDallas
- Pronounshe/him
- WorkSoftware Engineer
- Joined
Let me know if you've seen other creative animations or have ways to improve this!

- Email
- LocationAmsterdam, the Netherlands
- EducationTRS-80 Scriptkiddie '79+ 🎓 Computer Science '87 - '92 ❤️ Online since 1990
- WorkI guide people with PTSD, Autism, or general burnout towards a career in web development
- Joined
I am still looking for acool realisticfold animation.
Several people took a shot at it:StackOverflow Feb'23
For further actions, you may consider blocking this person and/orreporting abuse