2015-02-11 114 views
2

我遇到了水平菜單欄列表項重疊的問題。列表元素重疊

因此nav div元素的ul ul li重疊,我能做些什麼來阻止它?

The jsfiddle

CSS:

body, html{ 
 
padding: 0px; 
 
margin: 0px; 
 
} 
 
.nav { 
 
font-family: 'PT Sans', sans-serif; 
 
width: 100%; 
 
color: #F5F5F5; 
 
background: #1565c0; 
 
display: flex; 
 
height: 40px; 
 
padding: 0; 
 
margin: 0; 
 
border: 0; 
 
} 
 
.nav ul, .nav ul li, .nav{ 
 
list-style: none; 
 
margin: 0; 
 
padding: 0; 
 
border-bottom: 3px solid #0d47a1; 
 
} 
 
.nav ul li{ 
 
background: #1565c0; 
 
width:auto; 
 
position: relative; 
 
transition: background 250ms ease-in; 
 
line-height: 40px; 
 
display:block; 
 
} 
 
.nav ul li:hover{ 
 
background: #0d47a1; 
 
} 
 
.nav ul ul{ 
 
overflow:visible; 
 
position: absolute; 
 
visibility: hidden; 
 
top: 100%; 
 
width: 0%; 
 
} 
 
.nav ul ul li{ 
 
border:none; 
 
display:block; 
 
position: absolute; 
 
left:0; 
 
margin:0px; 
 
} 
 
.nav ul li:hover>ul{ 
 
visibility: visible; 
 
} 
 
.nav a{ 
 
text-decoration: none; 
 
color: inherit; 
 
margin: 0px 20px; 
 
} 
 
.nav ul ul li:last-child{ 
 
border-bottom: 3px solid #0d47a1; 
 
}
<div class="nav"> 
 
<ul> 
 
<li><a>Hi there</a><ul><li><a>Here is the awesomeness</a></li><li><a>Awesome</a></li></ul></li> 
 
</ul> 
 
</div>

回答

0

fiddle這裏是你的代碼做一些修改:

.nav ul ul li { 
    border: medium none; 
    display: block; 
    /*left: 0;*/ 
    margin: 0; 
    /*position: absolute;*/ 
} 
.nav ul ul { 
    overflow: visible; 
    position: absolute; 
    top: 100%; 
    visibility: hidden; 
    /*width: 0;*/ 
} 

您可以添加以下CSS不包裹在兩行文字:

.nav ul ul { 
    white-space: nowrap; 
} 
+1

偉大工程, 謝謝! – 2015-02-11 07:17:00

0

嘗試這樣Demo

CSS:

.nav ul li > ul { 
    position: relative; 
    display:none; 
} 
.nav ul ul li { 
    border:none; 
    display:block; 
    position: relative; 
    left:0; 
    margin:0px; 
} 
.nav ul li:hover>ul { 
    display:block; 
} 
0

body, html { 
 
    padding: 0px; 
 
    margin: 0px; 
 
} 
 
.nav { 
 
    font-family:'PT Sans', sans-serif; 
 
    width: 100%; 
 
    color: #F5F5F5; 
 
    background: #1565c0; 
 
    display: flex; 
 
    height: 40px; 
 
    padding: 0; 
 
    margin: 0; 
 
    border: 0; 
 
} 
 
.nav ul, .nav ul li, .nav { 
 
    list-style: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    border-bottom: 3px solid #0d47a1; 
 
} 
 
.nav ul li { 
 
    background: #1565c0; 
 
    width:auto; 
 
    position: relative; 
 
    transition: background 250ms ease-in; 
 
    line-height: 40px; 
 
    display:block; 
 
} 
 
.nav ul li:hover { 
 
    background: #0d47a1; 
 
} 
 
.nav ul li > ul { 
 
    position: relative; 
 
    display:none; 
 
} 
 
.nav ul ul li { 
 
    border:none; 
 
    display:block; 
 
    position: relative; 
 
    left:0; 
 
    margin:0px; 
 
} 
 
.nav ul li:hover>ul { 
 
    display:block; 
 
} 
 
.nav a { 
 
    text-decoration: none; 
 
    color: inherit; 
 
    margin: 0px 20px; 
 
} 
 
.nav ul ul li:last-child { 
 
    border-bottom: 3px solid #0d47a1; 
 
}
<div class="nav"> 
 
    <ul> 
 
     <li><a>Hi there</a> 
 

 
      <ul> 
 
       <li><a>Here is the awesomeness</a> 
 

 
       </li> 
 
       <li><a>Awesome</a> 
 

 
       </li> 
 
      </ul> 
 
     </li> 
 
    </ul> 
 
</div>

+0

爲什麼你在裏面再次使用ul ..可以請檢查html標籤..可能會有幫助 – 2015-02-11 07:34:44

+0

如果你仔細觀察,這是一個下拉菜單,所以它是另一個列表中的列表。 – 2015-02-11 08:19:45

+0

檢查一次,現在它的工作 – 2015-02-11 09:14:08