A like switch: the outlined heart rides across, then fills with the accent in a squash and pop while eight dots burst out around the knob. The burst is one element with box-shadow dots.
Also known as: like toggle switch, heart switch css, favorite toggle.
<label class="tgl-heart-toggle">
<input type="checkbox" role="switch" aria-label="Like">
<span class="tgl-heart-toggle-track" aria-hidden="true">
<span class="tgl-heart-toggle-knob">
<span class="tgl-heart-toggle-burst"></span>
<svg viewBox="0 0 24 24"><path class="tgl-heart-toggle-heart" d="M12 20.5s-8-4.9-8-10.6A4.4 4.4 0 0 1 12 7.4a4.4 4.4 0 0 1 8 2.5c0 5.7-8 10.6-8 10.6z"/></svg>
</span>
</span>
</label>.tgl-heart-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;
}
.tgl-heart-toggle input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-heart-toggle-track {
position: relative;
display: block;
width: 5.6em;
height: 2.6em;
border-radius: 1.3em;
background: color-mix(in srgb, currentColor 14%, transparent);
transition: background-color 0.35s ease;
}
.tgl-heart-toggle-knob {
position: absolute;
top: 0.3em;
left: 0.3em;
display: grid;
place-items: center;
width: 2em;
height: 2em;
border-radius: 50%;
background: #fff;
box-shadow: 0 0.1em 0.25em rgba(0, 0, 0, 0.25);
transition: transform 0.45s cubic-bezier(0.5, 1.3, 0.4, 1);
}
.tgl-heart-toggle-knob svg {
display: block;
width: 1.2em;
height: 1.2em;
overflow: visible;
}
.tgl-heart-toggle-heart {
fill: transparent;
stroke: #9ca3af;
stroke-width: 2;
stroke-linejoin: round;
transform-box: fill-box;
transform-origin: center;
transition: fill 0.2s ease, stroke 0.2s ease;
}
.tgl-heart-toggle-burst {
position: absolute;
top: calc(50% - 0.11em);
left: calc(50% - 0.11em);
width: 0.22em;
height: 0.22em;
border-radius: 50%;
--dot: color-mix(in srgb, var(--c) 45%, #fbbf24);
box-shadow:
0 -1.25em 0 var(--c), 0.88em -0.88em 0 var(--dot),
1.25em 0 0 var(--c), 0.88em 0.88em 0 var(--dot),
0 1.25em 0 var(--c), -0.88em 0.88em 0 var(--dot),
-1.25em 0 0 var(--c), -0.88em -0.88em 0 var(--dot);
opacity: 0;
}
.tgl-heart-toggle input:checked + .tgl-heart-toggle-track { background: color-mix(in srgb, var(--c) 35%, transparent); }
.tgl-heart-toggle input:checked + .tgl-heart-toggle-track .tgl-heart-toggle-knob { transform: translateX(3em); }
.tgl-heart-toggle input:checked + .tgl-heart-toggle-track .tgl-heart-toggle-heart {
fill: var(--c);
stroke: var(--c);
transition-delay: 0.3s;
animation: tgl-heart-toggle-pop 0.45s cubic-bezier(0.3, 0.7, 0.4, 1) 0.3s;
}
.tgl-heart-toggle input:checked + .tgl-heart-toggle-track .tgl-heart-toggle-burst {
animation: tgl-heart-toggle-burst 0.6s ease-out 0.38s both;
}
@keyframes tgl-heart-toggle-pop {
0%, 100% { transform: scale(1); }
30% { transform: scale(0.6); }
65% { transform: scale(1.3); }
}
@keyframes tgl-heart-toggle-burst {
0% { transform: scale(0.35); opacity: 0; }
15% { opacity: 1; }
100% { transform: scale(1.25); opacity: 0; }
}
.tgl-heart-toggle input:focus-visible + .tgl-heart-toggle-track {
outline: 2px solid var(--c);
outline-offset: 0.2em;
}
@media (prefers-reduced-motion: reduce) {
.tgl-heart-toggle, .tgl-heart-toggle *, .tgl-heart-toggle::before, .tgl-heart-toggle::after, .tgl-heart-toggle *::before, .tgl-heart-toggle *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--tgl-color on the element or a parent.
Currently previewing #3b82f6.--tgl-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 24px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.