2016-08-05 165 views
0

我只想在我的下拉菜單中創建子菜單...但我找不到解決此問題的代碼。如何在下拉菜單中創建子菜單(HTML/CSS)

HTML代碼:

<nav id='menu'> 
    <ul> 
    <li><a class='home' href='/'>Home</a></li> 
    <li><a class='prett' href='#' title='Menu'>Menu</a> 
     <ul class='menus'> 
     <li><a class='prett' href='Dropdown 1' title='Dropdown 1'>Dropdown 1 + Sub Menu</a> 
      <ul class='submenu'> 
      <li><a href="#" title="Sub Menu">Sub Menu</a></li> 
      </ul> 
     </li> 
     <li><a href='#' title='Dropdown 2'>Dropdown 2</a></li> 
     <li><a href='#' title='Dropdown 3'>Dropdown 3</a></li> 
     </ul> 
    </li> 
    </ul> 
</nav> 

CSS代碼:

#menu{background:#343434;color:#eee;height:35px;border-bottom:4px solid #eeeded} 
#menu ul,#menu li{margin:0 0;padding:0 0;list-style:none} 
#menu ul{height:35px} 
#menu li{float:left;display:inline;position:relative;font:bold 12px Arial;text-shadow: 0 -1px 0 #000;border-right: 1px solid #444;border-left: 1px solid #111;text-transform:uppercase} 
#menu li:first-child{border-left: none} 
#menu a{display:block;line-height:35px;padding:0 14px;text-decoration:none;color:#eee;} 
#menu li:hover > a,#menu li a:hover{background:#111} 
#menu input{display:none;margin:0 0;padding:0 0;width:80px;height:35px;opacity:0;cursor:pointer} 
#menu label{font:bold 30px Arial;display:none;width:35px;height:36px;line-height:36px;text-align:center} 
#menu label span{font-size:12px;position:absolute;left:35px} 
#menu ul.menus{height:auto;overflow:hidden;width:180px;background:#111;position:absolute;z-index:99;display:none;border:0;} 
#menu ul.menus li{display:block;width:100%;font:12px Arial;text-transform:none;} 
#menu li:hover ul.menus{display:block} 
#menu a.home {background: #c00;} 
#menu a.prett{padding:0 27px 0 14px} 
#menu a.prett::after{content:"";width:0;height:0;border-width:6px 5px;border-style:solid;border-color:#eee transparent transparent transparent;position:absolute;top:15px;right:9px} 
#menu ul.menus a:hover{background:#333;} 

謝謝,這個網站將真正幫助了我這麼多

+0

你能你的代碼粘貼到一個CodePen或東西,所以我們可以看看它?你要求我們瀏覽大量的代碼,不管你在CSS上表現如何好,你都不知道它會在Web瀏覽器中呈現出來之前如何結果。 – RobertAKARobin

+0

你需要搜索你自己吧,看到這裏https://codepen.io/philhoyt/pen/ujHzd –

+0

對不起林新手在這裏 http://codepen.io/aziu/pen/NAOawJ 此,請大家幫忙我... 感謝 – aziu

回答

2

我不太清楚什麼樣的的結果是你期待的,但有一種方法可以做到這一點,就是向子菜單裏添加一個類並觀察它的時間徘徊,然後顯示子菜單。就像這樣:

#menu { 
 
    background: #343434; 
 
    color: #eee; 
 
    height: 35px; 
 
    border-bottom: 4px solid #eeeded 
 
} 
 

 
#menu ul, 
 
#menu li { 
 
    margin: 0 0; 
 
    padding: 0 0; 
 
    list-style: none 
 
} 
 

 
#menu ul { 
 
    height: 35px 
 
} 
 

 
#menu li { 
 
    float: left; 
 
    display: inline; 
 
    position: relative; 
 
    font: bold 12px Arial; 
 
    text-shadow: 0 -1px 0 #000; 
 
    border-right: 1px solid #444; 
 
    border-left: 1px solid #111; 
 
    text-transform: uppercase 
 
} 
 

 
#menu li:first-child { 
 
    border-left: none 
 
} 
 

 
#menu a { 
 
    display: block; 
 
    line-height: 35px; 
 
    padding: 0 14px; 
 
    text-decoration: none; 
 
    color: #eee; 
 
} 
 

 
#menu li:hover > a, 
 
