Search code examples
javascripthtmlcssuser-experience

Nav Bar Toggle Not Turning Text White


Question

I can't understand why the text on the nav bar (when opened wit the Burger Menu) doesn't go white. I have tried everything to make it go white.

Please help me identify why the text won't go white when the hamburger menu is open.

My Code

//Nav Bar

const navSlide = () =>{
    const burger = document.querySelector('.burger');
    const nav = document.querySelector('.nav-links');
    const navLinks = document.querySelectorAll('.nav-links li');

// Toggle Nav
    burger.addEventListener('click',()=>{
        nav.classList.toggle('nav-active');


    //Animate Links
    navLinks.forEach((link, index)=>{
        if(link.style.animation){
            link.style.animation = '';
        } else {
        link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`
        }
   
    })
    //Burger Animation

    burger.classList.toggle('toggle');

})

}

navSlide();
nav{
    display: flex;
    justify-content: space-around;
    align-items: center;
    min-height: 8vh;
    font-family: 'Poppins', sans-serif;
    background-color: #ffffff;

}

.logo{
    color: #245871;
    text-transform: uppercase;
    letter-spacing: 5px;
    font-size: 20px;
}

.nav-links{
    display: flex;
    justify-content: space-around;
    width: 50%;
    
}

.nav-links li{
   list-style: none;
}

.nav-links a{
    color: #245871;
    text-decoration: none;
    letter-spacing: 3px;
    font-weight: 800;
    font-size: 18px;

}

.burger div{
    width: 25px;
    height: 3px;
    background-color: #245871;
    margin: 5px;
    transition: all 0.3s ease;
}

.burger{
    display: none;
    cursor: pointer;
}


@media screen and (max-width: 1024){
    .nav-links{
        width: 60%;
    }
}

@media screen and (max-width: 997px){
   body{
       overflow-x: hidden;
   }
   
   nav{
       color: white;
   }

   .test{
       color: white;
   }
    .nav-links{
        position: absolute;
        right: 0px;
        height: 92vh;
        top: 8vh;
        background-color: #245871;
        color: white;
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 50%;
        transform: translateX(100%);
        transition: transform 0.5s ease-in;
    }
    .nav-links a li{
        opacity: 1;
        color: white;
    }
    .burger{
      display: block;
    }
}

.nav-active{
    transform: translateX(0%);
    color: white;
}

@keyframes navLinkFade{
    from{
        opacity: 0;
        transform: translateX(50px);
    } to {
        opacity: 1;
        transform: translateX(0px);
    }
}


.toggle .line1{
    transform: rotate(-45deg) translate(-5px, 6px);
}

.toggle .line2{
    opacity: 0;
}

.toggle .line3{
    transform: rotate(45deg) translate(-5px, 6px);
}
 <body>
       
        <nav>
            <div class="logo">
                    <h4>Website</h4>    
            </div>
            <ul class="nav-links">
                <li class=".test"><a href="#">xxxx</a></li>
                <li class=".test"><a href="#">xxx</a></li>
                <li><a href="#">xxxx</a></li>
                <!-- browsers -->
                <li><a href="#">Free xxx</a></li>
                <li><a href="#">xxxx</a></li>
            </ul>
            <div class="burger">    
                <div class="line1"></div>
                <div class="line2"></div>
                <div class="line3"></div>

            </div>
        </nav>

Help Greatly Appreciated!

The issue appears to be in the classes that I am adding (or making applicable through the media queries, that are not actually working in practice).


Solution

  • Change,

    .nav-links a li{
        opacity: 1;
        color: white;
     }
    

    to

    .nav-links li a{
        opacity: 1;
        color: white;
    }
    

    Please make sure you follow the right order in mentioning the css selector.

    You have mentioned .nav-links a li but actual order is .nav-links li a

    Modified Snippet as follows,

    //Nav Bar
    
    const navSlide = () =>{
        const burger = document.querySelector('.burger');
        const nav = document.querySelector('.nav-links');
        const navLinks = document.querySelectorAll('.nav-links li');
    
    // Toggle Nav
        burger.addEventListener('click',()=>{
            nav.classList.toggle('nav-active');
    
    
        //Animate Links
        navLinks.forEach((link, index)=>{
            if(link.style.animation){
                link.style.animation = '';
            } else {
            link.style.animation = `navLinkFade 0.5s ease forwards ${index / 7 + 0.5}s`
            }
       
        })
        //Burger Animation
    
        burger.classList.toggle('toggle');
    
    })
    
    }
    
    navSlide();
    nav{
        display: flex;
        justify-content: space-around;
        align-items: center;
        min-height: 8vh;
        font-family: 'Poppins', sans-serif;
        background-color: #ffffff;
    
    }
    
    .logo{
        color: #245871;
        text-transform: uppercase;
        letter-spacing: 5px;
        font-size: 20px;
    }
    
    .nav-links{
        display: flex;
        justify-content: space-around;
        width: 50%;
        
    }
    
    .nav-links li{
       list-style: none;
    }
    
    .nav-links a{
        color: #245871;
        text-decoration: none;
        letter-spacing: 3px;
        font-weight: 800;
        font-size: 18px;
    
    }
    
    .burger div{
        width: 25px;
        height: 3px;
        background-color: #245871;
        margin: 5px;
        transition: all 0.3s ease;
    }
    
    .burger{
        display: none;
        cursor: pointer;
    }
    
    
    @media screen and (max-width: 1024){
        .nav-links{
            width: 60%;
        }
    }
    
    @media screen and (max-width: 997px){
       body{
           overflow-x: hidden;
       }
       
       nav{
           color: white;
       }
    
       .test{
           color: white;
       }
        .nav-links{
            position: absolute;
            right: 0px;
            height: 92vh;
            top: 8vh;
            background-color: #245871;
            color: white;
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 50%;
            transform: translateX(100%);
            transition: transform 0.5s ease-in;
        }
        .nav-links li a{
            opacity: 1;
            color: white;
        }
        .burger{
          display: block;
        }
    }
    
    .nav-active{
        transform: translateX(0%);
        color: white;
    }
    
    @keyframes navLinkFade{
        from{
            opacity: 0;
            transform: translateX(50px);
        } to {
            opacity: 1;
            transform: translateX(0px);
        }
    }
    
    
    .toggle .line1{
        transform: rotate(-45deg) translate(-5px, 6px);
    }
    
    .toggle .line2{
        opacity: 0;
    }
    
    .toggle .line3{
        transform: rotate(45deg) translate(-5px, 6px);
    }
    <body>
           
            <nav>
                <div class="logo">
                        <h4>Website</h4>    
                </div>
                <ul class="nav-links">
                    <li class=".test"><a href="#">xxxx</a></li>
                    <li class=".test"><a href="#">xxx</a></li>
                    <li><a href="#">xxxx</a></li>
                    <!-- browsers -->
                    <li><a href="#">Free xxx</a></li>
                    <li><a href="#">xxxx</a></li>
                </ul>
                <div class="burger">    
                    <div class="line1"></div>
                    <div class="line2"></div>
                    <div class="line3"></div>
    
                </div>
            </nav>