下拉菜单堆叠在一起

时间:2016-06-28 09:56:28

标签: css

我真的需要帮助我们尽快在我的网站上正确显示下拉子菜单。

我的下拉菜单中的子菜单相互堆叠

我希望这些子菜单垂直显示。

我该如何解决这个问题?

这是我的代码:

div#nav_menu-2{
  	float: left!important;
}

div.menu-secondary-container{
  	display: block;
    background-color: #f3e6ff;
	background-size: cover;
	margin: 0px;
	padding: 0px;
	overflow: visible;
	position: static;
}

div#nav_menu-2 ul{
    top: 0px;
    margin: 0px;
}

div#nav_menu-2 ul li{
   	list-style-type: none;
	/*position: relative;*/
	display: inline-block;
}

div#nav_menu-2 ul li a{
  	display: block;
	padding: 14px 16px;
	text-align: center;
	line-height: 30px;
	text-decoration: none;
	font-size: 12pt;
	font-weight: bold;
	color: purple;
	text-transform: uppercase;
}

div#nav_menu-2 ul li a:hover{
	background-color: lightgray;
}

div#nav_menu-2 ul li a:active{
	background-color: hotpink;
}

div#nav_menu-2 ul li a:link{
	color: darkpurple;
}

div#nav_menu-2 ul li a:visited{
	color: lightpurple;
}

.active{
	background-color: dimgray;
}

div#nav_menu-2 ul li li{
  	display:none;
  	clear: both;
  	overflow: auto;
  	white-space: nowrap;
}

div#nav_menu-2 ul li:hover ul li{
	display: block;
    position:absolute;
   
}

div#nav_menu-2 ul li li a{
	background-color: lightgray;
	padding: 14px 16px;
	min-width: 160px;
	display: block;
	text-align: center;
}
<div id="nav_menu-2" class="header-widget widget_nav_menu header-widget-1 header-widget-num-1">
<div class="menu-secondary-container">
  <ul id="menu-secondary" class="menu">
    <li id="menu-item-115" class="menu-item menu-item-     type-post_type menu-item-object-page menu-item-has-children menu-item-115">
      <a href="http://localhost/careerdishanepal/services/">Services</a>
      <ul class="sub-menu">
	    <li id="menu-item-126" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-126">
          <a href="http://localhost/careerdishanepal/background/">Background</a></li>
	<li id="menu-item-129" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-129">
      <a href="http://localhost/careerdishanepal/market-survey-reports/">Market Survey Reports</a></li>
	<li id="menu-item-131" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-131">
      <a href="http://localhost/careerdishanepal/behind-the-scene/">Behind the scene</a></li>
</ul>
</li>
  <li id="menu-item-116" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-116">
    <a href="http://localhost/careerdishanepal/about-us/">About Us</a>
<ul class="sub-menu">
	<li id="menu-item-128" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-128">
      <a href="http://localhost/careerdishanepal/goals-and-objectives/">Goals and Objectives</a></li>
	<li id="menu-item-130" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-130">
      <a href="http://localhost/careerdishanepal/social-need-assessment-study/">Social Need Assessment Study</a></li>
</ul>
</li>
<li id="menu-item-117" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-117">
  <a href="http://localhost/careerdishanepal/contact/">Contact</a></li>
</ul>
  </div>
</div>

2 个答案:

答案 0 :(得分:2)

您错误地隐藏了子菜单<li>而不是子菜单<ul>。除此之外,您的代码主要是工作。

这是一个小提琴:https://jsfiddle.net/rkwcty1u/

您现在会注意到:hover它显示的是<ul>而不是<li>。子菜单<ul>也位于absolute,父级<li>位于relative位置,因此它正确包含它。

答案 1 :(得分:0)

我已将以下代码添加到您的 CSS ,并在padding中修改了line-heightdiv#nav_menu-2 ul li a

div#nav_menu-2 ul ul {
  position: absolute;
  left: 0;
  top: 40px;
}
div#nav_menu-2 ul li:hover a {
  background-color: lightgray;
} 

&#13;
&#13;
div#nav_menu-2 {
  float: left;
}
div.menu-secondary-container {
  display: block;
  background-color: #f3e6ff;
  background-size: cover;
  margin: 0px;
  padding: 0px;
  overflow: visible;
  position: relative;
}
div#nav_menu-2 ul {
  top: 0px;
  margin: 0px;
}
div#nav_menu-2 ul li {
  list-style-type: none;
  display: inline-block;
}
div#nav_menu-2 ul li a {
  display: block;
  padding: 0px 16px;
  text-align: center;
  line-height: 40px;
  text-decoration: none;
  font-size: 12pt;
  font-weight: bold;
  color: purple;
  text-transform: uppercase;
}
div#nav_menu-2 ul li a:hover {
  background-color: lightgray;
}
div#nav_menu-2 ul li a:active {
  background-color: hotpink;
}
div#nav_menu-2 ul li a:link {
  color: darkpurple;
}
div#nav_menu-2 ul li a:visited {
  color: lightpurple;
}
.active {
  background-color: dimgray;
}
div#nav_menu-2 ul li li {
  display: none;
  clear: both;
  overflow: auto;
  float: none;
  white-space: nowrap;
}
div#nav_menu-2 ul li:hover ul li {
  display: block;
}
div#nav_menu-2 ul li li a {
  background-color: lightgray;
  padding: 14px 16px;
  min-width: 160px;
  display: block;
  text-align: center;
}
div#nav_menu-2 ul ul {
  position: absolute;
  left: 0;
  top: 40px;
}
div#nav_menu-2 ul li:hover a {
  background-color: lightgray;
}
&#13;
<div id="nav_menu-2" class="header-widget widget_nav_menu header-widget-1 header-widget-num-1">
  <div class="menu-secondary-container">
    <ul id="menu-secondary" class="menu">
      <li id="menu-item-115" class="menu-item menu-item-     type-post_type menu-item-object-page menu-item-has-children menu-item-115">
        <a href="http://localhost/careerdishanepal/services/">Services</a>
        <ul class="sub-menu">
          <li id="menu-item-126" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-126">
            <a href="http://localhost/careerdishanepal/background/">Background</a>
          </li>
          <li id="menu-item-129" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-129">
            <a href="http://localhost/careerdishanepal/market-survey-reports/">Market Survey Reports</a>
          </li>
          <li id="menu-item-131" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-131">
            <a href="http://localhost/careerdishanepal/behind-the-scene/">Behind the scene</a>
          </li>
        </ul>
      </li>
      <li id="menu-item-116" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-116">
        <a href="http://localhost/careerdishanepal/about-us/">About Us</a>
        <ul class="sub-menu">
          <li id="menu-item-128" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-128">
            <a href="http://localhost/careerdishanepal/goals-and-objectives/">Goals and Objectives</a>
          </li>
          <li id="menu-item-130" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-130">
            <a href="http://localhost/careerdishanepal/social-need-assessment-study/">Social Need Assessment Study</a>
          </li>
        </ul>
      </li>
      <li id="menu-item-117" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-117">
        <a href="http://localhost/careerdishanepal/contact/">Contact</a>
      </li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;