Animated Gradient Borders With @property: A Spinning Highlight Without Extra Elements

A rotating gradient border — light sweeping around the edge of a card — is a popular way to mark a featured plan, a live item or a call to action. The usual implementations either rotate an oversized pseudo-element behind the card and hide the excess with overflow: hidden, or use border-image and lose rounded corners. The narrow problem this page solves is a rounded, crisp gradient border that animates with one element and a few declarations, driven by a registered custom property. It belongs to Clip-Path & Mask Animations in the CSS-Only Micro-Interactions & Animations guide.

Why a registered property

A conic-gradient() has a starting angle: conic-gradient(from 90deg, …). Rotating the border means animating that angle. But the gradient is an image, and images do not interpolate — writing two keyframes with different gradients produces a hard switch at the halfway point.

The standard workaround is to rotate the element that paints the gradient with transform: rotate(), which does interpolate. That works for a square pseudo-element hidden behind a card, but it wastes pixels painting the corners that get clipped away, and the rotating square's diagonal must exceed the card's, so it has to be larger than the card by a factor that depends on the aspect ratio.

The cleaner route is to put the angle in a custom property and register it:

@property --angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0deg;
}

Registered with an <angle> syntax, --angle becomes a typed value that the animation engine can interpolate. The gradient reads var(--angle), so each frame the browser computes a new angle and repaints the gradient with it. No transform, no oversized element. The general mechanism is explained in Animating Custom Properties With @property.

Registered versus unregistered interpolation Two graphs of the angle value over one animation cycle. The unregistered property stays at zero degrees and jumps to 360 degrees halfway through. The registered property rises smoothly from zero to 360 degrees. Value of --angle over one cycle unregistered: flips at 50% 360deg 0deg registered <angle>: smooth 360deg 0deg

The complete implementation

The card below paints its gradient border with two background layers and a transparent border. Hover or focus the card to set the sweep in motion.

