Ul不会消失请咨询

时间:2016-06-01 11:45:35

标签: html css

在下面的代码中,我正在尝试练习一个下拉菜单。我想要"第三个"出现在" Second"每当我徘徊在" Second"。为此,我使用了ul li:hover > ul {display: block;}的着名技术,但它似乎没有用。 "第三"永久隐藏,并且在悬停时不会弹出。有人可以就我错在哪里提出建议吗?

代码:



body {
  height: 100%;
  width: 100%;
}
ul {
  background-color: rgba(0, 0, 0, 1);
  color: rgba(255, 0, 0, 1);
  /* [disabled]display: inline-block; */
  text-align: center;
  height: auto;
  position: relative;
  width: 50%;
  margin-right: auto;
  margin-left: auto;
}
ul li {
  display: inline-block;
  border: 1px solid #000;
  list-style-type: none;
  text-align: center;
  margin-right: 10px;
  margin-left: 10px;
}
ul ul {
  display: none;
}
ul li:hover > ul {
  display: block;
}

<ul>
  <li>First</li>
  <li>Second</li>
  <ul>
    <li>Third</li>
  </ul>
</ul>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:2)

您的HTML无效,因为ul > ul无效。将您的内部ul移至li的{​​{1}}:

Second
body {
  height: 100%;
  width: 100%;
}
ul {
  background-color: rgba(0, 0, 0, 1);
  color: rgba(255, 0, 0, 1);
  /* [disabled]display: inline-block; */
  text-align: center;
  height: auto;
  position: relative;
  width: 50%;
  margin-right: auto;
  margin-left: auto;
}
ul li {
  display: inline-block;
  border: 1px solid #000;
  list-style-type: none;
  text-align: center;
  margin-right: 10px;
  margin-left: 10px;
}
ul ul {
  display: none;
}
ul li:hover > ul {
  display: block;
}

答案 1 :(得分:2)

您的<ul> <li>First</li> <li>Second <ul> <li>Third</li> </ul> </li> </ul>菜单应该是drop down的{​​{1}},因此您的HTML必须是这样的

child
li

答案 2 :(得分:0)

第二个ul应位于li内以使其正常工作。

body {
  height: 100%;
  width: 100%;    
}

ul {
  background-color: rgba(0,0,0,1);
  color: rgba(255,0,0,1);
  list-style: none;
  /* [disabled]display: inline-block; */
  text-align: center;
  height: auto;
  position: relative;
  width: 50%;
  margin-right: auto;
  margin-left: auto;
}

.nav > li {
  display: inline-block;
  border: 1px solid #000;
  list-style-type: none;
  position: relative;
  text-align: center;
  margin-right: 10px;
  margin-left: 10px;
}

.nav ul {
  position: absolute;
  text-align: left;
  padding: 0 10px;
  display: none;
  width: 150px;
  top: 100%;
  left: 0;
}

ul li:hover > ul {display: block;}
<ul class="nav">
  <li> First </li>
  <li> Second
    <ul>
      <li> Third </li>
    </ul>
  </li>
</ul>

答案 3 :(得分:0)

&#13;
&#13;
ul{
width:294px;
height:50px;
padding-left:0;
margin:0;
background:#CCCCCC;
}
ul > li{
list-style:none;
display:inline;
padding:13px 19px;
font-size:24px;
float:left;
}
ul > li > ul{
margin:10px -10px;
padding-left:0;
width:auto;
height:55px;
visibility: hidden;
}
ul > li > ul > li{
display:block;
list-style-type:none;
}
ul > li:hover ul{
visibility:visible;
}
&#13;
<ul>
<li>First</li>
<li>
Second
<ul>
<li>Third</li>
</ul>
</li>
<li>Fourth</li>
</ul>
&#13;
&#13;
&#13;