下拉菜单无法正常运行

时间:2019-05-20 20:36:55

标签: html css

我正在尝试使用CSS创建一个下拉菜单。当我将鼠标悬停在菜单项上时,它无法按预期工作。它不显示该项目下的任何类别,我不知道为什么。

我不想使用JavaScript,我只想使用纯CSS。

https://jsfiddle.net/3dovsL0g/

*{
  margin:0px;
  padding:0px;
}
#container ul{
  list-style:none;	
}
#container ul li{
  background-color:#3c3e94;
  width:150px;
  border:1px solid white;
  height:50px;
  line-height: 50px;
  text-align:center;
  float:left;
  color:white;
  font-size:18px;
  position:relative;
}
#container ul ul{
  display:none;	
}
#container ul li:hover{
  background-color:#388222;
}
#container ul li:hover > ul{
  display:block;	
}
#container ul ul ul{
  transform:translateX(100%);
  top:0px;
  position:absolute;
}
h4{
  position:absolute;
  top:50%;
  left:50%;
  transform:translate(-50%,-50%);
}
<div id="container">
  <ul>
    <li><h4>Home</h4></li>
    <li><h4>About</h4></li>
    <li><h4>Entertainment</h4></li>
      <ul>
        <li><h4>Hollywood</h4></li>
        <li><h4>Jollywood</h4></li>
          <ul>
            <li><h4>steve</h4></li>
            <li><h4>jobs</h4></li>
            <li><h4>john</h4></li>
          </ul>
        <li><h4>Bollywood</h4></li>
      </ul>
    <li><h4>Contact</h4></li>
  </ul>	
</div>

3 个答案:

答案 0 :(得分:1)

使用此代码,然后通过dropDown更改菜单

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

li {
  float: left;
}

li a, .dropbtn {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {
  background-color: red;
}

li.dropdown {
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {background-color: #f1f1f1;}

.dropdown:hover .dropdown-content {
  display: block;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<ul>
  <li><a href="#home">Home</a></li>
  <li><a href="#news">News</a></li>
  <li class="dropdown">
    <a href="javascript:void(0)" class="dropbtn">Dropdown</a>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </li>
</ul>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Hover over the "Dropdown" link to see the dropdown menu.</p>

</body>
</html>

答案 1 :(得分:1)

您需要将子菜单放在悬停元素内,以便它们成为其子元素,然后只需使用class来定位它们。在这里试试。

 .hasSub:hover .sub-menu{
    display: block !important;
  }
  .hasSub1:hover .sub-menu1{
    display: block !important;
  }

*{
		margin:0px;
		padding:0px;
	}
	#container ul{
		list-style:none;	
	}
	#container ul li{
		background-color:#3c3e94;
		width:150px;
		border:1px solid white;
		height:50px;
		line-height: :50px;
		text-align:center;
		float:left;
		color:white;
		font-size:18px;
		position:relative;
	}
	#container ul ul{
		display:none;	
	}
	#container ul li:hover{
		background-color:#388222;
	}
	
	#container ul ul ul{
		transform:translateX(100%);
		top:0px;
		position:absolute;
	}
	h4{
		position:absolute;
		top:50%;
		left:50%;
		transform:translate(-50%,-50%);
	}
  
  .hasSub:hover .sub-menu{
    display: block !important;
  }
  .hasSub1:hover .sub-menu1{
    display: block !important;
  }
<div id="container">
	<ul>
		<li><h4>Home</h4></li>
		<li><h4>About</h4></li>
		<li class="hasSub"><h4>Entertainment</h4>
			<ul class="sub-menu">
				<li><h4>Hollywood</h4></li>
				<li class="hasSub1"><h4>Jollywood</h4>
					<ul class="sub-menu1">
						<li><h4>steve</h4></li>
						<li><h4>jobs</h4></li>
						<li><h4>john</h4></li>
					</ul>
                </li>
				<li><h4>Bollywood</h4></li>
			</ul>
       </li>
		<li><h4>Contact</h4></li>
	</ul>	
</div>

和小提琴:https://jsfiddle.net/kpdwf9th/1/

答案 2 :(得分:0)

您需要将<ul>标签嵌套在每个<li>元素中。从语义上讲,子项应该是父母的孩子。

.nav { /* style nav bar */
  display: flex;
  color: #fff;
  list-style: none;
  padding: 0;
}
.nav li { /* style ALL li tags */
  position: relative;
  padding: 12px;
  background-color: #333;
}
.nav li > ul { /* hide all ul tags at first */
  display: none;
  list-style: none;
  padding: 0;
  position: absolute;
  z-index: 1;
}
.nav li:hover > ul { /* show ul tags when parent li is hovering */
  display: block;
}
.nav li > ul > li > ul { /* move 3rd layer ul to the right */
  left: 100%;
  top: 0;
}
<ul class="nav">
  <li>Home</li>
  <li>About</li>
  <li>Entertainment
    <ul>
      <li>Hollywood</li>
      <li>Jollywood
        <ul>
          <li>steve</li>
          <li>jobs</li>
          <li>john</li>
        </ul>
      </li>
      <li>Bollywood</li>
    </ul>
  </li>
  <li>Contact</li>
</ul>