Live demoConic-gradient border driven by a registered angle
Hover or Tab to the card: the gradient sweeps around the rounded border, and pauses where it is when you leave.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Animated gradient border</title>
<style>
  body { font: 16px/1.5 system-ui, sans-serif; margin: 2rem; background: #0b1020; color: #e2e8f0; }

  @property --angle {
    syntax: "<angle>";
    inherits: false;
    initial-value: 0deg;
  }

  .featured {
    display: block;
    max-width: 20rem;
    padding: 1.5rem;
    border-radius: 16px;
    color: inherit;
    text-decoration: none;

    /* A transparent border reserves the ring's width... */
    border: 3px solid transparent;

    /* ...and two background layers fill it:
       1. the card surface, clipped to the PADDING box (inside the border);
       2. the conic gradient, clipped to the BORDER box (so it shows
          only through the transparent border ring). */
    background:
      linear-gradient(#111827, #111827) padding-box,
      conic-gradient(from var(--angle), #22d3ee, #a78bfa, #f472b6, #22d3ee) border-box;

    animation: spin 4s linear infinite;
    animation-play-state: paused;          /* still until interacted with */
  }

  .featured:hover,
  .featured:focus-visible {
    animation-play-state: running;
  }

  /* Keyboard focus also gets a conventional outline, because a moving
     border alone is not a reliable focus indicator. */
  .featured:focus-visible {
    outline: 2px solid #f8fafc;
    outline-offset: 4px;
  }

  @keyframes spin {
    to { --angle: 360deg; }
  }

  @media (prefers-reduced-motion: reduce) {
    .featured { animation: none; }
  }
</style>
</head>
<body>
  <a class="featured" href="/pricing/team/">
    <strong>Team plan</strong>
    <p>Most popular for groups of five to fifty.</p>
  </a>
</body>
</html>

animation-play-state: paused on the base rule and running under interaction means the gradient stays where it was when the pointer leaves, rather than snapping back to zero, which would read as a glitch. If the border should rotate continuously for a "live" indicator, drop the paused state — but consider the performance and motion notes below before doing so.

The key technique: background-clip layers

The border itself is drawn by a technique that has nothing to do with animation and is worth knowing in its own right. Backgrounds can be clipped to different boxes per layer. The first layer, the card surface, is clipped to padding-box, so it stops at the inner edge of the border. The second layer, the gradient, is clipped to border-box, so it extends under the border. Because the border is transparent, the only place the gradient is visible is the ring between the two boxes.

Two background layers make the ring Bottom layer: a gradient filling the whole border box. Top layer: a solid surface filling only the padding box. Seen together through a transparent border, only a ring of gradient remains visible. Surface over gradient, clipped to different boxes gradient · border-box + surface · padding-box = card only the ring shows Both layers follow border-radius, so corners stay round.

This is why the approach beats border-image: backgrounds follow border-radius, and border-image does not. It also means the surface layer must be an imagelinear-gradient(#111827, #111827) — rather than a plain colour, because only the final background layer can be a bare colour, and it is the gradient that sits last here.

The one limitation is transparency. The card surface must be opaque, because it is painted on top of the gradient; a translucent surface would let the gradient show through the whole card. For a see-through card with a gradient ring, use a mask instead: paint the gradient on a pseudo-element and cut out the middle with mask-composite: exclude over two layers clipped to different boxes — the same composition idea described in Fade Edges With mask-image.

Performance and motion considerations

Each frame of the animation changes --angle, which changes the background image, which requires a repaint of the card's background. That work is small for one card and grows linearly with the number of spinning borders on screen. Three habits keep it in check:

  • Animate on interaction, not forever. The paused-by-default pattern above means at most one card animates at a time.
  • Keep continuous spinners rare and small. A single "live" badge is fine; a grid of twelve spinning cards is not, and it is visually exhausting too.
  • Honour reduced motion. A perpetually rotating border is exactly the kind of non-essential, looping motion that prefers-reduced-motion users are opting out of. Under WCAG 2.2.2, anything that moves automatically for more than five seconds needs a way to pause it; not animating at all by default avoids the question.

A moving border is also not a focus indicator. It can be subtle in some frames and loud in others, and users who have disabled animation see a static border that may not change at all on focus. Always pair it with a real outline, as the example does, following Creating Accessible Focus Indicators.

Theming the ring for light and dark

The example hard-codes a dark card surface, which is the setting where glowing borders look best. On a site with both themes, the surface layer must follow the theme or the card will show a dark panel on a light page. Because the surface is a background image layer, the cleanest approach is to drive its colour from a custom property that the theme already defines.

.featured {
  --surface: #ffffff;
  background:
    linear-gradient(var(--surface), var(--surface)) padding-box,
    conic-gradient(from var(--angle), #0891b2, #7c3aed, #db2777, #0891b2) border-box;
}

[data-theme="dark"] .featured {
  --surface: #111827;
  background:
    linear-gradient(var(--surface), var(--surface)) padding-box,
    conic-gradient(from var(--angle), #22d3ee, #a78bfa, #f472b6, #22d3ee) border-box;
}

Note that the gradient colours change between themes as well. Pastel stops that glow against a dark surface nearly vanish against white, and the ring is the element's main affordance, so it must meet the 3:1 non-text contrast guideline of WCAG 1.4.11 against the page in both themes. Deeper, more saturated stops for the light theme keep the ring visible. Check contrast at the dimmest point of the gradient, not the brightest, because a rotating ring spends time at every angle.

Variation: a travelling highlight instead of a full spin

A full rainbow sweep suits a playful brand. A calmer variant keeps the border mostly neutral and sends a short bright segment around it, like light catching an edge.

.subtle {
  border: 2px solid transparent;
  background:
    linear-gradient(#111827, #111827) padding-box,
    conic-gradient(
      from var(--angle),
      #334155 0deg,
      #334155 300deg,
      #93c5fd 330deg,     /* the bright segment */
      #334155 360deg
    ) border-box;
  animation: spin 6s linear infinite paused;
}

.subtle:hover,
.subtle:focus-visible { animation-play-state: running; }

Because the gradient is mostly a single colour, the rotation is visible only as the highlight travelling around the edge. Slowing the cycle to six seconds makes it feel ambient rather than urgent.

Browser support

@property is supported in Chrome and Edge 85+, Firefox 128+ and Safari 16.4+. In browsers without it, --angle is unregistered: the border still renders with its initial gradient, it simply does not rotate — an acceptable fallback. conic-gradient() and per-layer background-clip are supported in all current engines. prefers-reduced-motion is supported in Chrome 74+, Edge 79+, Firefox 63+ and Safari 10.1+.

FAQ

Why does my conic-gradient border not animate? Because the angle lives in an unregistered custom property. The browser treats an unregistered property as an untyped token list and cannot interpolate it, so the value jumps at the end of the animation. Register it with @property and syntax: "<angle>" and it animates smoothly.

Can border-image be used for an animated gradient border?border-image can display a gradient border, but it ignores border-radius, so rounded cards get square gradient corners. The background-layer technique on this page respects border-radius and works with any gradient.

Is a spinning border bad for performance? The angle change repaints the gradient each frame, which is cheap for a single small card and noticeable if dozens run at once. Limit it to one highlighted element, pause it off screen or when not hovered, and never run it for users who prefer reduced motion.

How do I make the border only animate on hover or focus? Declare the animation on the element but set animation-play-state: paused, then switch it to running under :hover and :focus-visible. The border keeps its position between hovers instead of snapping back to zero.

Related articles

More pages in the same section.