子菜单导航不向左浮动

时间:2017-06-15 12:41:11

标签: html css

我正在尝试创建导航菜单,然后下拉并进一步显示子菜单 我面对子菜单的问题不在于左侧而是在下方浮动

我写的

的html代码



.mainmenu {
    background: rgba(0, 0, 0, 0) url("img/mainmenu_area_bg.png") repeat-x scroll 0 0;
    height: 53px;
}
.mainmenu ul#nav {
    list-style: outside none none;
    margin: 0;
    padding: 0;
    text-align: left;
}
.mainmenu ul#nav li {
    display: inline-block;
}

.mainmenu ul#nav li a {
    color: #fff;
    display: block;
    margin: 0 -2px;
    padding: 17px 20px;
    text-transform: uppercase;
}
.mainmenu ul#nav li:hover> a,
.mainmenu ul#nav li.current{
    background: #11c3f1 none repeat scroll 0 0;
}
/*=================
DropDown Menu
===================*/
#nav > li {
    position: relative;
}
#nav > li:hover ul {
    opacity: 1;
	z-index:9999;top: 100%;
	visibility:visible;
}
#nav > li ul {
  background: #fff none repeat scroll 0 0;
  opacity: 0;
  left: -2px;
  list-style: outside none none;
  margin: 0;
  padding: 0;
  position: absolute;
  text-align: left;
  transition: all 0.5s ease 0s;
  width: 204px;
  padding-left: 2px;
  z-index:-99999;
  top: 150%;
  visibility:hidden
}
#nav > li ul li {
    display: block;
    width: 200px;

}
.mainmenu ul#nav li ul li a:hover {
    color: #fff;
    border-radius: 0 0 80px 0;
    border-left: 4px solid #000;
    padding-left: 10px;
}
.mainmenu ul#nav li ul li a {
    border-bottom: 1px solid #ddd;
    color: #000;
    padding: 10px 10px;
}


#nav ul .subNav ul {
	display: none;
	 width: 100%;
	 list-style: outside none none;
}
#nav ul .subNav:hover ul{
	display: block;
	
}
#nav ul .subNav ul li {
	background-repeat: repeat-x;
	border-top: 1px solid #999;
	 float: left;
    list-style: none;
}
#nav ul .subNav ul li a {
	color: #fff;
    display: block;
    margin: 0 -2px;
    padding: 17px 20px;
    text-transform: uppercase;
	float: left;
	width:100%;
}
#nav li.subNav ul li a:link, #nav li.subNav ul li a:visited {
	color: #333;
}
#nav li.subNav ul li a:hover, #nav li.subNav ul li a:active {
	color: #F34B2B;
}

 <div class="mainmenu nav">
 <nav>
<ul id="nav">
<li><a href="services.php">Service</a>
	<ul> 
	 <li><a href="#">Menu</a></li>                  
   <li class="subNav"><a href="#">Menu2</a>
					<ul class="subNav">
						 <li><a href="#">Piano</a></li>                                   	 <li><a href="#"> Couch</a></li>
						 <li><a href="#">Appliances</a></li>
					 </ul>
          </li> 
			</ul> 
</li> 
</ul>
</nav>
</div>
&#13;
&#13;
&#13;

将光标放在结果页面中,你会看到输出,我想要一个人把光标带到MENU2,它应该浮动到左侧而不是向下。

提前致谢。

1 个答案:

答案 0 :(得分:1)

在这里,你需要让包含subnav的li相对显示,这样子子菜单可以在左边绝对定位200px,在li中引用0的顶部。

&#13;
&#13;
.mainmenu {
    background: rgba(0, 0, 0, 0) url("img/mainmenu_area_bg.png") repeat-x scroll 0 0;
    height: 53px;
}
.mainmenu ul#nav {
    list-style: outside none none;
    margin: 0;
    padding: 0;
    text-align: left;
}
.mainmenu ul#nav li {
    display: inline-block;
    position:relative;
}

.mainmenu ul#nav li a {
    color: #fff;
    display: block;
    margin: 0 -2px;
    padding: 17px 20px;
    text-transform: uppercase;
}
.mainmenu ul#nav li:hover> a,
.mainmenu ul#nav li.current{
    background: #11c3f1 none repeat scroll 0 0;
}
/*=================
DropDown Menu
===================*/
#nav > li {
    position: relative;
}
#nav > li:hover ul {
    opacity: 1;
	z-index:9999;top: 100%;
	visibility:visible;
}
#nav > li ul {
  background: #fff none repeat scroll 0 0;
  opacity: 0;
  left: -2px;
  list-style: outside none none;
  margin: 0;
  padding: 0;
  position: absolute;
  text-align: left;
  transition: all 0.5s ease 0s;
  width: 204px;
  padding-left: 2px;
  z-index:-99999;
  top: 150%;
  visibility:hidden
}
#nav > li ul li {
    display: block;
    width: 200px;

}
.mainmenu ul#nav li ul li a:hover {
    color: #fff;
    border-radius: 0 0 80px 0;
    border-left: 4px solid #000;
    padding-left: 10px;
}
.mainmenu ul#nav li ul li a {
    border-bottom: 1px solid #ddd;
    color: #000;
    padding: 10px 10px;
}


#nav ul .subNav ul {
	display: none;
	 width: 100%;
	 list-style: outside none none;
}
#nav ul .subNav:hover ul{
	display: block;
  left:200px;
  top:0;	
}
#nav ul .subNav ul li {
	background-repeat: repeat-x;
	border-top: 1px solid #999;
	 float: left;
    list-style: none;
}
#nav ul .subNav ul li a {
	color: #fff;
    display: block;
    margin: 0 -2px;
    padding: 17px 20px;
    text-transform: uppercase;
	float: left;
	width:100%;
}
#nav li.subNav ul li a:link, #nav li.subNav ul li a:visited {
	color: #333;
}
#nav li.subNav ul li a:hover, #nav li.subNav ul li a:active {
	color: #F34B2B;
}
&#13;
<div class="mainmenu nav">
 <nav>
<ul id="nav">
<li><a href="services.php">Service</a>
	<ul> 
	 <li><a href="#">Menu</a></li>                  
   <li class="subNav"><a href="#">Menu2</a>
					<ul class="subNav">
						 <li><a href="#">Piano</a></li>                                   	 <li><a href="#"> Couch</a></li>
						 <li><a href="#">Appliances</a></li>
					 </ul>
          </li> 
			</ul> 
</li> 
</ul>
</nav>
</div>
&#13;
&#13;
&#13;