AI CSS Animations

Text Align Toggle Group

An editor toolbar of icon radios for left, center and right alignment, next to a paragraph preview whose lines glide into the new alignment one after another.

Also known as: icon radio buttons css, toolbar toggle buttons css, button group radio css.

HTML

<div class="rad-text-align-toggle-group" role="radiogroup" aria-label="Text alignment">
  <div class="rad-text-align-toggle-group-bar">
    <label><input type="radio" name="rad-text-align-toggle-group" value="left" aria-label="Align left" checked><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16M4 10h10M4 14h16M4 18h10"/></svg></label>
    <label><input type="radio" name="rad-text-align-toggle-group" value="center" aria-label="Align center"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16M7 10h10M4 14h16M7 18h10"/></svg></label>
    <label><input type="radio" name="rad-text-align-toggle-group" value="right" aria-label="Align right"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M4 6h16M10 10h10M4 14h16M10 18h10"/></svg></label>
  </div>
  <div class="rad-text-align-toggle-group-text" aria-hidden="true"><i></i><i></i><i></i><i></i></div>
</div>

CSS

.rad-text-align-toggle-group {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  --k: 0;
  display: inline-flex;
  align-items: center;
  gap: 1em;
}
.rad-text-align-toggle-group:has(label:nth-of-type(2) :checked) { --k: 0.5; }
.rad-text-align-toggle-group:has(label:nth-of-type(3) :checked) { --k: 1; }
.rad-text-align-toggle-group-bar {
  display: flex;
  gap: 0.2em;
  padding: 0.25em;
  border: 1px solid color-mix(in srgb, currentColor 15%, transparent);
  border-radius: 0.65em;
}
.rad-text-align-toggle-group label {
  position: relative;
  display: grid;
  place-items: center;
  width: 2.1em;
  height: 2.1em;
  border-radius: 0.45em;
  color: color-mix(in srgb, currentColor 55%, transparent);
  cursor: pointer;
  transition: background-color 0.2s ease, color 0.2s ease;
}
.rad-text-align-toggle-group label:hover {
  background: color-mix(in srgb, currentColor 7%, transparent);
}
.rad-text-align-toggle-group label:has(:checked) {
  background: color-mix(in srgb, var(--c) 15%, transparent);
  color: var(--c);
}
.rad-text-align-toggle-group input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-text-align-toggle-group svg {
  width: 1.25em;
  height: 1.25em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}
.rad-text-align-toggle-group input:checked + svg {
  animation: rad-text-align-toggle-group-press 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.rad-text-align-toggle-group input:focus-visible + svg {
  outline: 0.1em solid var(--c);
  outline-offset: 0.35em;
  border-radius: 0.2em;
}
.rad-text-align-toggle-group-text {
  display: grid;
  gap: 0.42em;
  box-sizing: border-box;
  width: 8.6em;
  padding: 0.8em;
  border: 1px solid color-mix(in srgb, currentColor 12%, transparent);
  border-radius: 0.6em;
}
.rad-text-align-toggle-group-text i {
  display: block;
  width: var(--w);
  height: 0.36em;
  border-radius: 0.2em;
  background: color-mix(in srgb, currentColor 22%, transparent);
  transform: translateX(calc((7em - var(--w)) * var(--k)));
  transition: transform 0.5s cubic-bezier(0.34, 1.25, 0.64, 1);
}
.rad-text-align-toggle-group-text i:nth-child(1) { --w: 4.2em; height: 0.5em; background: var(--c); }
.rad-text-align-toggle-group-text i:nth-child(2) { --w: 7em; transition-delay: 0.05s; }
.rad-text-align-toggle-group-text i:nth-child(3) { --w: 5.2em; transition-delay: 0.1s; }
.rad-text-align-toggle-group-text i:nth-child(4) { --w: 6.1em; transition-delay: 0.15s; }
@keyframes rad-text-align-toggle-group-press {
  0% { transform: scale(0.8); }
  100% { transform: scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .rad-text-align-toggle-group, .rad-text-align-toggle-group *, .rad-text-align-toggle-group::before, .rad-text-align-toggle-group::after, .rad-text-align-toggle-group *::before, .rad-text-align-toggle-group *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --rad-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --rad-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 18px.
  • 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 radio buttons