Components

Spotlight effect in CSS: cursor-following glow on cards and borders

Build a spotlight hover effect: a radial glow that follows the cursor across a grid of cards and lights up their borders. Live demo, CSS, JS, React and timing.

20 November 2026 · 9 min read

The spotlight effect is the soft glow that follows your cursor across a grid of feature cards, lighting up the borders it passes near. It shows up on a lot of product and developer-tool sites because it makes a static grid feel responsive without moving any of the content.

It is a radial-gradient positioned by two custom properties, plus about fifteen lines of JavaScript that write the pointer position into them. This guide builds a shared spotlight across a grid, with a border glow that reveals near the cursor, an inner glow on the hovered card, fades on enter and leave, keyboard and touch fallbacks, and a React hook.

Move the pointer across the cards, or Tab through them

How the effect is put together

There are two glows per card, and only their opacity is animated. Their position is not a timeline at all: it is set directly from the pointer on every frame.

PartPropertyDurationDelayEasing
Border glow, pointer enters gridopacity: 0 → 1300 ms0ease
Border glow, pointer leaves gridopacity: 1 → 0500 ms0ease
Inner glow, hovered cardopacity: 0 → 1400 ms0ease
Glow position--x, --yone frame0none (follows pointer)
Keyboard focus glowopacity: 0 → 1200 ms0ease

The border glow is shared: when the pointer is anywhere over the grid, every card shows its border glow, but each card's gradient is centered on the same screen point. Cards far from the cursor fall outside the gradient's radius, so only the edges near the pointer light up, including the edges of neighbouring cards across the gap. The inner glow only appears on the card under the pointer.

The HTML

<div class="spl-grid">
  <a class="spl-card" href="/record">
    <span class="spl-title">Record</span>
    <span class="spl-text">Capture motion while it plays.</span>
  </a>
  <!-- more cards -->
</div>

Use real links or buttons for the cards so they are focusable without extra attributes. The text sits in its own spans so it can be lifted above the glow layers with z-index.

The CSS

.spl-card {
  --x: -999px; --y: -999px;             /* off-card until the pointer arrives */
  position: relative;
  border: 1px solid rgba(255, 255, 255, .1);
  border-radius: 14px;
  background: #1c1f26;
}

/* border glow: a 1px ring with the middle masked out */
.spl-card::before {
  content: ""; position: absolute; inset: -1px; padding: 1px;
  border-radius: inherit; pointer-events: none;
  background: radial-gradient(220px circle at var(--x) var(--y),
              rgba(251, 159, 61, .95), transparent 70%);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  opacity: 0; transition: opacity .5s ease;      /* leave */
}

/* inner glow on the hovered card */
.spl-card::after {
  content: ""; position: absolute; inset: 0;
  border-radius: inherit; pointer-events: none;
  background: radial-gradient(260px circle at var(--x) var(--y),
              rgba(251, 159, 61, .12), transparent 60%);
  opacity: 0; transition: opacity .5s ease;
}

.spl-title, .spl-text { position: relative; z-index: 1; }

@media (hover: hover) {
  .spl-grid:hover .spl-card::before { opacity: 1; transition-duration: .3s; }  /* enter */
  .spl-card:hover::after { opacity: 1; transition-duration: .4s; }
}

/* touch screens: a static glow on the top edge */
@media (hover: none) {
  .spl-card::before { --x: 50%; --y: 0%; opacity: .6; }
}

