webdevpuneet.com

Background Boxes — Free HTML CSS JS Interactive Grid Snippet

Background Boxes · Animations · Plain HTML, CSS & JS · Live preview

CategoryAnimations

Copy & paste

Source Code

<section class="bb-hero">
  <div class="bb-grid" id="bbGrid" aria-hidden="true"></div>
  <div class="bb-fade" aria-hidden="true"></div>
  <div class="bb-content">
    <h1>Hover the grid</h1>
    <p>Cells light up in random colors under your cursor — a living interactive backdrop.</p>
    <button type="button" class="bb-btn">Explore</button>
  </div>
</section>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Code-generated grid
Cells built from three size constants.
Skewed lattice
skewX plus scale gives the boxes their slant.
Event delegation
Two listeners light hundreds of cells.
Random color flash
Each hovered cell picks a palette color.
Fading color trail
A delay plus transition leaves a comet tail.
Vignette fade
Cells emerge from a darkened surround.
Click-through content
pointer-events none lets the grid light under text.
Constant cost
Grid size never adds more listeners.

About this UI Snippet

Background Boxes — Cursor-Lit Skewed Grid Backdrop

Screenshot of the Background Boxes snippet rendered live

Background boxes is the interactive hero backdrop where a large skewed grid of cells lights up in random colors as your cursor sweeps across it, leaving a brief trail of color that fades away — a playful, living surface behind your headline. This snippet builds it with plain HTML, CSS, and a compact vanilla JavaScript handler that stays fast even with hundreds of cells.

Generating the grid

The grid is created in JavaScript: a loop appends COLS × ROWS (here 24 × 16 = 384) plain <div> cells into a CSS grid whose column and row tracks are set to a fixed pixel size. Each cell has only a faint right and bottom border, so together they form a clean graph-paper lattice. Building the cells in code rather than hand-writing 384 divs keeps the markup tiny and the dimensions trivially adjustable through three constants.

The signature skew

What turns a plain grid into the recognizable "boxes" look is the container transform: skewX(-48deg) scale(1.6). The skew shears the whole lattice into a dynamic parallelogram angle, and the scale-up ensures the skewed grid still covers the full hero with no empty corners. This single transform on the container is what gives the backdrop its distinctive diagonal energy.

Lighting cells with event delegation

Rather than attaching a listener to every one of the hundreds of cells, the snippet uses event delegation: two listeners on the grid container catch pointerover and pointerout events as they bubble up, and a class check confirms the target is a cell. On pointerover the cell's background-color is set to a random color from the palette; on pointerout it's cleared. Delegation means the cost stays constant no matter how large the grid grows — adding more cells doesn't add more listeners.

The fading color trail

Each cell has a transition on background-color, so when the color is cleared the cell fades back to transparent instead of snapping. The clear is also delayed by 400ms with a setTimeout, so a quick cursor pass leaves a short-lived trail of lit cells rather than only ever showing the single cell directly under the pointer. The combination of the delay and the transition is what creates the comet-tail effect.

The vignette and content layering

A radial .bb-fade layer darkens the grid toward the edges so the lit cells appear to emerge from darkness and the hard grid boundary is hidden. The content sits above at a higher z-index and is set pointer-events: none (except the button) so moving the cursor over the headline still passes through to light the grid underneath — the text never blocks the interaction.

Customizing it

Change COLS, ROWS, and SIZE to resize the lattice, edit the skew angle for a different slant, swap the COLORS palette, or tune the 400ms clear delay to lengthen or shorten the trail. Adjust the vignette stops to focus more or less tightly. Pair it with a text generate headline or a shimmer button for a vivid interactive hero.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA skewed grid hero renders behind a centered headline.
  2. 2
    Sweep your cursorCells light up in random colors under the pointer.
  3. 3
    Move quicklyA short trail of lit cells fades out behind you.
  4. 4
    Hover the headlineThe grid still lights up through the text.
  5. 5
    Resize the latticeChange COLS, ROWS, and SIZE constants.
  6. 6
    Recolor and re-skewEdit the palette and the skew angle.

Real-world uses

Common Use Cases

Interactive hero backdrops
Place a skewed grid behind a text generate headline, with cells lighting in random colours as the cursor sweeps across.
Developer tool landing pages
Pair with an animated grid background in another section of a developer tool landing page for a coherent technical look.
Playful 404 screens
Turn an empty state into a toy, as hundreds of cells respond to just two delegated event listeners.
Launch-event call to action
Add a shimmer button over the grid, with each hovered cell flashing a palette colour that fades away.
Event delegation reference
Study how delegated listeners handle a large generated grid, and compare the effect with a minimal hero for portfolio intros.
Related: Canvas Fractal Tree Generator
See the Canvas Fractal Tree Generator for a related animations pattern worth pairing with this one.
Related: FAB Container Transform Sheet
See the FAB Container Transform Sheet for a related animations pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

Through event delegation. Instead of a listener on every cell, two listeners on the grid container catch pointerover and pointerout as they bubble, and a class check confirms the target is a cell. The cost stays constant regardless of grid size, so adding more cells never adds more listeners.

Each cell has a CSS transition on background-color, and the color is cleared on pointerout after a 400ms setTimeout. The delay keeps recently-hovered cells lit briefly while you move on, and the transition fades them out smoothly, producing a short comet-tail trail rather than only the single cell under the pointer.

The container has transform: skewX(-48deg) scale(1.6). The skew shears the lattice into a dynamic diagonal parallelogram — the recognizable boxes look — and the scale ensures the skewed grid still covers the whole hero with no empty corners. It's a single transform on the container.

The content layer is set to pointer-events: none (except the button), so pointer events pass through the text to the grid beneath. That means moving the cursor over the headline still triggers the cells underneath, and the text never blocks the interaction.

Render the cells from an array sized by your constants, and attach delegated pointerover/pointerout handlers on the grid container (or use the framework's event binding with a target check). Set the cell color via inline style or a brief state map, clearing it on a timeout. The skew and vignette are pure CSS. In Tailwind, build the grid with grid utilities and apply the skew with an arbitrary transform value.