AI CSS Animations

Binary Counter Animation

Eight bits count upward in binary, each one flipping at half the speed of its right-hand neighbor, with the decimal value beside them. Every bit is one keyframe whose duration doubles.

Also known as: binary number animation, bits counting css, binary clock text.

HTML

<div class="txt-binary-counter" aria-label="Binary counter">
  <span class="txt-binary-counter-bits" aria-hidden="true"><i style="--w: 128"></i><i style="--w: 64"></i><i style="--w: 32"></i><i style="--w: 16"></i><i style="--w: 8"></i><i style="--w: 4"></i><i style="--w: 2"></i><i style="--w: 1"></i></span>
  <b class="txt-binary-counter-dec" aria-hidden="true"></b>
</div>

CSS

@property --txt-binary-counter-v {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}
.txt-binary-counter {
  --c: var(--txt-color, #3b82f6);
  font-size: var(--txt-size, 32px);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  line-height: 1.2;
  display: inline-flex;
  align-items: center;
  gap: 0.45em;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --tick: 0.5s;
  /* start from 45 (00101101) instead of zero */
  --from: calc(var(--tick) * -45);}
.txt-binary-counter-bits {
  display: flex;
  gap: 0.1em;
}
.txt-binary-counter-bits i:nth-child(4) {
  margin-right: 0.22em;
}
.txt-binary-counter-bits i::before {
  content: "0";
  display: grid;
  place-items: center;
  width: 0.95em;
  height: 1.3em;
  border-radius: 0.18em;
  font-style: normal;
  animation: txt-binary-counter-bit calc(var(--tick) * 2 * var(--w)) step-end var(--from) infinite;
}
.txt-binary-counter-dec {
  width: 5ch;
  color: var(--c);
  counter-reset: txt-binary-counter-n var(--txt-binary-counter-v);
  animation: txt-binary-counter-count calc(var(--tick) * 256) steps(256) var(--from) infinite;
}
.txt-binary-counter-dec::after {
  content: "= " counter(txt-binary-counter-n);
}
@keyframes txt-binary-counter-bit {
  0%, 49% { content: "0"; color: color-mix(in srgb, var(--c) 55%, transparent); background: color-mix(in srgb, var(--c) 10%, transparent); }
  50%, 100% { content: "1"; color: #fff; background: var(--c); }
}
@keyframes txt-binary-counter-count {
  to { --txt-binary-counter-v: 256; }
}

@media (prefers-reduced-motion: reduce) {
  .txt-binary-counter, .txt-binary-counter *, .txt-binary-counter::before, .txt-binary-counter::after, .txt-binary-counter *::before, .txt-binary-counter *::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