/* keyboard: light the focused card from the top */
.spl-card:focus-visible { outline: 2px solid #fb9f3d; outline-offset: 3px; }
.spl-card:focus-visible::before,
.spl-card:focus-visible::after { --x: 50%; --y: 0%; opacity: 1; transition-duration: .2s; }

@media (prefers-reduced-motion: reduce) {
  .spl-card::before, .spl-card::after { transition: none; }
}

The border trick is the one used for animated gradient borders. The pseudo-element covers the card's border box and has 1px of padding. The mask paints two layers, one clipped to the content box and one covering everything, and exclude keeps only the area where they do not overlap: the 1px ring. The -webkit- lines cover older WebKit and Blink builds that only understand the prefixed form.

Enter and leave get different speeds by putting the slower duration on the base rule and the faster one on the hover rule. A transition uses the duration of the state it is moving to, so the glow appears in 300 ms and lingers for 500 ms after the pointer leaves.

The focus rule sets --x and --y on the pseudo-elements themselves. That beats the inline values the script writes on the card, because a custom property declared on an element wins over one it would inherit.

The JavaScript

const grid = document.querySelector('.spl-grid');
const cards = [...grid.querySelectorAll('.spl-card')];
let frame = 0, px = 0, py = 0;

function update() {
  frame = 0;
  const rects = cards.map(card => card.getBoundingClientRect()); // read all first
  cards.forEach((card, i) => {                                    // then write
    card.style.setProperty('--x', `${px - rects[i].left}px`);
    card.style.setProperty('--y', `${py - rects[i].top}px`);
  });
}

grid.addEventListener('pointermove', e => {
  if (e.pointerType === 'touch') return;   // touch gets the static glow
  px = e.clientX; py = e.clientY;
  if (!frame) frame = requestAnimationFrame(update);
});

Pointer events can fire several times per frame on high-rate mice. The handler only stores the latest position and schedules one requestAnimationFrame; the frame callback does the real work. Coordinates are converted to each card's own space, because a gradient position is relative to the box it paints.

The listener is on the grid, not on each card, so it keeps firing while the pointer crosses the gaps. That is what makes the spotlight feel like one light moving over the whole surface instead of a separate effect per card.

The React version

import { useEffect, useRef } from 'react';

export function useSpotlight() {
  const ref = useRef(null);
  useEffect(() => {
    const grid = ref.current;
    if (!grid) return;
    let frame = 0, px = 0, py = 0;

    const update = () => {
      frame = 0;
      const cards = [...grid.querySelectorAll('[data-spotlight]')];
      const rects = cards.map(c => c.getBoundingClientRect());
      cards.forEach((c, i) => {
        c.style.setProperty('--x', `${px - rects[i].left}px`);
        c.style.setProperty('--y', `${py - rects[i].top}px`);
      });
    };
    const onMove = e => {
      if (e.pointerType === 'touch') return;
      px = e.clientX; py = e.clientY;
      if (!frame) frame = requestAnimationFrame(update);
    };

    grid.addEventListener('pointermove', onMove);
    return () => {
      grid.removeEventListener('pointermove', onMove);
      cancelAnimationFrame(frame);
    };
  }, []);
  return ref;
}

export function FeatureGrid({ items }) {
  const ref = useSpotlight();
  return (
    <div ref={ref} className="spl-grid">
      {items.map(item => (
        <a key={item.href} href={item.href} className="spl-card" data-spotlight>
          <span className="spl-title">{item.title}</span>
          <span className="spl-text">{item.text}</span>
        </a>
      ))}
    </div>
  );
}

The hook writes custom properties straight to the DOM instead of keeping the pointer in React state. State would re-render the grid on every mouse move for a value only CSS needs. The CSS file is the same as above.

Performance

  • One update per frame. The rAF throttle caps work at the display's refresh rate, however fast the pointer events arrive.
  • Read, then write. All getBoundingClientRect() calls happen before any property is set. Interleaving them forces the browser to recalculate styles between cards.
  • Paint, not layout. Changing --x and --y repaints the two pseudo-elements per card. Nothing moves, so there is no layout. Keep the grid to tens of cards, not hundreds.
  • Keep gradients modest. A 220 px radius is plenty. Very large gradients and stacked blur() filters are what make this effect expensive.

Accessibility notes

  • The glow is decoration. Cards still need a visible focus style; here the outline stays and the focus glow is extra.
  • Touch devices have no hover, so @media (hover: none) gives them a static glow on the top edge instead of a glow stuck wherever the last tap landed.
  • Under prefers-reduced-motion: reduce the fades are removed. The glow still tracks the pointer, because that is direct feedback to the user's own movement rather than motion that plays on its own. If your glow is large or bright, consider turning tracking off as well and showing the static version. The reduced motion guide covers where to draw that line.
  • Check contrast with the glow at full strength. A bright border under body text can make the text harder to read.

Copy one you already like

Spotlight effects differ in radius, colour, how far the glow reaches into neighbouring cards and how quickly it fades. To match one you have seen, open the page and run the Capture Motion extension on the card. It sets a real :hover state during capture, as described in how to capture hover animations, and records the duration, delay and easing of the opacity and colour transitions, which you can export as CSS, React or JSON. The pointer-following part is live input rather than keyframes, so pair the captured timing with the script above.

Questions

How do I make a CSS glow follow the mouse?

Draw the glow with a radial-gradient whose position uses two custom properties, such as --x and --y. A small pointermove listener writes the cursor position into those properties, and the gradient moves with it on the next frame.

How do I make the spotlight light up only the border?

Put the gradient on a pseudo-element with 1px of padding and mask out its content box with mask-composite: exclude. Only the 1px ring that sits over the border stays visible, so the glow reads as a lit edge.

Is a cursor spotlight bad for performance?

It is cheap if you throttle updates to one per animation frame and only change custom properties. Each update repaints the gradient but does not trigger layout, so a grid of a dozen cards stays smooth.

Can I copy a spotlight effect from another website?

Usually, yes. The Capture Motion Chrome extension sets a real :hover state during capture and records the transitions on the glow, such as its opacity fade, duration and easing. The glow position follows the pointer live, so pair the captured timing with the pattern in this guide.