Colors run around the whole perimeter at one steady speed, turning each corner without speeding up. Four gradient bands, one per side, move in step, which a single rotating conic cannot do.
Also known as: moving gradient around border css, css border loop animation, gradient running along border.
<div class="bdr-running-gradient-border">Running gradient</div>
.bdr-running-gradient-border {
--c: var(--bdr-color, #3b82f6);
font-size: var(--bdr-size, 16px);
position: relative;
display: grid;
place-items: center;
width: 15em;
height: 8.5em;
border-radius: 1em;
font-family: system-ui, sans-serif;
font-weight: 600;
--run: var(--c), color-mix(in oklch, var(--c), #ec4899 80%), color-mix(in oklch, var(--c), #f59e0b 75%), var(--c);
box-sizing: border-box;
border: 0.22em solid transparent;
background:
linear-gradient(color-mix(in srgb, var(--c) 10%, #0b0f19), color-mix(in srgb, var(--c) 10%, #0b0f19)) padding-box,
linear-gradient(90deg, var(--run)) 0 0 / 11.75em 1em repeat-x border-box,
linear-gradient(180deg, var(--run)) 100% 8.5em / 1em 11.75em repeat-y border-box,
linear-gradient(270deg, var(--run)) 3.25em 100% / 11.75em 1em repeat-x border-box,
linear-gradient(0deg, var(--run)) 0 0 / 1em 11.75em repeat-y border-box;
color: #f1f5f9;
animation: bdr-running-gradient-border-run 3s linear infinite;
}
@keyframes bdr-running-gradient-border-run {
to { background-position: 0 0, 11.75em 0, 100% 20.25em, -8.5em 100%, 0 -11.75em; }
}
@media (prefers-reduced-motion: reduce) {
.bdr-running-gradient-border, .bdr-running-gradient-border *, .bdr-running-gradient-border::before, .bdr-running-gradient-border::after, .bdr-running-gradient-border *::before, .bdr-running-gradient-border *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--bdr-color on the element or a parent.
Currently previewing #3b82f6.--bdr-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.