AI CSS Animations

Auto Width Rotating Text

The chip resizes to hug each word as it rolls in, so the sentence closes up around short words instead of leaving a gap. A monospace chip lets width animate in exact ch steps per word.

Also known as: rotating text dynamic width, resizing word rotator, text rotator that fits each word.

I'm a designerdevwriterfounder

HTML

<div class="txt-auto-width-rotating-text">I'm a
  <span class="txt-auto-width-rotating-text-chip">
    <span class="txt-auto-width-rotating-text-track">
      <span>designer</span><span>dev</span><span>writer</span><span>founder</span><span aria-hidden="true">designer</span>
    </span>
  </span>
</div>

CSS

.txt-auto-width-rotating-text {
  --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: flex;
  align-items: center;
  gap: 0.3em;
  white-space: nowrap;}
.txt-auto-width-rotating-text-chip {
  display: block;
  /* width in ch = letters in the word; content-box keeps the padding outside it */
  box-sizing: content-box;
  width: 8ch;
  height: 1.2em;
  padding: 0.04em 0.3em;
  overflow: hidden;
  border-radius: 0.22em;
  background: color-mix(in srgb, var(--c) 14%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 30%, transparent);
  color: var(--c);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  /* 4 words x 2s = 8s */
  animation: txt-auto-width-rotating-text-fit 8s cubic-bezier(0.65, 0, 0.35, 1) infinite;
}
.txt-auto-width-rotating-text-track {
  display: flex;
  flex-direction: column;
  animation: txt-auto-width-rotating-text-roll 8s cubic-bezier(0.65, 0, 0.35, 1) infinite;
}
.txt-auto-width-rotating-text-track span {
  height: 1.2em;
}
@keyframes txt-auto-width-rotating-text-fit {
  0%, 18.13%   { width: 8ch; }
  25%, 43.13%  { width: 3ch; }
  50%, 68.13%  { width: 6ch; }
  75%, 93.13%  { width: 7ch; }
  100%         { width: 8ch; }
}
@keyframes txt-auto-width-rotating-text-roll {
  0%, 18.13%   { transform: translateY(0); }
  25%, 43.13%  { transform: translateY(-1.2em); }
  50%, 68.13%  { transform: translateY(-2.4em); }
  75%, 93.13%  { transform: translateY(-3.6em); }
  100%         { transform: translateY(-4.8em); }
}

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