AI CSS Animations

Pull Cord Lamp Switch

A hanging lamp with a pull cord: pressing tugs the cord down, releasing springs it back, and the bulb flickers on under a cone of light. The press is :active, the light is :checked.

Also known as: lamp pull switch css, pull chain light toggle, hanging lamp toggle.

HTML

<label class="tgl-pull-cord-lamp">
  <input type="checkbox" role="switch" aria-label="Lamp">
  <span class="tgl-pull-cord-lamp-lamp" aria-hidden="true">
    <span class="tgl-pull-cord-lamp-beam"></span>
    <span class="tgl-pull-cord-lamp-cord"></span>
    <span class="tgl-pull-cord-lamp-bulb"></span>
    <span class="tgl-pull-cord-lamp-shade"></span>
  </span>
</label>

CSS

.tgl-pull-cord-lamp {
  --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;
}
.tgl-pull-cord-lamp input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.tgl-pull-cord-lamp-lamp {
  position: relative;
  display: block;
  width: 7em;
  height: 7.4em;
}
/* ceiling cable and cap */
.tgl-pull-cord-lamp-lamp::before {
  content: "";
  position: absolute;
  left: calc(50% - 0.05em);
  top: 0.1em;
  width: 0.1em;
  height: 1.1em;
  background: color-mix(in srgb, currentColor 55%, transparent);
}
.tgl-pull-cord-lamp-lamp::after {
  content: "";
  position: absolute;
  left: calc(50% - 0.4em);
  top: 1em;
  width: 0.8em;
  height: 0.4em;
  border-radius: 0.2em 0.2em 0 0;
  background: color-mix(in srgb, var(--c) 60%, #000);
}
.tgl-pull-cord-lamp-shade {
  position: absolute;
  left: 1.4em;
  top: 1.3em;
  width: 4.2em;
  height: 1.8em;
  clip-path: polygon(30% 0, 70% 0, 100% 100%, 0 100%);
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--c) 65%, #000),
    var(--c) 35%,
    color-mix(in srgb, var(--c) 55%, #fff) 50%,
    var(--c) 65%,
    color-mix(in srgb, var(--c) 65%, #000));
}
.tgl-pull-cord-lamp-bulb {
  position: absolute;
  left: calc(50% - 0.55em);
  top: 2.5em;
  width: 1.1em;
  height: 1.1em;
  border-radius: 50%;
  background: #d6d3d1;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
}
.tgl-pull-cord-lamp-beam {
  position: absolute;
  left: 0.2em;
  right: 0.2em;
  top: 3em;
  height: 4.3em;
  clip-path: polygon(20% 0, 80% 0, 100% 100%, 0 100%);
  background: linear-gradient(rgba(253, 224, 71, 0.6), rgba(253, 224, 71, 0));
  opacity: 0;
  transition: opacity 0.25s ease;
}
/* the cord starts behind the shade, so pulling it down never shows a gap */
.tgl-pull-cord-lamp-cord {
  position: absolute;
  left: 4.85em;
  top: 2.3em;
  width: 0.08em;
  height: 2.4em;
  background: color-mix(in srgb, currentColor 60%, transparent);
  transition: transform 0.55s cubic-bezier(0.3, 1.9, 0.5, 1);
}
.tgl-pull-cord-lamp-cord::after {
  content: "";
  position: absolute;
  left: -0.2em;
  bottom: -0.55em;
  width: 0.48em;
  height: 0.62em;
  border-radius: 45% 45% 50% 50%;
  background: color-mix(in srgb, var(--c) 70%, #000);
}
.tgl-pull-cord-lamp:hover .tgl-pull-cord-lamp-cord { transform: translateY(0.12em); }
.tgl-pull-cord-lamp:active .tgl-pull-cord-lamp-cord {
  transform: translateY(0.6em);
  transition-duration: 0.12s;
  transition-timing-function: ease-out;
}
.tgl-pull-cord-lamp input:checked + .tgl-pull-cord-lamp-lamp .tgl-pull-cord-lamp-bulb {
  background: #fef9c3;
  box-shadow: 0 0 0.6em 0.2em rgba(253, 224, 71, 0.75);
}
.tgl-pull-cord-lamp input:checked + .tgl-pull-cord-lamp-lamp .tgl-pull-cord-lamp-beam {
  opacity: 1;
  animation: tgl-pull-cord-lamp-flicker 0.6s linear;
}
@keyframes tgl-pull-cord-lamp-flicker {
  0% { opacity: 0; }
  10% { opacity: 0.8; }
  20% { opacity: 0.15; }
  35% { opacity: 1; }
  45% { opacity: 0.5; }
  60%, 100% { opacity: 1; }
}
.tgl-pull-cord-lamp input:focus-visible + .tgl-pull-cord-lamp-lamp {
  outline: 2px solid var(--c);
  outline-offset: 0.1em;
  border-radius: 0.6em;
}

@media (prefers-reduced-motion: reduce) {
  .tgl-pull-cord-lamp, .tgl-pull-cord-lamp *, .tgl-pull-cord-lamp::before, .tgl-pull-cord-lamp::after, .tgl-pull-cord-lamp *::before, .tgl-pull-cord-lamp *::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