0

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.

Sebastian Richner
  • 782
  • 1
  • 13
  • 21

4 Answers4

1

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>
DecPK
  • 24,537
  • 6
  • 26
  • 42
0

Remove space on li:hover

li:hover {
    color: rgb(223, 223, 223);
    background-color: #ff812c;
}
li:hover a{
    color: rgb(223, 223, 223);
}

CSS:
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;
}
li:hover a{
    color: rgb(223, 223, 223);}



HTMl:
<!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>
Lalji Tadhani
  • 14,041
  • 3
  • 23
  • 40
0

You can also move the li padding to the a, so the a fills entirely li :

example

CSS:
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;
    font-family: 'Poppins', sans-serif;
    color: #ff812c;
    
   
}

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


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

li :hover {
    color: rgb(223, 223, 223);
    background-color: #ff812c;
}



HTMl:
<!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>
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129
0

#container{
  display: flex;
  flex-direction: row;
}
#container ul{
  display: flex;
  flex-direction: row;
  margin-left: 2em;
}
#container ul li{
  list-style: none;
}
#container ul li a{
  margin : 0em 0.4em;
  text-decoration: none;
  padding: 10px 18px;

}
#container ul li a:hover{
  background: purple;
  color: white;
}
<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>