2015-12-31 59 views
0

我正在嘗試做一個下拉菜單,我有兩個問題。下拉菜單錯誤

  1. 當我將鼠標懸停在主導航欄(aboutconnect)鏈接,可點擊區域僅僅是的話,而不是文本所佔用的像「作品」和整個區域的成就。「

  2. 在實際的下拉菜單中,當我將鼠標懸停在鏈接上時,並未選中佔用的整個文本部分。

我試圖解決它,但只是要突出顯示該區域的正確部分。

我試圖添加填充/邊距,但它沒有完全工作。

這是我的CSS:

body { 
    background-color: #FFFFF5; 
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
} 
#nav { 
    width: 480px; 
    float: right; 
    height: 30px; 
    border-top: thin solid #000000; 
    line-height: 0px; 
    text-align: right; 
    padding-right: 20px; 
} 
.navText { 
    color: #000000; 
    text-decoration: none; 
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
    font-size: 17px; 
} 
#nav ul { 
    text-align: left; 
    display: inline; 
    list-style: none; 
    padding-top: 0px; 
    padding-right: 0px; 
    padding-bottom: 0px; 
    padding-left: 0px; 
} 
#nav ul li { 
    font: 17px/17px sans-serif; 
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
    display: inline-block; 
    position: relative; 
    cursor: pointer; 
    padding-top: 7px; 
    padding-bottom: 7px; 
    padding-left: 10px; 
    padding-right: 10px; 
} 
#nav ul li a:hover { 
    background: #555; 
    color: #fffff5; 
    display: inline-block; 
    position: relative; 
    cursor: pointer; 
} 
#nav ul li:hover { 
    background: #555; 
    color: #fffff5; 
} 
#nav ul li ul { 
    position: absolute; 
    top: 30px; 
    left: 0px; 
    opacity: 0; 
    visibility: hidden; 
    width: 160px; 
} 
#nav ul li:hover ul { 
    display: block; 
    opacity: 1; 
    visibility: visible; 
} 
#nav ul li ul li { 
    background: #555; 
    display: block; 
    color: #fffff5; 
    font: 13px sans-serif; 
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
} 
#nav ul li ul li a { 
    text-decoration: none; 
    background: #555; 
    display: block; 
    color: #fffff5; 
    font: 13px sans-serif; 
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
} 
#nav ul li ul li a:hover { 
    background: #666; 
    width: 150px; 
} 

這是我的HTML:

<div id="nav"> 
    <ul> 
    <li><a href="about.html" class="navText">ABOUT</a></li> 
    <li>WORKS 
     <ul> 
     <li><a href="written.html">Written</a></li> 
     <li><a href="photos.html">Photography</a></li> 
     <li><a href="film.html">Film</a></li> 
     <li><a href="other.html">Other</a></li> 
     </ul> 
    </li> 
    <li>ACHIEVEMENTS 
     <ul> 
     <li><a href="skills.html">Skills</a></li> 
     <li><a href="acad.html">Academic</a></li> 
     <li><a href="cca.html">Co-Curricular</a></li> 
     </ul> 
    </li> 
    <li><a href="connect.html" class="navText">CONNECT</a></li> 
    </ul> 
</div> 

我的代碼可以在這裏預覽:https://jsfiddle.net/zinctan/83jh28o4/1/

回答

0

下面分別是針對問題的原因:

  1. 您需要填充添加到鏈接本身,而不是li 元素。
  2. 與上面相同的問題,再加上您需要將width:100%添加到 鏈接,以便它們可以佔據整個寬度。

另一個問題: 您還沒有添加<a>元素具有裏面下拉列表中li的。你應該添加鏈接作爲其他的。

DEMO

0

body { 
 
\t background-color: #FFFFF5; 
 
\t font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
} 
 
#nav { 
 
\t width: 480px; 
 
\t float: right; 
 
\t height: 30px; 
 
\t border-top: thin solid #000000; 
 
\t line-height: 0px; 
 
\t text-align: right; 
 
\t padding-right: 20px; 
 
} 
 
.navText { 
 
\t color: #000000; 
 
\t text-decoration: none; 
 
\t font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
\t font-size: 17px; 
 
} 
 
