AI CSS Animations

Trash Can Delete Button

Click delete and the bin's lid swings open, a sheet of paper drops in, the lid slams shut and the can squashes before the label reads 'Deleted'. The SVG parts move with fill-box transforms.

Also known as: delete button animation css, trash button animation, delete file button.

HTML

<label class="btn-trash-delete">
  <input class="btn-trash-delete-input" type="checkbox" aria-label="Delete">
  <span class="btn-trash-delete-face" aria-hidden="true">
    <svg class="btn-trash-delete-can" viewBox="0 0 24 24">
      <rect class="btn-trash-delete-paper" x="9.2" y="0.5" width="5.6" height="6.5" rx="0.8"/>
      <path class="btn-trash-delete-body" d="M5.5 8.5l1.1 11.2a1.6 1.6 0 0 0 1.6 1.3h7.6a1.6 1.6 0 0 0 1.6-1.3L18.5 8.5z"/>
      <path d="M10 11.5v6M14 11.5v6"/>
      <g class="btn-trash-delete-lid"><path d="M3.5 6.5h17"/><path d="M9.2 6.5V4.6a1.1 1.1 0 0 1 1.1-1.1h3.4a1.1 1.1 0 0 1 1.1 1.1v1.9"/></g>
    </svg>
    <span class="btn-trash-delete-text"><span>Delete</span><span>Deleted</span></span>
  </span>
</label>

CSS

.btn-trash-delete {
  --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-trash-delete-input {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.btn-trash-delete-face {
  display: inline-flex;
  align-items: center;
  gap: 0.55em;
  padding: 0.75em 1.5em 0.75em 1.2em;
  border-radius: 0.7em;
  background: var(--c);
  color: #fff;
  box-shadow: 0 0.4em 0.9em -0.4em var(--c);
  transition: transform 0.2s ease;
}
.btn-trash-delete-can {
  width: 1.35em;
  height: 1.35em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  overflow: visible;
  transform-origin: 50% 100%;
}
.btn-trash-delete-can * { transform-box: fill-box; }
.btn-trash-delete-body { fill: var(--c); }
.btn-trash-delete-paper {
  fill: #fff;
  stroke: none;
  opacity: 0;
}
.btn-trash-delete-lid { transform-origin: 100% 100%; }
.btn-trash-delete-text {
  display: grid;
  overflow: hidden;
}
.btn-trash-delete-text span { grid-area: 1 / 1; }
.btn-trash-delete-text span:last-child {
  opacity: 0;
  transform: translateY(100%);
}
.btn-trash-delete:active .btn-trash-delete-face { transform: scale(0.95); }
.btn-trash-delete-input:focus-visible ~ .btn-trash-delete-face {
  outline: 2px solid var(--c);
  outline-offset: 0.2em;
}
.btn-trash-delete-input:checked ~ .btn-trash-delete-face .btn-trash-delete-lid {
  animation: btn-trash-delete-lid 1.2s cubic-bezier(0.3, 0.7, 0.4, 1) forwards;
}
.btn-trash-delete-input:checked ~ .btn-trash-delete-face .btn-trash-delete-paper {
  animation: btn-trash-delete-paper 1.2s forwards;
}
.btn-trash-delete-input:checked ~ .btn-trash-delete-face .btn-trash-delete-can {
  animation: btn-trash-delete-squash 1.2s forwards;
}
.btn-trash-delete-input:checked ~ .btn-trash-delete-face .btn-trash-delete-text span:first-child {
  animation: btn-trash-delete-out 0.3s ease-in 0.85s forwards;
}
.btn-trash-delete-input:checked ~ .btn-trash-delete-face .btn-trash-delete-text span:last-child {
  animation: btn-trash-delete-in 0.35s cubic-bezier(0.3, 0.7, 0.4, 1.3) 0.9s forwards;
}
@keyframes btn-trash-delete-lid {
  0% { transform: rotate(0deg); }
  20%, 52% { transform: rotate(32deg); }
  64% { transform: rotate(-4deg); }
  72%, 100% { transform: rotate(0deg); }
}
@keyframes btn-trash-delete-paper {
  0%, 12% { opacity: 0; transform: translateY(-70%); }
  22% { opacity: 1; transform: translateY(-70%); animation-timing-function: cubic-bezier(0.55, 0, 1, 0.45); }
  48% { opacity: 1; transform: translateY(170%); }
  49%, 100% { opacity: 0; transform: translateY(170%); }
}
@keyframes btn-trash-delete-squash {
  0%, 62% { transform: none; }
  70% { transform: scale(1.12, 0.86); }
  80% { transform: scale(0.95, 1.06); }
  90%, 100% { transform: none; }
}
@keyframes btn-trash-delete-out {
  to { opacity: 0; transform: translateY(-100%); }
}
@keyframes btn-trash-delete-in {
  to { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .btn-trash-delete, .btn-trash-delete *, .btn-trash-delete::before, .btn-trash-delete::after, .btn-trash-delete *::before, .btn-trash-delete *::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