AI CSS Animations

Animated Grid Background

Thin grid lines pan diagonally under a soft top glow and fade out toward the edges, the dark hero look of many developer sites. The layer moves exactly one cell per loop, so it never jumps.

Also known as: moving grid background css, animated grid lines, css grid pattern background.

HTML

<div class="bgx-animated-grid" aria-hidden="true">
  <span class="bgx-animated-grid-lines"></span>
</div>

CSS

.bgx-animated-grid {
  --c: var(--bgx-color, #3b82f6);
  font-size: var(--bgx-size, 16px);
  position: absolute;
  inset: 0;
  overflow: hidden;
  --cell: 2.5em;
  background:
    radial-gradient(ellipse 70% 60% at 50% 0%, color-mix(in srgb, var(--c) 30%, transparent), transparent 70%),
    color-mix(in srgb, var(--c) 8%, #05070c);
}
.bgx-animated-grid-lines {
  position: absolute;
  inset: 0;
  -webkit-mask: radial-gradient(ellipse 75% 85% at 50% 35%, #000 25%, transparent 78%);
  mask: radial-gradient(ellipse 75% 85% at 50% 35%, #000 25%, transparent 78%);
}
.bgx-animated-grid-lines::before {
  content: "";
  position: absolute;
  inset: calc(var(--cell) * -1) 0 0 calc(var(--cell) * -1);
  background:
    linear-gradient(color-mix(in srgb, var(--c) 50%, transparent) 0 1px, transparent 2px) 0 0 / var(--cell) var(--cell),
    linear-gradient(90deg, color-mix(in srgb, var(--c) 50%, transparent) 0 1px, transparent 2px) 0 0 / var(--cell) var(--cell);
  animation: bgx-animated-grid-pan 3s linear infinite;
}
@keyframes bgx-animated-grid-pan {
  to { transform: translate(var(--cell), var(--cell)); }
}

@media (prefers-reduced-motion: reduce) {
  .bgx-animated-grid, .bgx-animated-grid *, .bgx-animated-grid::before, .bgx-animated-grid::after, .bgx-animated-grid *::before, .bgx-animated-grid *::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