skip to Main Content

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


  1. Error is due to Positioning and z-index of the elements in your CSS

    .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;
        z-index: 10; 
        transition: right 0.3s ease; 
    
    
    }
    
    .menu {
    
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: start;
        position: fixed;
        transition: transform 0.3s ease;
        transform: translateX(100%); 
        z-index: 5; 
    }
    
    .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;
    }
    

    }

    Try this…it might help

    Login or Signup to reply.
  2. 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).

    @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;
        overflow: hidden;
        margin: 0;
        padding: 0;
    
    
    }
    
    
    .header{
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
    
    .logo-image {
        margin: 15px;
    }
    
    ul.ul-menu {
        margin: 0;
        padding: 0;
        display: inline;
        justify-content: space-between;
        text-transform: uppercase;
        list-style: none;
        display: flex;
        gap: 2rem;
        align-items: center;
        justify-content: space-between;
    }
    
    .header ul a {
        display: block;
        height: 100%;
        color: rgba(42, 19, 19, 1);
        text-decoration: none;
        margin-left: 25px;
    }
    
    .header 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: 0px;
        right: 20px;
        width: 25px;
        height: 40px;
        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;
    }
    
    
    @media (max-width:992px) {
    
    
        .menu-toggle:checked~ul.ul-menu{ /*select every ul that is preceded by a checked .menu-toggle*/
            position: absolute;
            top: 0;
            right: 50%;
            transform: translate(50%);  /* this to properly center the ul menu try comment it to see*/
            display: flex;
            flex-direction: column;
            transition: all 500ms ease;
    
        }
    
        .header-right{
            display: none;
        }
    
        .menu-toggle,
        .hamburger {
            display: block;
        }
    
        ul.ul-menu {
            position: absolute;
            top: 0;
            right: -200px;
            display: flex;
            flex-direction: column;
        }
    
        .ul-menu a {
            margin: 0;
            display: flex;
            text-align: center;
            align-items: center;
            justify-content: center;
            padding: 25px;
            font-size: 20px;
            border-width: 1px;
            box-shadow: 0 1px 0 0 rgba(255, 255, 245, .5) inset;
        }
    
        .header ul li:hover {
            color: white;
        }
    }
      <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>
        <input type="checkbox" class="menu-toggle">
        <div class="hamburger"></div>
        <ul class="ul-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 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>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search