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.
<button class="btn-marching-ants">Select area</button>
.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;
}
}--btn-color on the element or a parent.
Currently previewing #3b82f6.--btn-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 18px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.