webdevpuneet.com

Mouse-Tilt 3D Extruded Text — Cursor-Driven Depth in CSS

Mouse-Tilt 3D Extruded Text · Text Animation · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<main class="stage" id="stage">
  <p class="hint">Move your pointer across the page</p>
  <h1 class="block" id="block">DEPTH</h1>
  <div class="swatches" id="swatches" role="group" aria-label="Extrusion colour">
    <button type="button" class="sw on" data-c="#f97316" style="--c:#f97316" aria-label="Orange"></button>
    <button type="button" class="sw" data-c="#22d3ee" style="--c:#22d3ee" aria-label="Cyan"></button>
    <button type="button" class="sw" data-c="#a3e635" style="--c:#a3e635" aria-label="Lime"></button>
    <button type="button" class="sw" data-c="#f472b6" style="--c:#f472b6" aria-label="Pink"></button>
  </div>
</main>

Extend with AI

Take This Snippet Further With AI

Ask an AI coding assistant to explain why the extrusion is built from many zero-blur shadows that get darker toward the back instead of one blurred shadow, and how the light direction vector links to the pointer. A good second question is why the tilt uses a lerp inside requestAnimationFrame rather than a CSS transition.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an interactive extruded 3D headline in plain HTML, CSS and JavaScript, using only text-shadow and CSS 3D transforms (no WebGL).

Requirements:
- One very heavy display word on a light background, inside a container with a perspective of around 900px.
- Every frame, build the text-shadow in JavaScript as about 22 zero-blur shadows, each offset one step further along a direction vector, each darker than the last, plus one soft blurred shadow further out to ground the word.
- Track the pointer over the page, normalise it to -1..1, and use it both to rotate the word (rotateY and rotateX up to about 26 and 20 degrees) and to set the direction the extrusion travels, so tilting right swings the sides to the left.
- Smooth the values with an exponential lerp inside requestAnimationFrame, and ease back to flat when the pointer leaves.
- Add colour swatches that change the extrusion colour, with a helper that darkens the hex colour toward the back layers.
- Under prefers-reduced-motion, draw it once at a fixed angle and do not start the animation loop.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

What's included

Features

Generated depth
22 offset shadows are built in a loop, so depth and direction are numbers you can change, not a pasted wall of CSS.
Shaded sides
Each layer is darker than the one in front, which makes the sides look lit rather than smeared.
Direction follows the cursor
Pointer position drives both the tilt and the shadow vector, so the extrusion swings as you move.
Smoothed motion
A per-frame lerp eases the tilt toward the pointer and settles back flat when you leave.
Recolourable
Four swatches change the colour live; the shading function derives every layer from one hex value.
Reduced-motion safe
With reduced motion set, the word is drawn once at a fixed angle and the animation loop never starts.

About this UI Snippet

Mouse-Tilt 3D Extruded Text — Stacked Text-Shadow Depth With Perspective Tilt

Screenshot of the Mouse-Tilt 3D Extruded Text snippet rendered live

CSS can't extrude text, but it can fake it convincingly: pile up dozens of zero-blur shadows, each one pixel further along the same direction, and the shadows merge into a solid side wall. This snippet does that for a block headline, then ties the direction of the wall to your pointer so the word seems to be a physical object you can look around.

The extrusion is generated, not written

JavaScript builds the text-shadow string on every frame: 22 shadows, each offset by i times a direction vector, and each darker than the last. The darkening matters as much as the offset. Colour fades by 55% toward the back, so the sides read as shaded geometry instead of a flat smear. One last soft, blurred shadow is added far out to ground the letters.

Light direction follows the pointer

The pointer position (-1 to 1 across the window) sets both a rotateY/rotateX tilt on the word and the vector that the shadows travel along. Tilting right swings the extrusion left, as if the sides are being revealed. The perspective of 900px on the container is what makes the tilt read as 3D and not a skew.

Smoothed with a lerp, not a transition

Mouse movement is jumpy, so the displayed tilt eases 12% of the way toward the target each animation frame. This also means the word keeps drifting after you stop, and it settles back flat when the pointer leaves. The swatches recolour the front-to-back gradient. For reduced motion the word is painted once at a fixed angle with no animation loop.

Step by step

How to Use

  1. 1
    Paste the three blocksA cream page appears with an oversized white word, orange sides, and four colour dots underneath. Move your pointer and the word turns toward it.
  2. 2
    Change the wordEdit the text in #block. Short, heavy words (4–7 letters) work best; very long words get wide enough that the extrusion gets noisy.
  3. 3
    Set the depthDEPTH is how many shadows build the side wall. 12 is a thin slab, 22 is chunky, and 40 is dramatic but costs more to paint.
  4. 4
    Pick the tilt strengthThe 26 and 20 in the rotateY and rotateX values are the maximum degrees. Lower them for a subtler effect, or drop the rotation and keep only the moving shadow.
  5. 5
    Match your paletteChange the color default and the swatch data-c values. The shade() function darkens whatever colour you give it toward the back.

Real-world uses

Common Use Cases

Poster-style hero headlines
A single bold word like "SALE", "LAUNCH" or the brand name fills a landing hero with a toy-like, tactile feel.
Playful brand and kids' sites
The chunky extrusion suits toys, games, learning apps and food brands where depth feels friendly.
Portfolio introductions
Use a first name or role as the interactive centrepiece of a creative portfolio.
404 and empty states
A giant "404" that follows the cursor makes an error page worth a second look.
Event and festival pages
Tilting block lettering fits music, sports and street-food posters turned into web pages.

Got questions?

Frequently Asked Questions

Text-shadow keeps the headline as real, selectable, SEO-readable text and needs no library. The trade-off is that depth is a visual trick: it works best on heavy, simple letterforms.

For one headline it is fine on modern devices. Lower DEPTH, or only run the loop while the pointer is over the stage, if you have a lot of text or target older phones.

Pointer events fire on touch drags, so it tilts as the finger moves. Without movement it stays flat. For devices with tilt sensors you could feed deviceorientation into the same goal values.

Yes for the front, using background-clip:text, but shadows then draw over the fill in some browsers. Test, and consider a pseudo-element copy for the face if you need a gradient.