AI CSS Animations

Animated Gradient Button

A pill whose three-color gradient flows sideways forever, so the call to action never sits still. The gradient tiles every 12em and a wider layer slides by one tile, so only transform moves.

Also known as: css button gradient animation, moving gradient button, gradient background animation button.

HTML

<button class="btn-animated-gradient" type="button">Start free trial</button>

CSS

.btn-animated-gradient {
  --c: var(--btn-color, #3b82f6);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  padding: 0.75em 1.6em;
  border: 0;
  border-radius: 0.6em;
  font: 600 var(--btn-size, 18px)/1.1 system-ui, sans-serif;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  position: relative;
  isolation: isolate;
  overflow: hidden;
  padding: 0.85em 1.9em;
  border-radius: 2em;
  background: var(--c);
  color: #fff;
  box-shadow: 0 0.55em 1.2em -0.55em color-mix(in srgb, var(--c) 80%, transparent);
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.25s ease;}
.btn-animated-gradient::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: -1;
  width: calc(100% + 12em);
  background: linear-gradient(90deg,
    var(--c),
    color-mix(in srgb, var(--c) 45%, #a855f7),
    color-mix(in srgb, var(--c) 35%, #ec4899),
    var(--c)) 0 0 / 12em 100%;
  animation: btn-animated-gradient-flow 4s linear infinite;
}
.btn-animated-gradient:is(:hover, :focus-visible) {
  box-shadow: 0 0.8em 1.6em -0.6em color-mix(in srgb, var(--c) 90%, transparent);
}
.btn-animated-gradient:active { transform: scale(0.96); }
@keyframes btn-animated-gradient-flow {
  to { transform: translateX(-12em); }
}

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

Customizing this snippet

  • Color: set --btn-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --btn-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 18px.
  • 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 buttons