AI CSS Animations

Snake Border

A line with a fading tail runs around the box like a snake and turns every corner. Four edge spans share one clock, each offset by its distance around the box, so the head never jumps.

Also known as: snake border animation css, running border animation, border trace animation.

Syncing files

HTML

<div class="bdr-snake-border">
  <span class="bdr-snake-border-top"></span>
  <span class="bdr-snake-border-right"></span>
  <span class="bdr-snake-border-bottom"></span>
  <span class="bdr-snake-border-left"></span>
  Syncing files
</div>

CSS

.bdr-snake-border {
  --c: var(--bdr-color, #3b82f6);
  font-size: var(--bdr-size, 16px);
  position: relative;
  display: grid;
  place-items: center;
  width: 15em;
  height: 8.5em;
  border-radius: 1em;
  font-family: system-ui, sans-serif;
  font-weight: 600;
  --t: 3.6s;
  --w: 0.2em;
  --tail: 5.5em;
  overflow: hidden;
  border-radius: 0.3em;
  background: color-mix(in srgb, var(--c) 6%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 18%, transparent);
}
.bdr-snake-border-top,
.bdr-snake-border-right,
.bdr-snake-border-bottom,
.bdr-snake-border-left {
  position: absolute;
  background-repeat: no-repeat;
  filter: drop-shadow(0 0 0.2em var(--c));
  animation: bdr-snake-border-x var(--t) linear infinite;
}
.bdr-snake-border-top,
.bdr-snake-border-bottom {
  left: 0;
  width: 100%;
  height: var(--w);
  background-image: linear-gradient(90deg, transparent, var(--c) 75%);
  background-size: var(--tail) 100%;
}
.bdr-snake-border-right,
.bdr-snake-border-left {
  top: 0;
  width: var(--w);
  height: 100%;
  background-image: linear-gradient(180deg, transparent, var(--c) 75%);
  background-size: 100% var(--tail);
  animation-name: bdr-snake-border-y;
}
.bdr-snake-border-top { top: 0; }
.bdr-snake-border-right { right: 0; animation-delay: calc(var(--t) * -0.681); }
.bdr-snake-border-bottom { bottom: 0; rotate: 180deg; animation-delay: calc(var(--t) * -0.5); }
.bdr-snake-border-left { left: 0; rotate: 180deg; animation-delay: calc(var(--t) * -0.181); }
/* The box is 15em by 8.5em, 47em around: an edge takes its share of the loop. */
@keyframes bdr-snake-border-x {
  0%           { transform: translateX(calc(var(--tail) * -1)); }
  31.9%        { transform: translateX(calc(100% - var(--tail))); }
  42.55%, 100% { transform: translateX(100%); }
}
@keyframes bdr-snake-border-y {
  0%           { transform: translateY(calc(var(--tail) * -1)); }
  18.09%       { transform: translateY(calc(100% - var(--tail))); }
  28.72%, 100% { transform: translateY(100%); }
}

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

Customizing this snippet

  • Color: set --bdr-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --bdr-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 border animations