CSS Marching Ants Border

An animated dashed border whose dashes crawl around the box like a selection marquee, for drop zones and crops.

  • marching ants
  • animated dashed border
  • selection
  • drop target
  • gradient
  • keyframes
HTML
<div class="ants-border">Drop files here</div>
CSS
.ants-border {
  --c: #2563eb;
  display: grid;
  place-items: center;
  width: 240px;
  height: 120px;
  background:
    linear-gradient(90deg, var(--c) 50%, transparent 0) repeat-x 0 0 / 14px 2px,
    linear-gradient(90deg, var(--c) 50%, transparent 0) repeat-x 0 100% / 14px 2px,
    linear-gradient(0deg, var(--c) 50%, transparent 0) repeat-y 0 0 / 2px 14px,
    linear-gradient(0deg, var(--c) 50%, transparent 0) repeat-y 100% 0 / 2px 14px,
    #eff6ff;
  color: #1e40af;
  font: 600 15px system-ui, sans-serif;
  animation: ants-border-march 0.6s linear infinite;
}

@keyframes ants-border-march {
  to {
    background-position: 14px 0, -14px 100%, 0 -14px, 100% 14px, 0 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .ants-border {
    animation: none;
  }
}

About this CSS Marching Ants Border

This CSS Marching Ants Border is 28 lines of pure CSS (0.8 KB) with no JavaScript and no dependencies. It uses keyframe animations, linear gradients, CSS Grid and custom properties. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

Change these custom properties to restyle the marching ants border without touching the rest of the CSS:

PropertyDefault
--c#2563eb

How to use this marching ants border

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .ants-border class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback