html – CSS宽度转换不起作用

下面是我正在使用的
HTML和CSS.不幸的是,已经提出的问题没有给出答案.基本上它减少了所有兄弟姐妹的宽度并增加了悬停的兄弟姐妹的宽度.我正在使用轻松进入,但转换的OUT部分瞬间跳回原来的状态.

html {
  background: #000066;
}
body {
  height: 100%;
  background: #cfcfd0;
  margin: 4% 4% 4% 4%;
}
#title {
  background: ;
  text-align: center;
  margin: 2% 2%;
  padding: 2% 2%;
}
#nav {
  width: 90%;
  overflow: auto;
  margin: 0 auto;
  padding: 0px 0px 0px 0px
}
ul {
  margin: 4% auto;
  padding: 0;
  width: 100%;
  list-style-type: none;
  overflow: auto;
}
li {
  box-sizing: border-box;
  text-align: center;
  display: inline-block;
  float: left;
  width: 33.33%;
  padding: 2% 0;
  margin: 0%;
  background: blue;
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  border-right: 1px solid black;
}
li:first-child {
  border-left: 1px solid black;
}
li:last-child {
  border-right: 1px solid black;
}
a {
  color: black;
  text-decoration: none;
}
ul:hover > li:hover {
  width: 37.33%;
  color: white;
  background: darkblue;
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  -ms-transition: all 300ms ease-in-out;
  -o-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
}
ul:hover > li {
  width: 31.33%;
  -webkit-transition: width 300ms ease-in-out;
  -moz-transition: width 300ms ease-in-out;
  -ms-transition: width 300ms ease-in-out;
  -o-transition: width 300ms ease-in-out;
  transition: width 300ms ease-in-out;
}
<div id="title">
  <h1>Projects Home</h1>
</div>
<div id="nav">
  <ul>
    <li><a href="">Project 1</a>
    </li>
    <li><a href="">Project 2</a>
    </li>
    <li><a href="">Project 3</a>
    </li>
  </ul>
</div>

我无法弄清楚为什么会这样.

最佳答案 只有匹配选择器时,任何CSS属性才会应用于元素.在:hover选择器下指定transition属性时,只有在悬停时才会应用它们.当我们将鼠标悬停时,它们会快速恢复,因为转换设置不再适用于该元素.

在您的情况下,因为转换仅在ul:hover>中指定. li:hover和ul:hover>只有当鼠标悬停在li上或当鼠标至少超过ul时(即当我们从一个li移动到另一个li而仍然在ul边界内时),它才被应用.

要使转换在鼠标移出期间正常工作,应在li选择器中指定,如下面的代码段所示.

html {
  background: #000066;
}
body {
  height: 100%;
  background: #cfcfd0;
  margin: 4% 4% 4% 4%;
}
#title {
  background: ;
  text-align: center;
  margin: 2% 2%;
  padding: 2% 2%;
}
#nav {
  width: 90%;
  overflow: auto;
  margin: 0 auto;
  padding: 0px 0px 0px 0px
}
ul {
  margin: 4% auto;
  padding: 0;
  width: 100%;
  list-style-type: none;
  overflow: auto;
}
li {
  box-sizing: border-box;
  text-align: center;
  display: inline-block;
  float: left;
  width: 33.33%;
  padding: 2% 0;
  margin: 0%;
  background: blue;
  border-top: 1px solid black;
  border-bottom: 1px solid black;
  border-right: 1px solid black;
  -webkit-transition: all 300ms ease-in-out;
  -moz-transition: all 300ms ease-in-out;
  -ms-transition: all 300ms ease-in-out;
  -o-transition: all 300ms ease-in-out;
  transition: all 300ms ease-in-out;
}
li:first-child {
  border-left: 1px solid black;
}
li:last-child {
  border-right: 1px solid black;
}
a {
  color: black;
  text-decoration: none;
}
ul:hover > li:hover {
  width: 37.33%;
  color: white;
  background: darkblue;
}
ul:hover > li {
  width: 31.33%;
}
<div id="title">
  <h1>Projects Home</h1>
</div>
<div id="nav">
  <ul>
    <li><a href="">Project 1</a>
    </li>
    <li><a href="">Project 2</a>
    </li>
    <li><a href="">Project 3</a>
    </li>
  </ul>
</div>
点赞