AI CSS Animations

Color Changing Border

The border, its tinted fill and the label glide through the color wheel together, one hue at a time. A hue-rotate() filter loop starts from your accent color and works in every browser.

Also known as: css border color animation, rainbow color cycle border, hue shift border.

Color changing

HTML

<div class="bdr-color-changing-border"><span class="bdr-color-changing-border-label">Color changing</span></div>

CSS

.bdr-color-changing-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;
  background: color-mix(in srgb, var(--c) 12%, transparent);
  animation: bdr-color-changing-border-cycle 6s linear infinite;
}
.bdr-color-changing-border::before {
  content: "";
  position: absolute;
  inset: 0;
  padding: 0.2em;
  border-radius: inherit;
  background: linear-gradient(135deg, var(--c), color-mix(in oklch, var(--c), #a855f7 45%));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box exclude, linear-gradient(#000 0 0);
  pointer-events: none;
}
.bdr-color-changing-border-label {
  color: color-mix(in srgb, var(--c) 70%, currentColor);
}
@keyframes bdr-color-changing-border-cycle {
  to { filter: hue-rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  .bdr-color-changing-border, .bdr-color-changing-border *, .bdr-color-changing-border::before, .bdr-color-changing-border::after, .bdr-color-changing-border *::before, .bdr-color-changing-border *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --bdr-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --bdr-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar border animations