Skip to content

Puzzle Card

The card is assembled from floating pieces. Pieces drift apart when idle, attract together on hover, and lock into place on click. Content appears only when complete.

Puzzle Card

by FreeCsStyles Dev's

CardsPuzzle
🌐🎨⚛️

Embed this component

Paste this snippet into any site — the design renders in a sandboxed iframe served from /embed/puzzle-card.

<iframe src="/embed/puzzle-card" sandbox="allow-scripts" loading="lazy" title="Puzzle Card" style="border:0;width:100%;height:480px;overflow:hidden"></iframe>

Implementation code

Copy the HTML and CSS for Puzzle Card, or grab a framework-specific implementation. The HTML and CSS variants are self-contained; framework variants assume that framework's project setup.

HTML
<div class="puzzle-card" onclick="assemblePiece(this)">
  <div class="puzzle-grid"></div>
  <div class="puzzle-pieces" id="puzzle-pieces"></div>
  <div class="puzzle-content">
    <div class="puzzle-header">
      <div><div class="puzzle-title">Mountain Vista</div><div class="puzzle-diff">Medium</div></div>
      <div class="puzzle-status" id="puzzle-status">0/12</div>
    </div>
    <div class="puzzle-divider"></div>
    <div class="puzzle-stats">
      <div class="puzzle-stat"><div class="puzzle-stat-val">12</div><div class="puzzle-stat-lbl">Pieces</div></div>
      <div class="puzzle-stat"><div class="puzzle-stat-val">Medium</div><div class="puzzle-stat-lbl">Difficulty</div></div>
    </div>
    <div class="puzzle-hint">[▼ click to assemble]</div>
  </div>
