Search code examples
htmlcssnav

When i hover over li only the text get's altered but not the whole box . I wonder why?


I have been trying to resolve this i.e. I wanted the navbar to alter, when I hover over li the bg-color will be orange and the text to be grey, this happened but this change didn't happen to the whole box instead only the text.

body {
    margin: 0%;
    padding: 0%;
}

#container {
    background-color: rgb(223, 223, 223);
}

ul {
    list-style-type: none;
    color: #ff812c;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

li {
    float: left;
    padding: 2%;
    font-family: 'Poppins', sans-serif;
    color: #ff812c;
}

li a {
    text-decoration: none;
    display: block;
    color: #ff812c;
}

img {
    width: 20%;
    height: 20%;
    float: right;
}

li :hover {
    color: rgb(223, 223, 223);
    background-color: #ff812c;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NavBar</title>
    <link rel="stylesheet" href="nav.css">
</head>
<body>
 <nav>
    <div id="container">
        <img src="" alt="logo">

        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Products</a></li>
            <li><a href="#">Contact</a></li>
            <li><a href="#">About</a></li>
        </ul>
    </div>
 </nav>  
</body>
</html>

This is when I tried to make the background orange and text grey but this is applied only to the text.

I even tried making block but still the same response.


Solution

  • 1) It should be li:hover not li :hover. Just remove space

    li:hover {
        background-color: #ff812c;
    }
    

    2) You have to change the color of a when you hover over the li

    li:hover a {
        color: rgb(223, 223, 223);
    }
    

    body {
      margin: 0%;
      padding: 0%;
    }
    
    #container {
      background-color: rgb(223, 223, 223);
    }
    
    ul {
      list-style-type: none;
      color: #ff812c;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }
    
    li {
      float: left;
      padding: 2%;
      font-family: 'Poppins', sans-serif;
      color: #ff812c;
    }
    
    li a {
      text-decoration: none;
      display: block;
      color: #ff812c;
    }
    
    img {
      width: 20%;
      height: 20%;
      float: right;
    }
    
    li:hover {
      background-color: #ff812c;
    }
    
    li:hover a {
      color: rgb(223, 223, 223);
    }
    <div id="container">
      <img src="" alt="logo">
    
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Products</a></li>
        <li><a href="#">Contact</a></li>
        <li><a href="#">About</a></li>
      </ul>
    </div>