Home / Learn / How do you make a card flip in 3D?

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

  1. Create an outer wrapper and give it perspective to create depth.
  2. Set the card itself to transform-style: preserve-3d with a transition on transform.
  3. 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.
  4. On click, toggle a class that rotates the card 180 degrees, and update aria-pressed to match the state.
  5. 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.