</div>
<script>
const emojis = ['🧩','🔷','🔶','🔸','🔹','⬡','▣','◈','◆','⬟','◇','⬠'];
let puzzlePieces = [];
let puzzleLocked = 0;
for (let i = 0; i < 12; i++) {
  puzzlePieces.push({ id: i, x: 10 + Math.random() * 75, y: 10 + Math.random() * 55, r: Math.random() * 60 - 30, emoji: emojis[i % emojis.length], locked: false });
}
function renderPieces() {
  const container = document.getElementById('puzzle-pieces');
  container.innerHTML = puzzlePieces.map(p => 
    `<span style="position:absolute;left:${p.x}%;top:${p.y}%;font-size:20px;opacity:${p.locked ? 1 : 0.4};transform:rotate(${p.r}deg);transition:all 0.4s cubic-bezier(0.34,1.56,0.64,1)">${p.emoji}</span>`
  ).join('');
}
function assemblePiece() {
  const next = puzzlePieces.find(p => !p.locked);
  if (!next) {
    puzzlePieces.forEach(p => { p.locked = false; p.x = 10 + Math.random() * 75; p.y = 10 + Math.random() * 55; p.r = Math.random() * 60 - 30; });
    puzzleLocked = 0;
    document.getElementById('puzzle-status').textContent = '0/12';
    renderPieces();
    return;
  }
  const cols = 4;
  const row = Math.floor(next.id / cols);
  const col = next.id % cols;
  next.x = 15 + col * 18;
  next.y = 10 + row * 16;
  next.r = 0;
  next.locked = true;
  puzzleLocked++;
  document.getElementById('puzzle-status').textContent = `${puzzleLocked}/12`;
  renderPieces();
  if (puzzleLocked === 12) {
    document.querySelector('.puzzle-card').classList.add('complete');
  }
}
renderPieces();
</script>
CSS
.puzzle-card {
  position: relative;
  width: 220px;
  min-height: 300px;
  border-radius: 16px;
  overflow: hidden;
  background: linear-gradient(160deg, #0a1620, #060e14);
  border: 1.5px solid rgba(56,189,248,0.08);
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  cursor: pointer;
  transition: all 0.4s ease;
}

.puzzle-card:hover {
  box-shadow: 0 0 30px rgba(56,189,248,0.08), 0 15px 40px rgba(0,0,0,0.4);
}

.puzzle-grid {
  position: absolute;
  top: 5%;
  left: 5%;
  right: 5%;
  bottom: 30%;
  border-radius: 8px;
  pointer-events: none;
  z-index: 5;
  border: 1px dashed rgba(56,189,248,0.06);
  background-image: linear-gradient(90deg, rgba(56,189,248,0.03) 1px, transparent 1px), linear-gradient(0deg, rgba(56,189,248,0.03) 1px, transparent 1px);
  background-size: 25% 25%;
}

.puzzle-pieces {
  position: absolute;
  top: 5%;
  left: 5%;
  right: 5%;
  bottom: 30%;
  pointer-events: none;
  z-index: 6;
}

.puzzle-card.complete .puzzle-pieces {
  background: rgba(56,189,248,0.04);
  border-radius: 8px;
}

.puzzle-content {
  position: absolute;
  inset-x: 0;
  bottom: 0;
  z-index: 10;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: linear-gradient(0deg, rgba(0,0,0,0.5), transparent);
}

.puzzle-header { display: flex; justify-content: space-between; align-items: center; }
.puzzle-title { font-size: 14px; font-weight: 700; color: rgba(255,255,255,0.9); }
.puzzle-diff { font-size: 9px; color: rgba(56,189,248,0.44); }
.puzzle-status {
  padding: 2px 8px; border-radius: 2px; font-size: 7px; font-weight: 700; letter-spacing: 0.1em;
  color: #38bdf8; background: rgba(56,189,248,0.1); border: 1px solid rgba(56,189,248,0.12);
}
.puzzle-divider { height: 1px; background: linear-gradient(90deg, transparent, rgba(56,189,248,0.08), transparent); }
.puzzle-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.puzzle-stat { text-align: center; padding: 6px 4px; border-radius: 8px; background: rgba(56,189,248,0.02); border: 1px solid rgba(56,189,248,0.06); }
.puzzle-stat-val { font-size: 10px; font-weight: 600; color: #38bdf8; }
.puzzle-stat-lbl { font-size: 7px; margin-top: 2px; color: rgba(255,255,255,0.2); }
.puzzle-hint { text-align: center; font-size: 7px; font-family: monospace; color: rgba(56,189,248,0.2); }

@media (prefers-reduced-motion: reduce) {
  .puzzle-card { transition: none; }
  .puzzle-pieces span { transition: none; }
}
Tailwind CSS
<div class="group relative w-[220px] min-h-[300px] rounded-2xl overflow-hidden border border-sky-400/20 shadow-lg group-hover:shadow-[0_0_30px_rgba(56,189,248,0.08),0_15px_40px_rgba(0,0,0,0.4)] bg-gradient-to-br from-[#0a1620] to-[#060e14] cursor-pointer">
  <div class="absolute top-[5%] left-[5%] right-[5%] bottom-[30%] rounded-lg pointer-events-none z-5 border border-dashed border-sky-400/20" style="background-image: linear-gradient(90deg, rgba(56,189,248,0.03) 1px, transparent 1px), linear-gradient(0deg, rgba(56,189,248,0.03) 1px, transparent 1px); background-size: 25% 25%;"></div>
  <div class="absolute inset-x-0 bottom-0 z-10 p-5 space-y-2 bg-gradient-to-t from-black/50 to-transparent">
    <div class="flex items-center justify-between">
      <div><p class="text-sm font-bold text-white/90">Mountain Vista</p><p class="text-[9px] text-sky-400/50">Medium</p></div>
      <div class="px-2 py-0.5 rounded-sm text-[7px] font-bold tracking-widest text-sky-400 bg-sky-500/10 border border-sky-500/20">0/12</div>
    </div>
    <div class="h-px bg-gradient-to-r from-transparent via-sky-400/20 to-transparent"></div>
    <div class="grid grid-cols-2 gap-2">
      <div class="text-center py-1.5 rounded-lg bg-sky-500/10 border border-sky-500/20"><p class="text-[10px] font-semibold text-sky-400">12</p><p class="text-[7px] mt-0.5 text-white/20">Pieces</p></div>
      <div class="text-center py-1.5 rounded-lg bg-sky-500/10 border border-sky-500/20"><p class="text-[10px] font-semibold text-sky-500">Medium</p><p class="text-[7px] mt-0.5 text-white/20">Difficulty</p></div>
    </div>
    <div class="text-center"><span class="text-[7px] font-mono text-sky-400/30">[▼ click to assemble]</span></div>
  </div>
</div>
React
import React, { useState, useRef } from 'react';
import { motion, useMotionValue, useSpring } from 'framer-motion';

export function PuzzleCard() {
  const [isHovered, setIsHovered] = useState(false);
  const [isComplete, setIsComplete] = useState(false);
  const [lockedCount, setLockedCount] = useState(0);
  const ref = useRef(null);
  const x = useMotionValue(0);
  const y = useMotionValue(0);
  const rotateX = useSpring(y, { damping: 25, stiffness: 200 });
  const rotateY = useSpring(x, { damping: 25, stiffness: 200 });
  const handleMouseMove = (e) => {
    if (!ref.current) return;
    const rect = ref.current.getBoundingClientRect();
    const cx = rect.left + rect.width / 2;
    const cy = rect.top + rect.height / 2;
    x.set(-((e.clientX - cx) / (rect.width / 2)) * 6);
    y.set(((e.clientY - cy) / (rect.height / 2)) * 6);
  };
  const pieceEmojis = ['🧩', '🔷', '🔶', '🔸', '🔹', '⬡', '▣', '◈', '◆', '⬟', '◇', '⬠'];
  const [pieces, setPieces] = useState(() =>
    Array.from({ length: 12 }, (_, i) => ({ id: i, x: 10 + Math.random() * 75, y: 10 + Math.random() * 55, rotate: Math.random() * 60 - 30, emoji: pieceEmojis[i % pieceEmojis.length], locked: false }))
  );
  const handleClick = () => {
    if (isComplete) {
      setIsComplete(false); setLockedCount(0);
      setPieces(Array.from({ length: 12 }, (_, i) => ({ id: i, x: 10 + Math.random() * 75, y: 10 + Math.random() * 55, rotate: Math.random() * 60 - 30, emoji: pieceEmojis[i % pieceEmojis.length], locked: false })));
      return;
    }
    setPieces((prev) => {
      const nextLocked = prev.findIndex((p) => !p.locked);
      if (nextLocked === -1) { setIsComplete(true); setLockedCount(12); return prev; }
      const newPieces = [...prev];
      const cols = 4; const row = Math.floor(nextLocked / cols); const col = nextLocked % cols;
      newPieces[nextLocked] = { ...newPieces[nextLocked], x: 15 + col * 18, y: 10 + row * 16, rotate: 0, locked: true };
      setLockedCount(nextLocked + 1);
      if (nextLocked === 11) setIsComplete(true);
      return newPieces;
    });
  };
  return (
    <div onMouseEnter={() => setIsHovered(true)} onMouseLeave={() => { setIsHovered(false); x.set(0); y.set(0); }}>
      <motion.div ref={ref} onMouseMove={handleMouseMove} onClick={handleClick} style={{ rotateX, rotateY, transformStyle: 'preserve-3d', cursor: 'pointer' }}>
        <motion.div className="relative overflow-hidden rounded-2xl" style={{ width: 220, minHeight: 300, background: 'linear-gradient(160deg, #0a1620, #060e14)', border: '1.5px solid rgba(56,189,248,0.08)' }}
          animate={{ boxShadow: isHovered ? '0 0 30px rgba(56,189,248,0.08), 0 15px 40px rgba(0,0,0,0.4)' : '0 10px 30px rgba(0,0,0,0.3)' }} transition={{ type: 'spring', stiffness: 280, damping: 20 }}>
          <div className="absolute top-[5%] left-[5%] right-[5%] bottom-[30%] rounded-lg pointer-events-none z-5" style={{ border: '1px dashed rgba(56,189,248,0.06)', backgroundImage: 'linear-gradient(90deg, rgba(56,189,248,0.03) 1px, transparent 1px), linear-gradient(0deg, rgba(56,189,248,0.03) 1px, transparent 1px)', backgroundSize: '25% 25%' }} />
          {pieces.map((piece) => (
            <motion.div key={piece.id} className="absolute flex items-center justify-center z-[6]" style={{ width: 32, height: 32, left: `${piece.x}%`, top: `${piece.y}%`, opacity: piece.locked ? 1 : isHovered ? 0.8 : 0.4 }}
              animate={{ x: isComplete ? 0 : isHovered && !piece.locked ? [0, 2, -2, 0] : 0, rotate: piece.locked ? 0 : piece.rotate, scale: isComplete ? [1, 1.05, 1] : isHovered && !piece.locked ? [1, 1.1] : 1 }}
              transition={{ duration: isComplete ? 0.5 : 2, repeat: isComplete || (!isHovered || piece.locked) ? 0 : Infinity, ease: 'easeInOut', delay: piece.locked ? piece.id * 0.05 : 0 }}>
              <span className="text-sm">{piece.emoji}</span>
            </motion.div>
          ))}
          <div className="absolute inset-x-0 bottom-0 z-10 p-5 space-y-2" style={{ background: 'linear-gradient(0deg, rgba(0,0,0,0.5), transparent)' }}>
            <div className="flex items-center justify-between">
              <div><p className="text-sm font-bold text-white/90">Mountain Vista</p><p className="text-[9px]" style={{ color: 'rgba(56,189,248,0.44)' }}>Medium</p></div>
              <motion.div className="px-2 py-0.5 rounded-sm text-[7px] font-bold tracking-widest" style={{ background: 'rgba(56,189,248,0.1)', border: '1px solid rgba(56,189,248,0.12)', color: '#38bdf8' }}>{isComplete ? 'DONE' : `${lockedCount}/12`}</motion.div>
            </div>
            <div className="h-px" style={{ background: 'linear-gradient(90deg, transparent, rgba(56,189,248,0.08), transparent)' }} />
            <div className="grid grid-cols-2 gap-2">
              <div className="text-center py-1.5 rounded-lg" style={{ background: 'rgba(56,189,248,0.02)', border: '1px solid rgba(56,189,248,0.06)' }}>
                <p className="text-[10px] font-semibold" style={{ color: '#38bdf8' }}>12</p><p className="text-[7px] mt-0.5 text-white/20">Pieces</p>
              </div>
              <div className="text-center py-1.5 rounded-lg" style={{ background: 'rgba(56,189,248,0.02)', border: '1px solid rgba(56,189,248,0.06)' }}>
                <p className="text-[10px] font-semibold" style={{ color: '#0284c7' }}>Medium</p><p className="text-[7px] mt-0.5 text-white/20">Difficulty</p>
              </div>
            </div>
            <motion.div className="text-center" animate={isHovered ? { y: [0, -1, 0] } : {}} transition={{ duration: 1.5, repeat: isHovered ? Infinity : 0 }}>
              <span className="text-[7px] font-mono" style={{ color: 'rgba(56,189,248,0.2)' }}>{isComplete ? '[▼ new puzzle]' : '[▼ click to assemble]'}</span>
            </motion.div>
          </div>
        </motion.div>
      </motion.div>
    </div>
  );
}

Interaction states & animations

  • Hover