2017-01-05 36 views
0

我試圖實現一個CSS下拉菜單,基於這個w3schools example。它正在工作,但dropbtn類的左邊距與簡單導航鏈接(Home,News)的左邊距不同。當你從一個列表項移動到另一個列表項時,實現下拉菜單的項目左邊有一個黑色邊框,我無法解釋。CSS鄰接兄弟分頁下拉菜單保證金

當我嘗試在使用li + li的項目之間實現分隔符時,此行爲開始:before as shown here

default.html中

<!DOCTYPE HTML> 
<html> 
<head> 
    <link rel="stylesheet" type="text/css" href="styles/style.css" /> 
</head> 
<body> 
    <ul> 
     <li><a href="#home">Home</a></li> 
     <li><a href="#news">News</a></li> 
     <li class="dropdown"> 
      <a href="javascript:void(0)" class="dropbtn">Dropdown</a> 
      <div class="dropdown-content"> 
       <a href="#">Link 1</a> 
       <a href="#">Link 2</a> 
       <a href="#">Link 3</a> 
      </div> 
     </li> 
    </ul> 
    <h3>Dropdown Menu inside a Navigation Bar</h3> 
    <p>Hover over the "Dropdown" link to see the dropdown menu.</p> 
</body> 
</html> 

的style.css

ul { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
    background-color: #333; 
} 

li { 
    float: left; 
} 

li+li:before { 
    color: white; 
    content: "|"; 
} 

li a, .dropbtn { 
    display: inline-block; 
    color: white; 
    text-align: center; 
    padding: 14px 16px; 
    text-decoration: none; 
} 

li a:hover, .dropdown:hover .dropbtn { 
    background-color: red; 
} 

li.dropdown { 
    display: inline-block; 
} 

.dropdown-content { 
    display: none; 
    position: absolute; 
    background-color: #f9f9f9; 
    min-width: 160px; 
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); 
} 

.dropdown-content a { 
    color: black; 
    padding: 12px 16px; 
    text-decoration: none; 
    display: block; 
    text-align: left; 
} 

.dropdown-content a:hover {background-color: #f1f1f1} 

.dropdown:hover .dropdown-content { 
    display: block; 
} 
+0

它對我來說似乎很好...這條黑線究竟在哪裏? –

+0

@SergChernata在懸停時,Home和News列表項目的背景從黑色變爲紅色。下拉列表項的背景也變爲紅色,但在左邊空白處有一個小區域,可能是5px寬的黑色區域。經過考慮,我猜測|字符比看起來更寬,我可以用負邊距來解決這個問題嗎? –

回答

2

你可以的立場,即before元多一點正是:

li { 
    float: left; 
    position: relative; 
} 

li+li:before { 
    color: white; 
    content: "|"; 
    left: -1px; 
    top: 14px; 
    position: absolute; 
} 

https://jsfiddle.net/oao3yt8w/

+0

您的解決方案似乎已修復了保證金,但下拉菜單不再有效? –

+0

更新了小提琴。必須擺脫溢出:隱藏在你的UL上,並增加它的高度。 https://jsfiddle.net/oao3yt8w/1/ –