AI CSS Animations

Marching Ants Border Button

A dashed outline like a selection box. On hover the dashes march clockwise around the button and the inside tints. The dashes are four background gradients, each moved by one tile per loop.

Also known as: animated dashed border, dashed border animation css, moving dashed outline.

HTML

<button class="btn-marching-ants">Select area</button>

CSS

.btn-marching-ants {
  --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;
  padding: 0.8em 1.7em;
  border: 0;
  border-radius: 0;
  color: var(--c);
  background-color: transparent;
  background-image:
    linear-gradient(90deg, var(--c) 50%, transparent 0),
    linear-gradient(90deg, var(--c) 50%, transparent 0),
    linear-gradient(0deg, var(--c) 50%, transparent 0),
    linear-gradient(0deg, var(--c) 50%, transparent 0);
  background-repeat: repeat-x, repeat-x, repeat-y, repeat-y;
  background-size: 0.8em 2px, 0.8em 2px, 2px 0.8em, 2px 0.8em;
  background-position: 0 0, 0 100%, 0 0, 100% 0;
  transition: background-color 0.3s ease;}
.btn-marching-ants:is(:hover, :focus-visible) {
  background-color: color-mix(in srgb, var(--c) 10%, transparent);
  animation: btn-marching-ants-march 0.5s linear infinite;
}
@keyframes btn-marching-ants-march {
  to { background-position: 0.8em 0, -0.8em 100%, 0 -0.8em, 100% 0.8em; }
}

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