A padlock drawn with two spans: unchecked, the shackle swings open on its left leg; checked, it snaps shut with a small clunk and the body fills with the accent. Good for locking layers or files.
Also known as: lock unlock checkbox css, padlock toggle css, lock icon animation css.
<label class="chk-padlock-lock">
<input type="checkbox">
<span class="chk-padlock-lock-icon" aria-hidden="true">
<i class="chk-padlock-lock-shackle"></i>
<i class="chk-padlock-lock-body"><b class="chk-padlock-lock-hole"></b></i>
</span>
<span class="chk-padlock-lock-text">Lock layer</span>
</label>.chk-padlock-lock {
--c: var(--chk-color, #3b82f6);
font-size: var(--chk-size, 24px);
position: relative;
display: inline-flex;
align-items: center;
gap: 0.6em;
line-height: 1.3;
cursor: pointer;
-webkit-user-select: none;
user-select: none;
-webkit-tap-highlight-color: transparent;
gap: 0.65em;}
.chk-padlock-lock input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.chk-padlock-lock-icon {
position: relative;
flex: none;
width: 1.3em;
height: 1.7em;
border-radius: 0.25em;
color: color-mix(in srgb, currentColor 70%, transparent);
transition: color 0.25s ease;
}
.chk-padlock-lock-shackle {
position: absolute;
left: 0.22em;
top: 0.1em;
width: 0.86em;
height: 0.66em;
border: 0.14em solid currentColor;
border-bottom: 0;
border-radius: 0.43em 0.43em 0 0;
transform-origin: 0.07em 100%;
transform: rotate(-28deg) translateY(0);
transition: transform 0.3s ease;
}
.chk-padlock-lock-body {
position: absolute;
left: 0;
bottom: 0;
display: grid;
place-items: center;
width: 1.3em;
height: 0.96em;
border: 0.14em solid currentColor;
border-radius: 0.25em;
transition: background-color 0.25s ease, border-color 0.25s ease;
}
.chk-padlock-lock-hole {
width: 0.15em;
height: 0.3em;
border-radius: 0.1em;
background: currentColor;
transition: background-color 0.25s ease;
}
.chk-padlock-lock-text {
transition: color 0.25s ease;
}
.chk-padlock-lock:hover .chk-padlock-lock-icon {
color: var(--c);
}
.chk-padlock-lock input:checked + .chk-padlock-lock-icon {
color: var(--c);
}
.chk-padlock-lock input:checked + .chk-padlock-lock-icon .chk-padlock-lock-shackle {
transform: rotate(0deg) translateY(0);
animation: chk-padlock-lock-snap 0.5s ease-out;
}
.chk-padlock-lock input:checked + .chk-padlock-lock-icon .chk-padlock-lock-body {
background: var(--c);
animation: chk-padlock-lock-clunk 0.4s ease-out 0.25s;
}
.chk-padlock-lock input:checked + .chk-padlock-lock-icon .chk-padlock-lock-hole {
background: #fff;
}
.chk-padlock-lock input:checked ~ .chk-padlock-lock-text {
color: var(--c);
}
.chk-padlock-lock input:focus-visible + .chk-padlock-lock-icon {
outline: 0.12em solid var(--c);
outline-offset: 0.18em;
}
@keyframes chk-padlock-lock-snap {
0% { transform: rotate(-28deg) translateY(0); }
45% { transform: rotate(0deg) translateY(-0.14em); }
70% { transform: rotate(0deg) translateY(0.04em); }
100% { transform: rotate(0deg) translateY(0); }
}
@keyframes chk-padlock-lock-clunk {
0%, 100% { transform: rotate(0deg); }
30% { transform: rotate(-7deg); }
65% { transform: rotate(4deg); }
}
@media (prefers-reduced-motion: reduce) {
.chk-padlock-lock, .chk-padlock-lock *, .chk-padlock-lock::before, .chk-padlock-lock::after, .chk-padlock-lock *::before, .chk-padlock-lock *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--chk-color on the element or a parent.
Currently previewing #3b82f6.--chk-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.