I’m trying to make a responsive header that when the screen size is smaller than 992 a hamburger menu appears that controls the menu using a checkbox.
The problem I’m trying to solve is the menu not showing even if the checkbox is checked.
What I have so far is below.
@import url("https://fonts.googleapis.com/css2?family=Comfortaa:wght@400;700&family=Roboto&display=swap");
html {
height: 100%;
}
body {
height: 100%;
background-color: #fcddc6;
margin: 0;
padding: 0;
}
body::-webkit-scrollbar {
display: none;
}
.header:not(.menu) {
display: flex;
align-items: center;
justify-content: space-between;
}
.logo-image {
margin: 15px;
}
.header-middle ul {
margin: 0;
padding: 0;
display: inline;
justify-content: space-between;
text-transform: uppercase;
list-style: none;
display: flex;
align-items: center;
justify-content: space-between;
}
.header-middle ul a {
color: rgba(42, 19, 19, 1);
text-decoration: none;
margin-left: 25px;
}
.header-middle ul a li {
font-family: 'Comfortaa';
font-weight: 400;
font-size: 25px;
}
#header-right-links {
font-size: 25px;
margin-right: 15px;
}
.hamburger,
.menu-toggle {
display: none;
}
.hamburger {
margin: 0;
position: relative;
width: 25px;
height: 4px;
right: 20px;
background-color: rgba(42, 19, 19, 1);
border-radius: 3px;
cursor: pointer;
z-index: 100;
transition: .3s ease;
}
.hamburger::before,
.hamburger::after {
content: "";
border-radius: 10px;
position: absolute;
height: 4px;
right: 0;
background-color: rgba(42, 19, 19, 1);
transition: .3s ease;
}
.hamburger::before {
top: -10px;
width: 20px;
}
.hamburger::after {
top: 10px;
width: 30px;
}
.menu-toggle {
top: 34px;
right: 20px;
width: 25px;
height: 20px;
position: absolute;
z-index: 1000;
cursor: pointer;
opacity: 0;
}
.menu-toggle:checked+.hamburger {
background: transparent;
}
.menu-toggle:checked+.hamburger:before {
top: 0;
transform: rotate(-45deg);
width: 30px;
}
.menu-toggle:checked+.hamburger:after {
top: 0;
transform: rotate(45deg);
width: 30px;
}
.header .menu-toggle:checked+.header-middle,
.menu-toggle:checked~.menu,
.menu-toggle:checked+.header-right {
right: 0;
opacity: 1;
}
@media (max-width:992px) {
.menu-toggle,
.hamburger {
display: block;
}
.header-middle,
.header-right {
display: flex;
flex-direction: column;
align-items: center;
justify-content: start;
position: fixed;
top: 85px;
width: 300px;
height: 800px;
right: -300px;
padding-top: 30px;
background-color: #dc9a7f;
border-width: 1px;
border-color: #dc9a7f;
border-style: solid;
}
.menu {
display: flex;
flex-direction: column;
align-items: center;
justify-content: start;
position: fixed;
}
.header-middle li {
width: 100%;
}
.menu a {
margin: 0;
display: flex;
align-items: center;
justify-content: flex-start;
padding: 25px;
font-size: 20px;
border-width: 1px;
box-shadow: 0 1px 0 0 rgba(255, 255, 245, .5) inset;
}
.header-middle ul li:hover {
color: white;
}
.header-right {
background: transparent;
height: auto;
top: 455px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-around;
}
}
<script src="https://kit.fontawesome.com/d92fc73161.js" crossorigin="anonymous"></script>
<div class="header">
<div class="header-left">
<a class="logo-link" href="https://www.example.com/"><img class="logo-image" src="https://picsum.photos/30/30" alt="" /></a>
</div>
<div class="menu">
<div class="header-middle">
<ul class="menu">
<a href="https://www.example.com/">
<li>Home</li>
</a>
<a href="https://www.example.com/">
<li>Products</li>
</a>
<a href="https://www.example.com/">
<li>About Us</li>
</a>
<a href="https://www.example.com/">
<li>Contact Us</li>
</a>
</ul>
</div>
</div>
<div class="header-right">
<i id="header-right-links" class="fa-solid fa-magnifying-glass"></i>
<i id="header-right-links" class="fa-regular fa-heart"></i>
<i id="header-right-links" class="fa-solid fa-cart-shopping"></i>
</div>
<input type="checkbox" class="menu-toggle">
<div class="hamburger"></div>
</div>
I’m struggling to find a solution, and I’m at a loss as to how to proceed.
2
Answers
Error is due to Positioning and z-index of the elements in your CSS
}
Try this…it might help
I changed your HTML to be able to select your ul using CSS selector (~) when checkbox is checked. I changed also position for ul as absolute to give it a top and right position. I also used overflow: hidden; not to have the x scrollbar. Fontawsome icons were display : none with small screen (you can try placing it wherever you want using the same procedure).