AI CSS Animations

Input Focus Border

An input whose border draws itself around the field when it gets focus, with a soft ring fading in behind. It erases the same way on blur, because it is a transition on an SVG stroke.

Also known as: css input border animation, input box border animation css, input focus animation.

HTML

<label class="bdr-input-focus-border">
  <span class="bdr-input-focus-border-label">Email</span>
  <span class="bdr-input-focus-border-field">
    <input type="email" placeholder="you@example.com">
    <svg class="bdr-input-focus-border-svg" aria-hidden="true"><rect width="100%" height="100%" pathLength="100"/></svg>
  </span>
</label>

CSS

.bdr-input-focus-border {
  --c: var(--bdr-color, #3b82f6);
  font-size: var(--bdr-size, 16px);
  display: grid;
  gap: 0.45em;
  align-content: start;
  width: 15em;
  font-family: system-ui, sans-serif;
}
.bdr-input-focus-border-label {
  font-size: 0.8em;
  font-weight: 600;
  opacity: 0.75;
  transition: color 0.3s ease, opacity 0.3s ease;
}
.bdr-input-focus-border-field {
  position: relative;
  border-radius: 0.6em;
  transition: box-shadow 0.4s ease;
}
.bdr-input-focus-border input {
  display: block;
  box-sizing: border-box;
  width: 100%;
  padding: 0.75em 0.9em;
  border: 1px solid color-mix(in srgb, var(--c) 35%, transparent);
  border-radius: 0.6em;
  background: color-mix(in srgb, var(--c) 5%, transparent);
  color: inherit;
  font: inherit;
  outline: none;
  transition: border-color 0.3s ease;
}
.bdr-input-focus-border input::placeholder { color: inherit; opacity: 0.45; }
.bdr-input-focus-border-field:hover input { border-color: color-mix(in srgb, var(--c) 70%, transparent); }
.bdr-input-focus-border-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}
.bdr-input-focus-border-svg rect {
  rx: 0.6em;
  fill: none;
  stroke: var(--c);
  stroke-width: 0.13em;
  stroke-dasharray: 100 100;
  stroke-dashoffset: 100;
  transition: stroke-dashoffset 0.7s cubic-bezier(0.65, 0, 0.35, 1);
}
.bdr-input-focus-border:focus-within .bdr-input-focus-border-label { color: var(--c); opacity: 1; }
.bdr-input-focus-border-field:focus-within { box-shadow: 0 0 0 0.3em color-mix(in srgb, var(--c) 18%, transparent); }
.bdr-input-focus-border-field:focus-within rect { stroke-dashoffset: 0; }

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

Tailwind CSS version

The same effect using Tailwind utility classes. No custom CSS or config needed.

<label class="group grid w-60 gap-2 text-base">
  <span class="text-sm font-semibold opacity-75 transition group-focus-within:text-blue-500 group-focus-within:opacity-100">Email</span>
  <span class="relative rounded-[0.6em] transition-shadow duration-300 group-focus-within:shadow-[0_0_0_0.3em_rgb(59_130_246/0.18)]">
    <input type="email" placeholder="you@example.com" class="block w-full rounded-[0.6em] border border-blue-500/35 bg-blue-500/5 px-3.5 py-3 outline-none transition-colors placeholder:opacity-45 hover:border-blue-500/70">
    <svg class="pointer-events-none absolute inset-0 h-full w-full overflow-visible" aria-hidden="true"><rect width="100%" height="100%" pathLength="100" class="fill-none stroke-blue-500 [rx:0.6em] [stroke-dasharray:100_100] [stroke-dashoffset:100] [stroke-width:0.13em] transition-[stroke-dashoffset] duration-700 ease-[cubic-bezier(.65,0,.35,1)] group-focus-within:[stroke-dashoffset:0]"/></svg>
  </span>
</label>

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