AI CSS Animations

Stopwatch Timer Animation

A running stopwatch: minutes and seconds tick while the hundredths blur past, and the hand on the icon jumps once a second. Three registered integers, each stepped at its own speed.

Also known as: stopwatch css, timer counting up, lap timer text.

HTML

<div class="txt-stopwatch-timer" aria-label="Stopwatch running">
  <svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="13.5" r="8"/><path d="M10 2.5h4M12 2.5v3"/><path class="txt-stopwatch-timer-hand" d="M12 13.5V8.5"/></svg>
  <span class="txt-stopwatch-timer-min" aria-hidden="true"></span><span aria-hidden="true">:</span><span class="txt-stopwatch-timer-sec" aria-hidden="true"></span><span class="txt-stopwatch-timer-hun" aria-hidden="true"></span>
</div>

CSS

@property --txt-stopwatch-timer-v {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
.txt-stopwatch-timer {
  --c: var(--txt-color, #3b82f6);
  font-size: calc(var(--txt-size, 32px) * 1.4);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  display: inline-flex;
  align-items: baseline;
  color: var(--c);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
  /* where the watch starts: 00:12.47 */
  --t: -12.47s;}
.txt-stopwatch-timer svg {
  align-self: center;
  width: 0.8em;
  height: 0.8em;
  margin-right: 0.25em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
}
.txt-stopwatch-timer-hand {
  transform-box: view-box;
  transform-origin: 50% 56.25%;
  animation: txt-stopwatch-timer-hand 60s steps(60) var(--t) infinite;
}
.txt-stopwatch-timer :is(.txt-stopwatch-timer-min, .txt-stopwatch-timer-sec, .txt-stopwatch-timer-hun) {
  counter-reset: txt-stopwatch-timer-v var(--txt-stopwatch-timer-v);
}
.txt-stopwatch-timer :is(.txt-stopwatch-timer-min, .txt-stopwatch-timer-sec)::after {
  content: counter(txt-stopwatch-timer-v, decimal-leading-zero);
}
.txt-stopwatch-timer-min { animation: txt-stopwatch-timer-count 3600s steps(60) var(--t) infinite; }
.txt-stopwatch-timer-sec { animation: txt-stopwatch-timer-count 60s steps(60) var(--t) infinite; }
.txt-stopwatch-timer-hun {
  font-size: 0.6em;
  opacity: 0.6;
  animation: txt-stopwatch-timer-hundredths 1s steps(100) var(--t) infinite;
}
.txt-stopwatch-timer-hun::after {
  content: "." counter(txt-stopwatch-timer-v, decimal-leading-zero);
}
@keyframes txt-stopwatch-timer-count {
  to { --txt-stopwatch-timer-v: 60; }
}
@keyframes txt-stopwatch-timer-hundredths {
  to { --txt-stopwatch-timer-v: 100; }
}
@keyframes txt-stopwatch-timer-hand {
  to { transform: rotate(360deg); }
}

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

Customizing this snippet

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