当我将鼠标悬停在 li 上时,只有文本被更改,而不是整个框。我想知道为什么?

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

我一直在尝试解决这个问题,即我希望改变导航栏,当我将鼠标悬停在 li 上时,背景颜色将变为橙色,文本将变为灰色,这发生了,但这种变化并没有发生在整个框而不是只有文本。

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>

这是我尝试将背景设置为橙色并将文本设置为灰色,但这仅适用于文本。

我什至尝试过阻止,但仍然是同样的反应。

1) 应该是 li:hover 而不是 li :hover只需删除 space

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

2) 当鼠标悬停在 li 上时,必须更改 a 的颜色

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>

li:hover

上删除 space
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>

您也可以将 li 填充移动到 a,这样 a 就完全填充 li :

例子

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>

#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>