AI CSS Animations

Gradient Outline Button

A ghost button whose outline and label share one gradient. On hover the colors flow through both and a faint tint fills in. The outline is a masked pseudo-element, so the inside stays see-through.

Also known as: gradient text button, gradient border button css, gradient ghost button.

HTML

<button class="btn-gradient-outline"><span class="btn-gradient-outline-label">Read the docs</span></button>

CSS

.btn-gradient-outline {
  --c: var(--btn-color, #3b82f6);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5em;
  font: 600 var(--btn-size, 18px)/1.2 system-ui, sans-serif;
  cursor: pointer;
  --g: linear-gradient(90deg, var(--c), color-mix(in srgb, var(--c) 45%, #d946ef), color-mix(in srgb, var(--c) 30%, #f43f5e), color-mix(in srgb, var(--c) 45%, #d946ef), var(--c));
  position: relative;
  padding: 0.8em 1.8em;
  border: 0;
  border-radius: 0.6em;
  background-color: transparent;
  transition: background-color 0.3s ease;}
.btn-gradient-outline::before {
  content: "";
  position: absolute;
  inset: 0;
  padding: 2px;
  border-radius: inherit;
  background: var(--g) 0 0 / 200% 100%;
  -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, linear-gradient(#000 0 0);
  mask-composite: exclude;
  pointer-events: none;
}
.btn-gradient-outline-label {
  background: var(--g) 0 0 / 200% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.btn-gradient-outline:is(:hover, :focus-visible) { background-color: color-mix(in srgb, var(--c) 8%, transparent); }
.btn-gradient-outline:is(:hover, :focus-visible)::before,
.btn-gradient-outline:is(:hover, :focus-visible) .btn-gradient-outline-label {
  animation: btn-gradient-outline-flow 2.4s linear infinite;
}
@keyframes btn-gradient-outline-flow {
  to { background-position: 200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .btn-gradient-outline, .btn-gradient-outline *, .btn-gradient-outline::before, .btn-gradient-outline::after, .btn-gradient-outline *::before, .btn-gradient-outline *::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