#nav ul { 
 
\t text-align: left; 
 
\t display: inline; 
 
\t list-style: none; 
 
\t padding-top: 0px; 
 
\t padding-right: 0px; 
 
\t padding-bottom: 0px; 
 
\t padding-left: 0px; 
 
} 
 
#nav ul li { 
 
\t font: 17px/17px sans-serif; 
 
\t font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
\t display: inline-block; 
 
\t position: relative; 
 
\t cursor: pointer; 
 
\t padding-top: 7px; 
 
\t padding-bottom: 7px; 
 
\t padding-left: 10px; 
 
\t padding-right: 10px; 
 
} 
 
#nav ul li a:hover { 
 
\t background: #555; 
 
\t color: #fffff5; 
 
\t display: inline-block; 
 
\t position: relative; 
 
\t cursor: pointer; 
 
} 
 
#nav ul li:hover { 
 
\t background: #555; 
 
\t color: #fffff5; 
 
} 
 
#nav ul li ul { 
 
\t position: absolute; 
 
\t top: 30px; 
 
\t left: 0px; 
 
\t opacity: 0; 
 
\t visibility: hidden; 
 
\t -webkit-transiton: opacity 0.2s; 
 
\t -moz-transition: opacity 0.2s; 
 
\t -ms-transition: opacity 0.2s; 
 
\t -o-transition: opacity 0.2s; 
 
\t -transition: opacity 0.2s; 
 
\t width: 160px; 
 
} 
 
#nav ul li:hover ul { 
 
\t display: block; 
 
\t opacity: 1; 
 
\t visibility: visible; 
 
} 
 
#nav ul li ul li { 
 
\t background: #555; 
 
\t display: block; 
 
\t color: #fffff5; 
 
\t font: 13px sans-serif; 
 
\t font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
} 
 
#nav ul li ul li a { 
 
\t text-decoration: none; 
 
\t background: #555; 
 
\t display: block; 
 
\t color: #fffff5; 
 
\t font: 13px sans-serif; 
 
\t font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
} 
 
#nav ul li ul li:hover { 
 
background: #666; 
 
} 
 
#nav ul li ul li:hover a { 
 
    background: #666; 
 
} 
 
</style>
 <div id="nav"> 
 
     <ul> 
 
     <a href="about.html" class="navText"><li>ABOUT</li></a> 
 
     <li>WORKS 
 
     \t <ul> 
 
     \t <li><a href="written.html">Written</a></li> 
 
     \t <li><a href="photos.html">Photography</a></li> 
 
     <li><a href="film.html">Film</a></li> 
 
     <li><a href="other.html">Other</a></li> 
 
\t \t </ul> 
 
\t  </li> 
 
     <li>ACHIEVEMENTS 
 
     <ul> 
 
     \t <li><a href="skills.html">Skills</a></li> 
 
     <li><a href="acad.html">Academic</a></li> 
 
     <li><a href="cca.html">Co-Curricular</a></li> 
 
\t \t </ul> 
 
     </li> 
 
     <a href="connect.html" class="navText"><li>CONNECT</li></a> 
 
     </ul> 
 
    </div>

1:更改

<li><a href="about.html" class="navText">ABOUT</a></li> 
… 
<li><a href="connect.html" class="navText">CONNECT</a></li> 

<a href="about.html" class="navText"><li>ABOUT</li></a> 
… 
<a href="connect.html" class="navText"><li>CONNECT</li></a> 

2:更改

CSS

#nav ul li ul li a:hover { 
    background: #666; 
    width: 150px; 
} 

#nav ul li ul li:hover { 
    background: #666; 
} 

,並添加

#nav ul li ul li:hover a { 
    background: #666; 
} 
0

添加填充到鏈接:

#nav ul li a { 
    display: block; 
    padding: 7px 10px; 
} 

此外,在同級別需要<li>要素都發揮<a>標籤的工作,就像如下:

<a href="#" class="navText">WORKS</a> 

在這部分代碼將寬度更改爲140px(160像素減去兩邊填充10像素):

#nav ul li ul li a:hover { 
    width: 140px; 
} 

https://jsfiddle.net/jonathanzuniga/6mpn8wpo/