AI CSS Animations

Download Button with Progress

Click to download: the button darkens into a track, fills left to right while the percentage counts up and the arrow bobs, then shows a check. The number is a registered @property integer.

Also known as: download button animation css, download progress button, css button progress bar.

HTML

<label class="btn-download-progress">
  <input class="btn-download-progress-input" type="checkbox" aria-label="Download">
  <span class="btn-download-progress-face" aria-hidden="true">
    <span class="btn-download-progress-fill"></span>
    <svg class="btn-download-progress-icon" viewBox="0 0 24 24">
      <path class="btn-download-progress-arrow" d="M12 3.5v11M7 10l5 5 5-5"/>
      <path class="btn-download-progress-check" d="M6.5 10.5l3.8 3.8L17.5 7"/>
      <path d="M4.5 20h15"/>
    </svg>
    <span class="btn-download-progress-text">
      <span class="btn-download-progress-idle">Download</span>
      <span class="btn-download-progress-busy"></span>
      <span class="btn-download-progress-done">Saved</span>
    </span>
  </span>
</label>

CSS

@property --btn-download-progress-p {
  syntax: "<integer>";
  inherits: false;
  initial-value: 0;
}
.btn-download-progress {
  --c: var(--btn-color, #3b82f6);
  position: relative;
  display: inline-grid;
  place-items: center;
  font: 600 var(--btn-size, 18px)/1.1 system-ui, sans-serif;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.btn-download-progress-input {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.btn-download-progress-face {
  position: relative;
  isolation: isolate;
  overflow: hidden;
  display: inline-flex;
  align-items: center;
  gap: 0.6em;
  width: 9.4em;
  box-sizing: border-box;
  padding: 0.8em 1.25em;
  border-radius: 0.7em;
  background-color: var(--c);
  color: #fff;
  transition: background-color 0.3s ease, transform 0.2s ease;
}
.btn-download-progress-fill {
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--c);
  transform: scaleX(0);
  transform-origin: left;
}
.btn-download-progress-icon {
  flex: none;
  width: 1.25em;
  height: 1.25em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
  overflow: visible;
}
.btn-download-progress-check {
  stroke-dasharray: 17;
  stroke-dashoffset: 17;
}
.btn-download-progress-text {
  display: grid;
  flex: 1;
  justify-items: center;
}
.btn-download-progress-text > span { grid-area: 1 / 1; }
.btn-download-progress-busy,
.btn-download-progress-done { opacity: 0; }
.btn-download-progress-busy {
  counter-reset: btn-download-progress-n var(--btn-download-progress-p);
  font-variant-numeric: tabular-nums;
}
.btn-download-progress-busy::after { content: counter(btn-download-progress-n) "%"; }
.btn-download-progress:active .btn-download-progress-face { transform: scale(0.96); }
.btn-download-progress-input:focus-visible ~ .btn-download-progress-face {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}
.btn-download-progress-input:checked ~ .btn-download-progress-face {
  background-color: color-mix(in srgb, var(--c) 35%, #0b1120);
}
.btn-download-progress-input:checked ~ .btn-download-progress-face .btn-download-progress-fill {
  animation: btn-download-progress-fill 2.4s linear forwards;
}
.btn-download-progress-input:checked ~ .btn-download-progress-face .btn-download-progress-busy {
  animation:
    btn-download-progress-count 2.4s linear forwards,
    btn-download-progress-busy 2.6s ease forwards;
}
.btn-download-progress-input:checked ~ .btn-download-progress-face .btn-download-progress-idle {
  animation: btn-download-progress-out 0.15s ease-in forwards;
}
.btn-download-progress-input:checked ~ .btn-download-progress-face .btn-download-progress-done {
  animation: btn-download-progress-in 0.35s cubic-bezier(0.3, 0.7, 0.4, 1.4) 2.5s forwards;
}
.btn-download-progress-input:checked ~ .btn-download-progress-face .btn-download-progress-arrow {
  transform-box: fill-box;
  animation:
    btn-download-progress-bob 0.6s ease-in-out 4,
    btn-download-progress-out 0.2s ease-in 2.4s forwards;
}
.btn-download-progress-input:checked ~ .btn-download-progress-face .btn-download-progress-check {
  animation: btn-download-progress-draw 0.35s ease-out 2.5s forwards;
}
@keyframes btn-download-progress-fill {
  0% { transform: scaleX(0); }
  20% { transform: scaleX(0.22); }
  38% { transform: scaleX(0.3); }
  60% { transform: scaleX(0.66); }
  75% { transform: scaleX(0.72); }
  92%, 100% { transform: scaleX(1); }
}
@keyframes btn-download-progress-count {
  0% { --btn-download-progress-p: 0; }
  20% { --btn-download-progress-p: 22; }
  38% { --btn-download-progress-p: 30; }
  60% { --btn-download-progress-p: 66; }
  75% { --btn-download-progress-p: 72; }
  92%, 100% { --btn-download-progress-p: 100; }
}
@keyframes btn-download-progress-busy {
  0%, 6%, 100% { opacity: 0; }
  13%, 90% { opacity: 1; }
}
@keyframes btn-download-progress-out {
  to { opacity: 0; transform: translateY(-0.4em); }
}
@keyframes btn-download-progress-in {
  from { opacity: 0; transform: translateY(0.5em); }
  to { opacity: 1; transform: none; }
}
@keyframes btn-download-progress-bob {
  50% { transform: translateY(18%); }
}
@keyframes btn-download-progress-draw {
  to { stroke-dashoffset: 0; }
}

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

Customizing this snippet

  • Color: set --btn-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --btn-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 18px.
  • 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 buttons