Animated Gradient Border in CSS
A thin light that keeps running around the edge of a button is one of the easiest ways to pull the eye to a call to action, without making the whole page louder.
This snippet does it in pure CSS: no JavaScript and no images. Below is the demo with the full code, then how it works and how to change its speed, color and thickness.
Demo:
How it works
The effect is three layers stacked on top of each other:
- The wrapper (
.animated-link-wrapper-demo) isposition: relative, so the effect layer can sit exactly on top of the button. The border is an absolutely positioned layer inside it; here’s how position absolute vs relative works. - The ring (
.animated-link-effect-demo) covers the button and haspadding: 1px. Its mask uses two layers, one for the content box and one for the full box, combined withmask-composite: exclude. Only the area where the two differ stays visible: the 1px padding ring. Everything inside it is cut away, so the button stays clickable and readable. - The light is a square
divinside the ring, filled with aconic-gradientthat is bright for a short slice and transparent everywhere else. Therotate-demokeyframes spin it one full turn every 2 seconds, so the bright slice travels around the ring.
Why scale(1.4)? The rotating square has to cover the button’s corners at every angle. A square needs to be about √2 ≈ 1.41 times bigger than the box it covers so no corner peeks out while it turns.
Change the speed, color and thickness
Three properties control the whole look:
- Speed: the
2sinanimation. Lower is faster; 3–4s feels calmer. - Thickness: the
paddingon the ring.2pxgives a bolder border. - Color: the colors in the
conic-gradient. Swap the white for your brand color, or use several colors for a rainbow trail.
.animated-link-effect-demo {
padding: 2px; /* thicker border */
}
.animated-link-effect-demo div {
background-image: conic-gradient(
from 0 at 50% 50%,
#a855f7 0deg,
transparent 60deg,
transparent 300deg,
#22d3ee 360deg
);
animation-duration: 3s; /* slower */
}
Respect reduced motion
Some people turn on “reduce motion” in their OS because constant movement makes them dizzy or distracts them. Stop the rotation for them. The gradient stays visible as a static highlight, so the button still stands out.
@media (prefers-reduced-motion: reduce) {
.animated-link-effect-demo div {
animation: none;
}
}
A shorter version with @property
Modern browsers can animate a custom property, so the same effect fits on a single element: no wrapper, no extra divs and no mask. The trick is two backgrounds. A solid one is clipped to the padding box, and the rotating gradient is clipped to the border box, so the gradient only shows through the transparent border.
It works in all current browsers. In older browsers the border just stays static.
@property --angle {
syntax: '<angle>';
initial-value: 0turn;
inherits: false;
}
.glow-button {
border: 1px solid transparent;
border-radius: 6px;
background:
linear-gradient(#141518, #141518) padding-box,
conic-gradient(from var(--angle), transparent 80%, #fff) border-box;
animation: border-spin 2s linear infinite;
}
@keyframes border-spin {
to {
--angle: 1turn;
}
}
Want more attention-grabbing effects? Try the CSS pulse animation, build your own buttons with the CSS button generator, or make the gradient first in the CSS gradient generator.