December 4, 2026
A 3D tilt card leans toward the pointer as it moves across the card. To build one, give the card a perspective, turn the
pointer's position inside the card into two small angles, and apply them as rotateX and rotateY. It
takes about ten lines of JavaScript and no library.
What is the basic pattern?
.tilt {
transform: perspective(800px) rotateX(var(--rx, 0deg)) rotateY(var(--ry, 0deg));
transition: transform 0.15s ease-out;
will-change: transform;
}
.tilt.is-leaving { transition-duration: 0.5s; }
const MAX = 12; // degrees
document.querySelectorAll(".tilt").forEach((card) => {
card.addEventListener("pointermove", (e) => {
const r = card.getBoundingClientRect();
const px = (e.clientX - r.left) / r.width - 0.5; // -0.5 to 0.5
const py = (e.clientY - r.top) / r.height - 0.5;
card.classList.remove("is-leaving");
card.style.setProperty("--ry", `${px * MAX}deg`);
card.style.setProperty("--rx", `${-py * MAX}deg`);
card.style.setProperty("--gx", `${(px + 0.5) * 100}%`);
card.style.setProperty("--gy", `${(py + 0.5) * 100}%`);
});
card.addEventListener("pointerleave", () => {
card.classList.add("is-leaving");
card.style.setProperty("--rx", "0deg");
card.style.setProperty("--ry", "0deg");
});
});
Note the minus sign on --rx. Moving the pointer down should tip the top of the card away from you, and rotation
around the X axis runs the other way by default.
How do I add the glare highlight?
.tilt { position: relative; overflow: hidden; }
.tilt::after {
content: ""; position: absolute; inset: 0; pointer-events: none;
background: radial-gradient(circle at var(--gx, 50%) var(--gy, 50%),
rgb(255 255 255 / 0.35), transparent 60%);
}
The script above already writes --gx and --gy, so the bright spot follows the pointer.
How do I make the content float above the card?
Set transform-style: preserve-3d on the card and give the inner elements a translateZ(), for example
40 pixels for the title and 20 for the text. They then shift by different amounts as the card turns. Remove
overflow: hidden if you do this, because it flattens 3D children.
Which finished examples can I copy?
| Element | What it does |
|---|---|
| Card hover tilt | A grid of cards that tilt in 3D toward the pointer with a moving glare highlight and a lifted shadow. |
| Skew hover cards | Three cards that bend, twist and tilt in 3D under the pointer, each with its own character. |
| Depth parallax card | Layered shapes that shift by different amounts as the pointer moves, drifting on touch screens. |
All are free in the Enrichr Library with live controls and copyable code.
What should I be careful about?
- Keep the angle small. Past 15 degrees text gets hard to read.
- Do not tilt cards that hold forms or long text.
- Hold the cards still when
prefers-reduced-motionis set.
Where to go next
- How to build a 3D carousel in CSS.
- How to make a magnetic button.
- The Enrichr Library: 175 free elements.
FAQ
How do I make a card tilt toward the mouse?
Give the card transform: perspective(800px) rotateX() rotateY() and update the two angles on pointermove from the pointer's position inside the card. Reset them on pointerleave.
Do I need vanilla-tilt.js or another library?
No. The core effect is about ten lines. A library is useful if you want gyroscope support or many options without writing them.
Why does my tilt card look flat?
There is no perspective. 3D rotations need either perspective() at the start of the transform or a perspective property on the parent.
Does the tilt effect work on phones?
Not from hover. Either leave the cards flat on touch screens or give them a gentle automatic drift, as the Library's depth parallax card does.