How do you make a card flip in 3D?
A flip card gives you two faces in one space: an image in front, details on the back. Here you learn how perspective, preserve-3d and backface-visibility work together.
Steps
- Create an outer wrapper and give it perspective to create depth.
- Set the card itself to transform-style: preserve-3d with a transition on transform.
- Stack the two faces exactly on top of each other, hide each back with backface-visibility: hidden, and pre-rotate the back face 180 degrees.
- On click, toggle a class that rotates the card 180 degrees, and update aria-pressed to match the state.
- Under prefers-reduced-motion, drop the transition so the swap is instant with no rotation.
The code
HTML
<div class="flip-scene">
<button class="flip-card" id="flipCard" aria-pressed="false">
<span class="face face-front">الوجه الأمامي</span>
<span class="face face-back">التفاصيل في الخلف</span>
</button>
</div>
<script>
const flipCard = document.getElementById("flipCard");
flipCard.addEventListener("click", () => {
const isFlipped = flipCard.classList.toggle("is-flipped");
flipCard.setAttribute("aria-pressed", String(isFlipped));
});
</script>
<style>
.flip-scene {
perspective: 800px;
}
.flip-card {
position: relative;
width: 240px;
height: 150px;
padding: 0;
border: 0;
background: none;
cursor: pointer;
transform-style: preserve-3d;
transition: transform 0.6s cubic-bezier(.22, .61, .36, 1);
}
.flip-card.is-flipped {
transform: rotateY(180deg);
}
.face {
position: absolute;
inset: 0;
display: grid;
place-items: center;
border-radius: 14px;
font-size: 1.1rem;
backface-visibility: hidden;
}
.face-front {
background: #6d5ef0;
color: #fff;
}
.face-back {
background: #0f172a;
color: #fff;
transform: rotateY(180deg);
}
@media (prefers-reduced-motion: reduce) {
.flip-card { transition: none; }
}
</style>RTL & Arabic notes: the vertical-axis rotation is symmetric in both directions, and the Arabic text on both faces renders fully joined.
Didn't find the motion you need? Request it from the Vellox team.