AI CSS Animations

Chevron Expand Button

A Show more pill whose chevron flips round with a spring when pressed while the label rolls to Show less. On hover the chevron chip fills in. A checkbox drives it, ready to open a details panel.

Also known as: chevron button, show more button css, expand collapse button chevron, chevron rotate animation css.

HTML

<label class="btn-chevron-expand">
  <input class="btn-chevron-expand-input" type="checkbox" aria-label="Show 8 more replies">
  <span class="btn-chevron-expand-face" aria-hidden="true">
    <span class="btn-chevron-expand-text"><span>Show 8 more</span><span>Show less</span></span>
    <span class="btn-chevron-expand-chip"><svg viewBox="0 0 16 16"><path d="M4 6.5l4 4 4-4" /></svg></span>
  </span>
</label>

CSS

.btn-chevron-expand {
  --c: var(--btn-color, #3b82f6);
  position: relative;
  display: inline-grid;
  place-items: center;
  font: 600 var(--btn-size, 18px)/1.1 system-ui, sans-serif;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.btn-chevron-expand-input {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.btn-chevron-expand-face {
  display: inline-flex;
  align-items: center;
  gap: 0.7em;
  padding: 0.4em 0.4em 0.4em 1.15em;
  border-radius: 2em;
  background: color-mix(in srgb, var(--c) 10%, transparent);
  color: var(--c);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 18%, transparent);
  transition: background-color 0.25s ease, transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.btn-chevron-expand-text {
  display: grid;
  height: 1.1em;
  overflow: hidden;
  font-size: 0.9em;
}
.btn-chevron-expand-text span {
  grid-area: 1 / 1;
  transition: transform 0.45s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.25s ease;
}
.btn-chevron-expand-text span:last-child {
  transform: translateY(110%);
  opacity: 0;
}
.btn-chevron-expand-chip {
  display: grid;
  place-items: center;
  width: 1.8em;
  height: 1.8em;
  border-radius: 50%;
  background: color-mix(in srgb, var(--c) 14%, transparent);
  transition: background-color 0.25s ease, color 0.25s ease;
}
.btn-chevron-expand-chip svg {
  width: 1em;
  height: 1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: transform 0.55s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.btn-chevron-expand:hover .btn-chevron-expand-face { background-color: color-mix(in srgb, var(--c) 14%, transparent); }
.btn-chevron-expand:hover .btn-chevron-expand-chip { background-color: var(--c); color: #fff; }
.btn-chevron-expand:hover .btn-chevron-expand-chip svg { transform: translateY(0.08em); }
.btn-chevron-expand:active .btn-chevron-expand-face { transform: scale(0.96); }
.btn-chevron-expand-input:focus-visible ~ .btn-chevron-expand-face {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}
.btn-chevron-expand-input:checked ~ .btn-chevron-expand-face .btn-chevron-expand-chip svg { transform: rotate(180deg); }
.btn-chevron-expand-input:checked ~ .btn-chevron-expand-face .btn-chevron-expand-text span:first-child { transform: translateY(-110%); opacity: 0; }
.btn-chevron-expand-input:checked ~ .btn-chevron-expand-face .btn-chevron-expand-text span:last-child { transform: none; opacity: 1; }
@media (prefers-reduced-motion: reduce) {
  .btn-chevron-expand, .btn-chevron-expand *, .btn-chevron-expand *::before, .btn-chevron-expand *::after { animation: none; transition-duration: 0.01s; }
}

@media (prefers-reduced-motion: reduce) {
  .btn-chevron-expand, .btn-chevron-expand *, .btn-chevron-expand::before, .btn-chevron-expand::after, .btn-chevron-expand *::before, .btn-chevron-expand *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --btn-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --btn-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 buttons