Theme
Design your own
On this page (9)
Component Skill — components/text-rotate/component-skill.md

Native basis

A one-line window (height: 1lh; overflow: hidden) around a stack of lines - any number; registered counters (@property) animate the round and each line's move is computed from them and the line count (sibling-count()).

Web Platform APIs

lh:has()animation-play-stateprefers-reduced-motion

Classes

.text-rotate

Data attributes

data-direction="down", data-effect="fade", data-align (center, end), data-pause="none"; the duration of one round: --text-rotate-duration (default 10s).

§Default

Three lines in a big headline: each holds for its share of the 10s round, then rolls up to the next; after the last it rolls back to the first. Hover to pause.

§In a sentence

Inline in running text - the window takes the sentence's font and sits on its baseline; each line can have its own color. The window is as wide as its longest line, so the rotating word reads best at the end of a sentence.

§Centered headline

data-align='center' centers shorter lines in the window; emojis are just text.

§Duration

--text-rotate-duration is one full round through all lines (default 10s): here 4s and 16s.

§Direction and fade

data-direction='down' drops each next line in from above (same reading order); data-effect='fade' crossfades in place with a small drift.

§Line height

The window is 1lh - one line of whatever line-height the text has, so a loose line-height gives each line more room. Keep it at 1.2 or more: tighter than the glyphs themselves, the edges of the neighbouring lines peek into the window.

§Six lines, never pausing

data-pause='none' keeps it rolling under the pointer (a ticker).

§Twenty lines

Any number of lines works the same way - twenty here, two seconds each (--text-rotate-duration: 40s).

§CSS view file

/* -- Text Rotate component ------------------------------------------ */
/* the two counters the loop runs on - registered so they animate, inherited so every line reads them */
@property --_text-rotate-i {
  syntax: "<integer>";
  inherits: true;
  initial-value: 0;
}
/* the line count and a line's index, registered as integers: a value from sibling-count() / sibling-index() computes to
   a number before steps() and calc() read it (VERIFIED: Firefox kept the raw sibling-count() text, and
   steps(sibling-count()) dropped the animation) */
@property --_text-rotate-n {
  syntax: "<integer>";
  inherits: true;
  initial-value: 1;
}
@property --_text-rotate-k {
  syntax: "<integer>";
  inherits: false;
  initial-value: 0;
}
/* the round's progress, 0 to 1 in steps of 1/n - literal keyframes (VERIFIED: Firefox does not interpolate a keyframe
   value written as var(), it flipped at half time) */
