簡體   English   中英

如何使水平導航欄上的嵌套子菜單垂直?

[英]How to make a nested sub menu on horizontal navigation bar vertical?

因此,我創建了一個水平導航欄,但我希望將子菜單懸停在上方時垂直顯示,但無論如何,它仍會水平顯示。

這是我的代碼的JSFiddle: https ://jsfiddle.net/ma85nbgx/

下面是我的HTML和CSS。

HTML

<div class="nav"> <!-- Start of Nav Bar -->
    <ul>
        <li class="home"><a href="#">HOME</a></li>
        <li class="aboutus"><a href="#">ABOUT&nbsp;US</a></li>
        <li class="services"><a href="#">SERVICES</a>
            <ul>
                <li class="programs"><a href="#">PROGRAMS</a></li>
                <li class="events"><a href="#">EVENTS</a></li>
            </ul>
        </li>
        <li class="resources"><a href="#">RESOURCES</a></li>
        <li class="getinvolved"><a href="#">GET&nbsp;INVOLVED</a></li>
        <li class="contactus"><a href="#">CONTACT&nbsp;US</a></li>
    </ul>
</div>

CSS

.nav ul {
  list-style: none;
  text-align: center;
  padding: 0;
  margin: 0;
}

.nav ul li {
    font-family: 'Roboto', sans-serif;
    border-bottom: none;
    height: 86px;
    line-height: 86px;
    font-size: 14px;
    display: inline-block;
    float:left;
    margin: 0 auto;
}

.nav ul li a {
  text-decoration: none;
  color:#000000;
  display: block;
  transition: .3s background-color;
  padding:0 24px;
}

.nav ul li a:hover {
  background-color: #5c89c7;
  color:#FFFFFF;
}

.nav a {
    border-bottom:none;
}

.nav li ul {
    position:absolute;
    display:none;
    width:inherit;
}

.nav li:hover ul {
    display:block;
}

.nav ul li ul li {
  display: block;
}

我經過的大多數網站或答案都說將display:block放在嵌套的子菜單上,但是我嘗試了一下,但仍然水平顯示,任何幫助將不勝感激!

嘗試這樣的事情。 添加以下CSS

.dropdown li{
  float: none !important;
}

像這樣在子菜單UL添加.dropdown類。

<ul class="dropdown">

我已經更改了您的代碼一點點嘗試此答案,我已經刪除了嵌套的li float:left ,請檢查CSS的下面我添加了新行

 .nav ul { list-style: none; text-align: center; padding: 0; margin: 0; } .nav ul li { font-family: 'Roboto', sans-serif; border-bottom: none; height: 86px; line-height: 86px; font-size: 14px; display: inline-block; float:left; margin: 0 auto; } .nav ul li a { text-decoration: none; color:#000000; display: block; transition: .3s background-color; padding:0 24px; } .nav ul li a:hover { background-color: #5c89c7; color:#FFFFFF; } .nav a { border-bottom:none; } .nav li ul { position:absolute; display:none; width:inherit; text-align:left; } .nav li:hover ul { display:block; } .nav ul li ul li { display: block; float:none !important; /* newly added */ height:auto; /* newly added */ line-height:34px; /* newly added */ } 
 <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <div class="nav"> <!-- Start of Nav Bar --> <ul> <li class="home"><a href="#">HOME</a></li> <li class="aboutus"><a href="#">ABOUT&nbsp;US</a></li> <li class="services"><a href="#">SERVICES</a> <ul> <li class="programs"><a href="#">PROGRAMS</a></li> <li class="events"><a href="#">EVENTS</a></li> </ul> </li> <li class="resources"><a href="#">RESOURCES</a></li> <li class="getinvolved"><a href="#">GET&nbsp;INVOLVED</a></li> <li class="contactus"><a href="#">CONTACT&nbsp;US</a></li> </ul> </div> </body> </html> 

將display flex和flex-direction放在子菜單本身的包裝上。

看一下這個

 nav ul { list-style: none; text-align: center; padding: 0; margin: 0; width:100%; display:flex; flex-direction:row; flex-wrap:wrap; } nav ul li { font-family: 'Roboto', sans-serif; border-bottom: none; height: 86px; line-height: 86px; font-size: 14px; margin: 0 auto; position:relative; } nav ul li a { text-decoration: none; color:#000000; display: block; transition: .3s background-color; padding:0 24px; } nav ul li a:hover { background-color: #5c89c7; color:#FFFFFF; } nav a { border-bottom:none; } nav .withSubMenu ul{ display: none; } .withSubMenu:hover ul { display:flex; width:100%; flex-direction:column; } 
 <nav> <!-- Start of Nav Bar --> <ul> <li class="home"><a href="#">HOME</a></li> <li class="aboutus"><a href="#">ABOUT&nbsp;US</a></li> <li class="services withSubMenu"><a href="#">SERVICES</a> <ul> <li class="programs"><a href="#">PROGRAMS</a></li> <li class="events"><a href="#">EVENTS</a></li> </ul> </li> <li class="resources"><a href="#">RESOURCES</a></li> <li class="getinvolved"><a href="#">GET&nbsp;INVOLVED</a></li> <li class="contactus"><a href="#">CONTACT&nbsp;US</a></li> </ul> </nav> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM