<div class="demo">
<div class="menu-icon">
<input class="menu-icon__cheeckbox" type="checkbox" />
<div>
<span></span>
<span></span>
</div>
</div>
</div>
.demo {
display: flex;
align-items: center;
justify-content: center;
width: 100vw;
height: 100vh;
background-image: linear-gradient(
to top,
#dad4ec 0%,
#dad4ec 1%,
#f3e7e9 100%
);
// for demo
.menu-icon {
transform: scale(1.5);
}
}
:root {
--bar-bg: #212529;
}
.menu-icon {
position: relative;
width: 50px;
height: 50px;
cursor: pointer;
.menu-icon__cheeckbox {
display: block;
width: 100%;
height: 100%;
position: relative;
cursor: pointer;
z-index: 2;
touch-callout: none;
position: absolute;
opacity: 0;
}
div {
margin: auto;
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
width: 22px;
height: 12px;
}
span {
position: absolute;
display: block;
width: 100%;
height: 2px;
background-color: var(--bar-bg, #000);
border-radius: 1px;
transition: all 0.2s cubic-bezier(0.1, 0.82, 0.76, 0.965);
&:first-of-type {
top: 0;
}
&:last-of-type {
bottom: 0;
}
}
&.active,
.menu-icon__cheeckbox:checked + div {
span {
&:first-of-type {
transform: rotate(45deg);
top: 5px;
}
&:last-of-type {
transform: rotate(-45deg);
bottom: 5px;
}
}
}
&.active:hover span:first-of-type,
&.active:hover span:last-of-type,
&:hover .menu-icon__cheeckbox:checked + div span:first-of-type,
&:hover .menu-icon__cheeckbox:checked + div span:last-of-type {
width: 22px;
}
&:hover {
// no need hover effect on mobile.
@media (min-width: 1024px) {
span:first-of-type {
width: 26px;
}
span:last-of-type {
width: 12px;
}
}
}
}
View Compiled
// Can be used with JavaScript
// -----------------------------
// const icnMenu = document.querySelector('.menu-icon');
// icnMenu.addEventListener('click', () => {
// icnMenu.classList.toggle('active');
// });
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.