AI CSS Animations

Border Draw Button

Two pseudo-elements draw the border around the button on hover: one grows along the top and right, the other along the bottom and left, meeting at the corners.

Also known as: animated border button, border trace hover.

HTML

<button class="hfx-border-draw">Explore</button>

CSS

.hfx-border-draw {
  --c: var(--hfx-color, #3b82f6);
  font-size: var(--hfx-size, 16px);
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.7em 1.6em;
  border-radius: 0.5em;
  font: 600 1em system-ui, sans-serif;
  cursor: pointer;
  position: relative;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--c);}
.hfx-border-draw::before,
.hfx-border-draw::after {
  content: "";
  position: absolute;
  width: 0;
  height: 0;
  border: 0 solid var(--c);
  /* hidden while idle: a zero-size box still paints a 2px corner dot */
  opacity: 0;
  transition: width 0.25s ease 0.25s, height 0.25s ease, opacity 0s 0.5s;
}
.hfx-border-draw::before {
  top: 0;
  left: 0;
  border-top-width: 2px;
  border-right-width: 2px;
}
.hfx-border-draw::after {
  bottom: 0;
  right: 0;
  border-bottom-width: 2px;
  border-left-width: 2px;
}
.hfx-border-draw:hover::before,
.hfx-border-draw:hover::after {
  width: 100%;
  height: 100%;
  opacity: 1;
  transition: width 0.25s ease, height 0.25s ease 0.25s, opacity 0s;
}

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

Customizing this snippet

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