AI CSS Animations

Rainbow Background

A full spectrum gradient whose hues roll continuously, the RGB look from gaming setups. A registered @property number turns every stop round the color wheel, so nothing slides.

Also known as: css background rainbow animation, rgb background animation css, hue shift background.

HTML

<div class="bgx-rainbow" aria-hidden="true"></div>

CSS

@property --bgx-rainbow-h {
  syntax: "<number>";
  inherits: false;
  initial-value: 0;
}
.bgx-rainbow {
  --c: var(--bgx-color, #3b82f6);
  font-size: var(--bgx-size, 16px);
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: linear-gradient(120deg,
    oklch(from var(--c) 0.7 0.19 calc(h + var(--bgx-rainbow-h))),
    oklch(from var(--c) 0.72 0.18 calc(h + var(--bgx-rainbow-h) + 60)),
    oklch(from var(--c) 0.8 0.17 calc(h + var(--bgx-rainbow-h) + 120)),
    oklch(from var(--c) 0.76 0.15 calc(h + var(--bgx-rainbow-h) + 180)),
    oklch(from var(--c) 0.7 0.17 calc(h + var(--bgx-rainbow-h) + 240)),
    oklch(from var(--c) 0.68 0.2 calc(h + var(--bgx-rainbow-h) + 300)));
  animation: bgx-rainbow-roll 8s linear infinite;
}
@keyframes bgx-rainbow-roll {
  to { --bgx-rainbow-h: 360; }
}

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

Customizing this snippet

  • Color: set --bgx-color on the element or a parent. Currently previewing #3b82f6.
  • Pattern scale: set --bgx-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 animated backgrounds