AI CSS Animations

Bracket Rotating Text

Two brackets squeeze in on the word, pinching it to nothing, then spring back open around the next one. Each bracket rides a half-width layer, so translateX(100%) lands it in the middle.

Also known as: brackets word swap, code bracket text animation, squeeze text rotator.

Let's talk build ship

HTML

<div class="txt-bracket-rotating-text">Let's
  <span class="txt-bracket-rotating-text-slot">
    <span style="--i: 0">talk</span>
    <span style="--i: 1">build</span>
    <span style="--i: 2">ship</span>
  <i class="txt-bracket-rotating-text-open" aria-hidden="true"></i><i class="txt-bracket-rotating-text-close" aria-hidden="true"></i>
  </span>
</div>

CSS

.txt-bracket-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.4em;
  white-space: nowrap;}
.txt-bracket-rotating-text-slot {
  position: relative;
  display: inline-grid;
  padding: 0 0.45em;
  color: var(--c);
}
.txt-bracket-rotating-text-slot span {
  grid-area: 1 / 1;
  justify-self: center;
  /* 3 words x 2s = 6s */
  animation: txt-bracket-rotating-text-word 6s infinite;
  animation-delay: calc(var(--i) * 2s - 6s);
}
.txt-bracket-rotating-text-slot span:not(:first-child) {
  opacity: 0;
}
/* each bracket sits on a layer half the slot wide */
.txt-bracket-rotating-text-open,
.txt-bracket-rotating-text-close {
  position: absolute;
  top: 0.08em;
  bottom: 0.08em;
  width: 50%;
  animation: 2s infinite;
}
.txt-bracket-rotating-text-open {
  left: 0;
  animation-name: txt-bracket-rotating-text-open;
}
.txt-bracket-rotating-text-close {
  right: 0;
  animation-name: txt-bracket-rotating-text-close;
}
.txt-bracket-rotating-text-open::before,
.txt-bracket-rotating-text-close::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 0.22em;
  border: 0.1em solid currentColor;
  border-radius: 0.04em;
}
.txt-bracket-rotating-text-open::before {
  left: 0;
  border-right: 0;
}
.txt-bracket-rotating-text-close::before {
  right: 0;
  border-left: 0;
}
@keyframes txt-bracket-rotating-text-open {
  0%, 70% { transform: translateX(0); animation-timing-function: cubic-bezier(0.6, 0, 0.9, 0.5); }
  85%     { transform: translateX(calc(100% - 0.36em)); animation-timing-function: cubic-bezier(0.3, 1.6, 0.5, 1); }
  100%    { transform: translateX(0); }
}
@keyframes txt-bracket-rotating-text-close {
  0%, 70% { transform: translateX(0); animation-timing-function: cubic-bezier(0.6, 0, 0.9, 0.5); }
  85%     { transform: translateX(calc(-100% + 0.36em)); animation-timing-function: cubic-bezier(0.3, 1.6, 0.5, 1); }
  100%    { transform: translateX(0); }
}
@keyframes txt-bracket-rotating-text-word {
  0%, 23.33% { opacity: 1; transform: scaleX(1); animation-timing-function: cubic-bezier(0.6, 0, 0.9, 0.5); }
  28.33%     { opacity: 0; transform: scaleX(0.1); }
  95%        { opacity: 0; transform: scaleX(0.1); animation-timing-function: cubic-bezier(0.3, 1.6, 0.5, 1); }
  100%       { opacity: 1; transform: scaleX(1); }
}

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