JSFiddle - React, Tailwind, and code Playground
by amatiasq
HTML
<div class="hamburger">
<input type="checkbox" />
<span></span>
<span></span>
<span></span>
</div>
CSS
body {
background-color: black
}
.hamburger {
display: inline-block;
position: absolute;
top: 24px;
right: 40px;
z-index: 1;
-webkit-user-select: none;
user-select: none;
}
.hamburger input {
display: block;
width: 40px;
height: 32px;
position: absolute;
top: -7px;
left: -5px;
cursor: pointer;
opacity: 0; /* hide this */
z-index: 2; /* and place it over the hamburger */
-webkit-touch-callout: none;
}
.hamburger span {
display: block;
width: 33px;
height: 4px;
margin-bottom: 5px;
position: relative;
background: #cdcdcd;
border-radius: 3px;
z-index: 1;
transform-origin: center right;
transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1),
background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1), opacity 0.55s ease;
}
/*
* Transform all the slices of hamburger
* into a crossmark.
*/
.hamburger input:checked ~ span:nth-of-type(2) {
opacity: 0;
transform: rotate(0deg) scale(0.2, 0.2);
}
.hamburger input:checked ~ span:first-of-type {
transform: rotate(-45deg) translate(0, -4px);
}
.hamburger input:checked ~ span:last-of-type {
transform: rotate(45deg) translate(0, 4px);
}
JavaScript
document.querySelector('input')
.addEventListener('change', event =>
console.log(event.target.checked)
)