#menu li a:hover { 
 
    background: #111 
 
} 
 

 
#menu input { 
 
    display: none; 
 
    margin: 0 0; 
 
    padding: 0 0; 
 
    width: 80px; 
 
    height: 35px; 
 
    opacity: 0; 
 
    cursor: pointer 
 
} 
 

 
#menu label { 
 
    font: bold 30px Arial; 
 
    display: none; 
 
    width: 35px; 
 
    height: 36px; 
 
    line-height: 36px; 
 
    text-align: center 
 
} 
 

 
#menu label span { 
 
    font-size: 12px; 
 
    position: absolute; 
 
    left: 35px 
 
} 
 

 
#menu ul.menus { 
 
    height: auto; 
 
    width: 180px; 
 
    background: #111; 
 
    position: absolute; 
 
    z-index: 99; 
 
    display: none; 
 
    border: 0; 
 
} 
 

 
#menu ul.menus li { 
 
    display: block; 
 
    width: 100%; 
 
    font: 12px Arial; 
 
    text-transform: none; 
 
} 
 

 
#menu li:hover ul.menus { 
 
    display: block 
 
} 
 

 
#menu a.home { 
 
    background: #c00; 
 
} 
 

 
#menu a.prett { 
 
    padding: 0 27px 0 14px 
 
} 
 

 
#menu a.prett::after { 
 
    content: ""; 
 
    width: 0; 
 
    height: 0; 
 
    border-width: 6px 5px; 
 
    border-style: solid; 
 
    border-color: #eee transparent transparent transparent; 
 
    position: absolute; 
 
    top: 15px; 
 
    right: 9px 
 
} 
 

 
#menu ul.menus a:hover { 
 
    background: #333; 
 
} 
 

 
#menu ul.menus .submenu { 
 
    display: none; 
 
    position: absolute; 
 
    left: 180px; 
 
    background: #111; 
 
    top: 0; 
 
    width: 180px; 
 
} 
 

 
#menu ul.menus .submenu li { 
 
    background: #111; 
 
} 
 

 
#menu ul.menus .has-submenu:hover .submenu { 
 
    display: block; 
 
}
<nav> 
 
    <ul id='menu'> 
 
    <li><a class='home' href='/'>Home</a></li> 
 
    <li><a class='prett' href='#' title='Menu'>Menu</a> 
 
     <ul class='menus'> 
 
     <li class='has-submenu'><a class='prett' href='Dropdown 1' title='Dropdown 1'>Dropdown 1 + Sub Menu</a> 
 
      <ul class='submenu'> 
 
      <li><a href="#" title="Sub Menu">Sub Menu</a></li> 
 
      <li><a href="#" title="Sub Menu">Sub Menu 2</a></li> 
 
      <li><a href="#" title="Sub Menu">Sub Menu 3</a></li> 
 
      </ul> 
 
     </li> 
 
     <li><a href='#' title='Dropdown 2'>Dropdown 2</a></li> 
 
     <li><a href='#' title='Dropdown 3'>Dropdown 3</a></li> 
 
     </ul> 
 
    </li> 
 
    </ul> 
 
</nav>

編輯:

使用JavaScript和點擊打開:https://jsfiddle.net/thepio/pn0ym10e/2/

+0

這就是忍不住,我要上的小道子菜單... 感謝您的幫助 – aziu

+0

謝謝你,你是站長:d 你能命令如果不點擊不開,如果單擊打開 坦克如此Muchhhh! – aziu

+0

但是當我添加更多的子其外觀Transparat https://2.bp.blogspot.com/-X7HqK7CVCqM/V6Q6LPQBSNI/AAAAAAAAAAo/VHj8NsHHKtg4CM3F2RiWW8gJYbBnga9MwCLcB/s320/HTML%2BCSS%2BDrop%2BDown.jpg – aziu