@property --_text-rotate-t {
  syntax: "<number>";
  inherits: true;
  initial-value: 0;
}
@property --_text-rotate-e {
  syntax: "<number>";
  inherits: true;
  initial-value: 0;
}
@layer components {
  /* daisyUI's text-rotate, CSS only: a .text-rotate holds one <span> of lines (each a child element, any number from
     2); one line shows at a time and the stack rolls on to the next - an infinite loop that pauses while hovered. The
     window is one line tall (1lh), so it works at any font-size and line-height, inline in a sentence or as a
     headline. --text-rotate-duration sets one full round (default 10s). data-direction="down" rolls the other way,
     data-effect="fade" crossfades in place, data-pause="none" keeps it running under the pointer. Reduced motion shows
     the first line only.
     One mechanism for every line count: two registered counters animate on every line, in step - --_text-rotate-t,
     the round's progress (0, 1/n, 2/n ... in steps(n); times n it is i, the line shown), and --_text-rotate-e, the roll
     at the end of each slot (0 while the line holds, easing to 1 in the slot's last 8%). Every line moves by i + e
     lines, so the stack rolls as one; in the last slot it rolls back to the first line. A fade reads the same counters on every line. n is the line count (sibling-count(), or the :has()
     rules below), k a line's index (sibling-index(), or :nth-child()). */
  .text-rotate {
    --_text-rotate-n: 1;
    --_dur: var(--text-rotate-duration, 10s);
    display: inline-grid;
    height: 1lh;
    overflow: hidden;
    vertical-align: bottom;
    & > span {
      display: flex;
      flex-direction: column;
    }
    /* every line runs the same two animations (in step - they start together) and moves by the same amount, so the
       stack rolls as one; a line's own sibling-count() is the line count */
    & > span > * {
      --_text-rotate-k: 0;
      /* the line shown, 0 to n - 1 (an integer: the progress times n, rounded) */
      --_text-rotate-i: calc(var(--_text-rotate-t) * var(--_text-rotate-n));
      /* 1 in the last slot: the roll goes back to the first line */
      --_last: clamp(0, var(--_text-rotate-i) - var(--_text-rotate-n) + 2, 1);
      /* how many lines the stack has moved up: i + e, and back to 0 across the last slot */
      --_p: calc(var(--_text-rotate-i) + var(--_text-rotate-e) * (1 - var(--_text-rotate-n) * var(--_last)));
      display: block;
      height: 1lh;
      white-space: nowrap;
      translate: 0 calc(var(--_p) * -1lh);
      animation:
        text-rotate-index var(--_dur) steps(var(--_text-rotate-n)) infinite,
        text-rotate-roll calc(var(--_dur) / var(--_text-rotate-n)) infinite;
    }
    &[data-align="center"] > span > * {
      text-align: center;
    }
    &[data-align="end"] > span > * {
      text-align: end;
    }
    &:hover > span > *,
    &:focus-within > span > * {
      animation-play-state: paused;
    }
    &[data-pause="none"]:hover > span > *,
    &[data-pause="none"]:focus-within > span > * {
      animation-play-state: running;
    }
    /* down: the lines stack bottom up and the stack moves down - line i sits n - 1 - i lines from the top */
    &[data-direction="down"] > span {
      flex-direction: column-reverse;
    }
    &[data-direction="down"] > span > * {
      translate: 0 calc((var(--_text-rotate-n) - 1 - var(--_p)) * -1lh);
    }
    /* fade: every line in the same cell; the line shown fades out across the slot's roll while the next fades in */
    &[data-effect="fade"] > span {
      display: grid;
    }
    &[data-effect="fade"] > span > * {
      /* 1 for the line shown, 1 for the line after it (the first after the last) */
      --_on: calc(1 - min(1, abs(var(--_text-rotate-i) - var(--_text-rotate-k))));
      --_text-rotate-next: calc(
        1 - min(1, abs(mod(var(--_text-rotate-i) + 1, var(--_text-rotate-n)) - var(--_text-rotate-k)))
      );
      grid-area: 1 / 1;
      opacity: calc(var(--_on) * (1 - var(--_text-rotate-e)) + var(--_text-rotate-next) * var(--_text-rotate-e));
      translate: 0
        calc((var(--_text-rotate-next) * (1 - var(--_text-rotate-e)) - var(--_on) * var(--_text-rotate-e)) * 0.25lh);
    }
  }
  /* the line count and each line's index: the browser counts where it can, else the rules below (up to 40 lines) */
  .text-rotate:has(> span > :nth-child(2):last-child) {
    --_text-rotate-n: 2;
  }
  .text-rotate:has(> span > :nth-child(3):last-child) {
    --_text-rotate-n: 3;
  }
  .text-rotate:has(> span > :nth-child(4):last-child) {
    --_text-rotate-n: 4;
  }
  .text-rotate:has(> span > :nth-child(5):last-child) {
    --_text-rotate-n: 5;
  }
  .text-rotate:has(> span > :nth-child(6):last-child) {
    --_text-rotate-n: 6;
  }
  .text-rotate:has(> span > :nth-child(7):last-child) {
    --_text-rotate-n: 7;
  }
  .text-rotate:has(> span > :nth-child(8):last-child) {
    --_text-rotate-n: 8;
  }
  .text-rotate:has(> span > :nth-child(9):last-child) {
    --_text-rotate-n: 9;
  }
  .text-rotate:has(> span > :nth-child(10):last-child) {
    --_text-rotate-n: 10;
  }
  .text-rotate:has(> span > :nth-child(11):last-child) {
    --_text-rotate-n: 11;
  }
  .text-rotate:has(> span > :nth-child(12):last-child) {
    --_text-rotate-n: 12;
  }
  .text-rotate:has(> span > :nth-child(13):last-child) {
    --_text-rotate-n: 13;
  }
  .text-rotate:has(> span > :nth-child(14):last-child) {
    --_text-rotate-n: 14;
  }
  .text-rotate:has(> span > :nth-child(15):last-child) {
    --_text-rotate-n: 15;
  }
  .text-rotate:has(> span > :nth-child(16):last-child) {
    --_text-rotate-n: 16;
  }
  .text-rotate:has(> span > :nth-child(17):last-child) {
    --_text-rotate-n: 17;
  }
  .text-rotate:has(> span > :nth-child(18):last-child) {
    --_text-rotate-n: 18;
  }
  .text-rotate:has(> span > :nth-child(19):last-child) {
    --_text-rotate-n: 19;
  }
  .text-rotate:has(> span > :nth-child(20):last-child) {
    --_text-rotate-n: 20;
  }
  .text-rotate:has(> span > :nth-child(21):last-child) {
    --_text-rotate-n: 21;
  }
  .text-rotate:has(> span > :nth-child(22):last-child) {
    --_text-rotate-n: 22;
  }
  .text-rotate:has(> span > :nth-child(23):last-child) {
    --_text-rotate-n: 23;
  }
  .text-rotate:has(> span > :nth-child(24):last-child) {
    --_text-rotate-n: 24;
  }
  .text-rotate:has(> span > :nth-child(25):last-child) {
    --_text-rotate-n: 25;
  }
  .text-rotate:has(> span > :nth-child(26):last-child) {
    --_text-rotate-n: 26;
  }
  .text-rotate:has(> span > :nth-child(27):last-child) {
    --_text-rotate-n: 27;
  }
  .text-rotate:has(> span > :nth-child(28):last-child) {
    --_text-rotate-n: 28;
  }
  .text-rotate:has(> span > :nth-child(29):last-child) {
    --_text-rotate-n: 29;
  }
  .text-rotate:has(> span > :nth-child(30):last-child) {
    --_text-rotate-n: 30;
  }
  .text-rotate:has(> span > :nth-child(31):last-child) {
    --_text-rotate-n: 31;
  }
  .text-rotate:has(> span > :nth-child(32):last-child) {
    --_text-rotate-n: 32;
  }
  .text-rotate:has(> span > :nth-child(33):last-child) {
    --_text-rotate-n: 33;
  }
  .text-rotate:has(> span > :nth-child(34):last-child) {
    --_text-rotate-n: 34;
  }
  .text-rotate:has(> span > :nth-child(35):last-child) {
    --_text-rotate-n: 35;
  }
  .text-rotate:has(> span > :nth-child(36):last-child) {
    --_text-rotate-n: 36;
  }
  .text-rotate:has(> span > :nth-child(37):last-child) {
    --_text-rotate-n: 37;
  }
  .text-rotate:has(> span > :nth-child(38):last-child) {
    --_text-rotate-n: 38;
  }
  .text-rotate:has(> span > :nth-child(39):last-child) {
    --_text-rotate-n: 39;
  }
  .text-rotate:has(> span > :nth-child(40):last-child) {
    --_text-rotate-n: 40;
  }
  .text-rotate > span > :nth-child(2) {
    --_text-rotate-k: 1;
  }
  .text-rotate > span > :nth-child(3) {
    --_text-rotate-k: 2;
  }
  .text-rotate > span > :nth-child(4) {
    --_text-rotate-k: 3;
  }
  .text-rotate > span > :nth-child(5) {
    --_text-rotate-k: 4;
  }
  .text-rotate > span > :nth-child(6) {
    --_text-rotate-k: 5;
  }
  .text-rotate > span > :nth-child(7) {
    --_text-rotate-k: 6;
  }
  .text-rotate > span > :nth-child(8) {
    --_text-rotate-k: 7;
  }
  .text-rotate > span > :nth-child(9) {
    --_text-rotate-k: 8;
  }
  .text-rotate > span > :nth-child(10) {
    --_text-rotate-k: 9;
  }
  .text-rotate > span > :nth-child(11) {
    --_text-rotate-k: 10;
  }
  .text-rotate > span > :nth-child(12) {
    --_text-rotate-k: 11;
  }
  .text-rotate > span > :nth-child(13) {
    --_text-rotate-k: 12;
  }
  .text-rotate > span > :nth-child(14) {
    --_text-rotate-k: 13;
  }
  .text-rotate > span > :nth-child(15) {
    --_text-rotate-k: 14;
  }
  .text-rotate > span > :nth-child(16) {
    --_text-rotate-k: 15;
  }
  .text-rotate > span > :nth-child(17) {
    --_text-rotate-k: 16;
  }
  .text-rotate > span > :nth-child(18) {
    --_text-rotate-k: 17;
  }
  .text-rotate > span > :nth-child(19) {
    --_text-rotate-k: 18;
  }
  .text-rotate > span > :nth-child(20) {
    --_text-rotate-k: 19;
  }
  .text-rotate > span > :nth-child(21) {
    --_text-rotate-k: 20;
  }
  .text-rotate > span > :nth-child(22) {
    --_text-rotate-k: 21;
  }
  .text-rotate > span > :nth-child(23) {
    --_text-rotate-k: 22;
  }
  .text-rotate > span > :nth-child(24) {
    --_text-rotate-k: 23;
  }
  .text-rotate > span > :nth-child(25) {
    --_text-rotate-k: 24;
  }
  .text-rotate > span > :nth-child(26) {
    --_text-rotate-k: 25;
  }
  .text-rotate > span > :nth-child(27) {
    --_text-rotate-k: 26;
  }
  .text-rotate > span > :nth-child(28) {
    --_text-rotate-k: 27;
  }
  .text-rotate > span > :nth-child(29) {
    --_text-rotate-k: 28;
  }
  .text-rotate > span > :nth-child(30) {
    --_text-rotate-k: 29;
  }
  .text-rotate > span > :nth-child(31) {
    --_text-rotate-k: 30;
  }
  .text-rotate > span > :nth-child(32) {
    --_text-rotate-k: 31;
  }
  .text-rotate > span > :nth-child(33) {
    --_text-rotate-k: 32;
  }
  .text-rotate > span > :nth-child(34) {
    --_text-rotate-k: 33;
  }
  .text-rotate > span > :nth-child(35) {
    --_text-rotate-k: 34;
  }
  .text-rotate > span > :nth-child(36) {
    --_text-rotate-k: 35;
  }
  .text-rotate > span > :nth-child(37) {
    --_text-rotate-k: 36;
  }
  .text-rotate > span > :nth-child(38) {
    --_text-rotate-k: 37;
  }
  .text-rotate > span > :nth-child(39) {
    --_text-rotate-k: 38;
  }
  .text-rotate > span > :nth-child(40) {
    --_text-rotate-k: 39;
  }
  @supports (z-index: sibling-count()) {
    .text-rotate > span > * {
      --_text-rotate-n: sibling-count();
      --_text-rotate-k: calc(sibling-index() - 1);
    }
  }
  /* the round's progress: 0, 1/n, 2/n ... (steps(n)) - the line shown is that times n */
  @keyframes text-rotate-index {
    from {
      --_text-rotate-t: 0;
    }
    to {
      --_text-rotate-t: 1;
    }
  }
  /* each slot: the line holds for 92%, then the roll eases to the next */
  @keyframes text-rotate-roll {
    0%,
    92% {
      --_text-rotate-e: 0;
      animation-timing-function: cubic-bezier(0.65, 0, 0.35, 1);
    }
    100% {
      --_text-rotate-e: 1;
    }
  }
  /* -- Accessibility ----------------------------------------------------
     Reduced motion: no rolling - the first line stays (both counters rest at 0). All lines stay in the DOM, so
     assistive tech reads the full list. */
  @media (prefers-reduced-motion: reduce) {
    .text-rotate > span > * {
      animation: none;
    }
  }
}

Comments, ideas or improvements? Edit this page's source on GitHub