3D Card Tilt Effect in React with Tailwind and Framer Motion

2026-11-17

A tilt card leans toward your pointer, like a physical card resting on a table. It is one of the most copied 3D effects on the web, and it needs no WebGL: just CSS perspective, two rotations and a spring. This guide builds one from scratch, then points out the details that separate a good version from a janky one.

How the effect works

Three ideas combine. First, perspective on a parent gives its children depth, so rotated elements look 3D instead of flat. Second, the pointer position inside the card (0 to 1 on each axis) is converted into a small rotateX and rotateY. Third, a spring smooths the movement so the card eases toward the pointer and settles back when you leave.

The component

Framer Motion's motion values update the transform without re-rendering React, which is what keeps this smooth. useTransform maps the pointer range to degrees and useSpring adds the easing.

import { useRef, type ReactNode, type PointerEvent } from "react";
import { motion, useMotionValue, useSpring, useTransform } from "framer-motion";

export function TiltCard({ children }: { children: ReactNode }) {
  const ref = useRef<HTMLDivElement>(null);
  const rect = useRef<DOMRect | null>(null);
  const x = useMotionValue(0.5);
  const y = useMotionValue(0.5);
  const spring = { stiffness: 200, damping: 20 };
  const rotateX = useSpring(useTransform(y, [0, 1], [10, -10]), spring);
  const rotateY = useSpring(useTransform(x, [0, 1], [-10, 10]), spring);

  function onEnter(e: PointerEvent) {
    if (e.pointerType === "touch") return;
    rect.current = ref.current!.getBoundingClientRect(); // measure once, not on every move
  }
  function onMove(e: PointerEvent) {
    const r = rect.current;
    if (!r) return;
    x.set((e.clientX - r.left) / r.width);
    y.set((e.clientY - r.top) / r.height);
  }
  function onLeave() {
    rect.current = null;
    x.set(0.5);
    y.set(0.5);
  }

  return (
    <div style={{ perspective: 900 }}>
      <motion.div
        ref={ref}
        onPointerEnter={onEnter}
        onPointerMove={onMove}
        onPointerLeave={onLeave}
        style={{ rotateX, rotateY, transformStyle: "preserve-3d" }}
        className="rounded-2xl border border-slate-200 bg-white p-6 shadow-xl dark:border-slate-800 dark:bg-slate-900"
      >
        {children}
      </motion.div>
    </div>
  );
}

Details that matter

  • Measure the card's rectangle once when the pointer enters. Calling getBoundingClientRect on every move forces layout work and is the usual cause of stutter.
  • Ignore touch. A finger dragging across a card should scroll the page, not tilt it, so the handler skips pointerType === "touch".
  • Keep the angle small. Eight to twelve degrees looks premium. Twenty or more looks like a toy and can make text hard to read.
  • Respect prefers-reduced-motion. Framer Motion's useReducedMotion() tells you when to skip the tilt entirely.
  • Only animate transform. It runs on the GPU. Animating top, left or box-shadow on every frame does not.

Adding depth inside the card

Because the card has transformStyle: "preserve-3d", you can lift children off its surface with translateZ. A title at translateZ(40px) appears to float above a background at translateZ(0), and it shifts slightly as the card tilts. This parallax is what makes the effect feel real. Remember that every ancestor between the perspective container and the lifted child must also use preserve-3d, or the depth is flattened.

Skip the boilerplate

Depth UI ships this as a copy-paste component with glare, layered depth and touch handling already done. See the Tilt card, the Spotlight card that adds a pointer-following light, and the Holo card with a foil sheen. Each page has a live preview, the full source and a props table, and the code is yours to edit.

Components from this guide