Three bars that slide together and then cross into an X, while the middle bar shrinks away. Separate translate and rotate transitions run in sequence, reversed on the way back.
Also known as: hamburger menu animation css, menu icon to close icon, burger icon hover.
<button class="hfx-hamburger-to-x" aria-label="Open menu"> <span class="hfx-hamburger-to-x-bar"></span> <span class="hfx-hamburger-to-x-bar"></span> <span class="hfx-hamburger-to-x-bar"></span> </button>
.hfx-hamburger-to-x {
--c: var(--hfx-color, #3b82f6);
font-size: var(--hfx-size, 16px);
display: grid;
place-content: center;
gap: 0.3em;
width: 3.5em;
height: 3.5em;
padding: 0;
border: 0;
border-radius: 0.8em;
background: color-mix(in srgb, var(--c) 12%, transparent);
cursor: pointer;
transition: background-color 0.25s ease;
}
.hfx-hamburger-to-x-bar {
display: block;
width: 1.6em;
height: 0.17em;
border-radius: 1em;
background: var(--c);
/* leaving: unrotate first, then slide apart */
transition: rotate 0.2s ease, translate 0.2s ease 0.2s, scale 0.15s ease 0.2s;
}
.hfx-hamburger-to-x:is(:hover, :focus-visible) { background: color-mix(in srgb, var(--c) 20%, transparent); }
.hfx-hamburger-to-x:is(:hover, :focus-visible) .hfx-hamburger-to-x-bar {
/* entering: slide together first, then rotate with a little overshoot */
transition: translate 0.2s ease, rotate 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) 0.2s, scale 0.15s ease;
}
.hfx-hamburger-to-x:is(:hover, :focus-visible) .hfx-hamburger-to-x-bar:nth-child(1) {
translate: 0 0.47em;
rotate: 45deg;
}
.hfx-hamburger-to-x:is(:hover, :focus-visible) .hfx-hamburger-to-x-bar:nth-child(2) { scale: 0 1; }
.hfx-hamburger-to-x:is(:hover, :focus-visible) .hfx-hamburger-to-x-bar:nth-child(3) {
translate: 0 -0.47em;
rotate: -45deg;
}
@media (prefers-reduced-motion: reduce) {
.hfx-hamburger-to-x, .hfx-hamburger-to-x *, .hfx-hamburger-to-x::before, .hfx-hamburger-to-x::after, .hfx-hamburger-to-x *::before, .hfx-hamburger-to-x *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.