I’m trying to implement a flip card animation in React with Tailwind.
The rotation works (the card flips in 3D), but the back image does not appear: when the card rotates, it stays blank or still shows the front.
I tried using both backgroundImage and with rotateY(180deg) and backface-visibility: hidden, but the back side never shows.
How can I make the back side of the card (CardBack) visible when the card rotates 180°?
Here’s a minimal example of my code:
import { useState } from "react";
import CardCover from "../cardImages/cardCover.png";
import CardBack from "../cardImages/cardBack.png";
export default function TestCard() {
const [isFlipped, setIsFlipped] = useState(false);
return (
<div
className="w-[200px] h-[300px] cursor-pointer [perspective:1000px]"
onClick={() => setIsFlipped(!isFlipped)}
>
<div
className="relative w-full h-full transition-transform duration-500"
style={{
transformStyle: "preserve-3d",
transform: isFlipped ? "rotateY(180deg)" : "rotateY(0deg)",
}}
>
{/* Front */}
<div
className="absolute w-full h-full rounded-xl bg-cover bg-center"
style={{
backgroundImage: `url(${CardCover})`,
backfaceVisibility: "hidden",
}}
></div>
{/* Back */}
<div
className="absolute w-full h-full rounded-xl bg-cover bg-center"
style={{
backgroundImage: `url(${CardBack})`,
transform: "rotateY(180deg)",
backfaceVisibility: "hidden",
}}
></div>
</div>
</div>
);
}