AI CSS Animations

Open Closed Sign Toggle

A shop sign hanging from a nail: switch it and the board flips over in 3D from CLOSED to OPEN, then swings on its string and settles. Two faces with backface-visibility: hidden.

Also known as: open closed toggle css, flip sign switch, shop sign toggle.

HTML

<label class="tgl-open-closed-sign-toggle">
  <input type="checkbox" role="switch" aria-label="Open">
  <span class="tgl-open-closed-sign-toggle-rig" aria-hidden="true">
    <span class="tgl-open-closed-sign-toggle-string"></span>
    <span class="tgl-open-closed-sign-toggle-board">
      <span class="tgl-open-closed-sign-toggle-closed">CLOSED</span>
      <span class="tgl-open-closed-sign-toggle-open">OPEN</span>
    </span>
  </span>
</label>

CSS

.tgl-open-closed-sign-toggle {
  --c: var(--tgl-color, #3b82f6);
  font-size: var(--tgl-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  font-family: system-ui, sans-serif;
}
.tgl-open-closed-sign-toggle input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-open-closed-sign-toggle-rig {
  position: relative;
  display: block;
  width: 6.4em;
  height: 4.1em;
  transform-origin: 50% 0.26em;
}
/* the nail */
.tgl-open-closed-sign-toggle-rig::before {
  content: "";
  position: absolute;
  left: calc(50% - 0.18em);
  top: 0.08em;
  z-index: 2;
  width: 0.36em;
  height: 0.36em;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #e5e7eb, #6b7280);
}
/* two diagonal strings, drawn with gradients */
.tgl-open-closed-sign-toggle-string {
  position: absolute;
  left: calc(50% - 1.9em);
  top: 0.26em;
  width: 3.8em;
  height: 1.15em;
  --line: color-mix(in srgb, currentColor 60%, transparent);
  background:
    linear-gradient(to bottom right, transparent calc(50% - 0.05em), var(--line) calc(50% - 0.04em) calc(50% + 0.04em), transparent calc(50% + 0.05em)) left / 50% 100% no-repeat,
    linear-gradient(to top right, transparent calc(50% - 0.05em), var(--line) calc(50% - 0.04em) calc(50% + 0.04em), transparent calc(50% + 0.05em)) right / 50% 100% no-repeat;
}
.tgl-open-closed-sign-toggle-board {
  position: absolute;
  left: calc(50% - 2.9em);
  top: 1.35em;
  width: 5.8em;
  height: 2.5em;
  transform-style: preserve-3d;
  transform: perspective(25em) rotateY(0deg);
  transition: transform 0.8s cubic-bezier(0.4, 1.4, 0.5, 1);
}
.tgl-open-closed-sign-toggle-closed,
.tgl-open-closed-sign-toggle-open {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 0.45em;
  font-size: 0.9em;
  font-weight: 800;
  letter-spacing: 0.12em;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
.tgl-open-closed-sign-toggle-closed {
  color: #dc2626;
  background: #fff;
  box-shadow: inset 0 0 0 0.18em #fff, inset 0 0 0 0.29em #dc2626, 0 0.2em 0.4em rgba(0, 0, 0, 0.25);
}
.tgl-open-closed-sign-toggle-open {
  color: #fff;
  background: var(--c);
  box-shadow: inset 0 0 0 0.18em var(--c), inset 0 0 0 0.29em #fff, 0 0.2em 0.4em rgba(0, 0, 0, 0.25);
  transform: rotateY(180deg);
}
.tgl-open-closed-sign-toggle input:checked + .tgl-open-closed-sign-toggle-rig {
  animation: tgl-open-closed-sign-toggle-swing 1.4s ease-in-out 0.2s;
}
.tgl-open-closed-sign-toggle input:checked + .tgl-open-closed-sign-toggle-rig .tgl-open-closed-sign-toggle-board {
  transform: perspective(25em) rotateY(180deg);
}
@keyframes tgl-open-closed-sign-toggle-swing {
  0%, 100% { transform: rotate(0deg); }
  15% { transform: rotate(7deg); }
  35% { transform: rotate(-5deg); }
  55% { transform: rotate(3deg); }
  75% { transform: rotate(-1.5deg); }
}
.tgl-open-closed-sign-toggle input:focus-visible + .tgl-open-closed-sign-toggle-rig .tgl-open-closed-sign-toggle-board {
  outline: 2px solid var(--c);
  outline-offset: 0.25em;
  border-radius: 0.45em;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-open-closed-sign-toggle, .tgl-open-closed-sign-toggle *, .tgl-open-closed-sign-toggle::before, .tgl-open-closed-sign-toggle::after, .tgl-open-closed-sign-toggle *::before, .tgl-open-closed-sign-toggle *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

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