【问题标题】:Drop Down Menu bugs下拉菜单错误
【发布时间】:2016-01-01 22:54:09
【问题描述】:

我正在尝试做一个下拉菜单,但遇到了两个问题。

    1234563 '
  1. 在实际的下拉菜单中,当我将鼠标悬停在链接上时,不会选择被占用的整个文本部分。

我试图修复它,但只能突出显示该区域的正确部分。

我尝试添加填充/边距,但效果不佳。

这是我的 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/

【问题讨论】:

    标签: html css drop-down-menu


    【解决方案1】:

    以下是问题的原因:

    1. 您需要将填充添加到链接本身而不是li 元素。
    2. 与上述相同的问题,另外您需要将width:100% 添加到 链接,以便它们可以占据整个宽度。

    另一个问题: 您没有为 li 添加 &lt;a&gt; 元素,其中包含下拉菜单。您应该像其他链接一样添加链接。

    DEMO

    【讨论】:

      【解决方案2】:

      为链接添加填充:

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

      另外,同一级别的&lt;li&gt;元素需要有&lt;a&gt;标签才能工作,如下所示:

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

      在这部分代码中,将宽度更改为 140 像素(160 像素减去两边的 10 像素填充):

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

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

      【讨论】:

        【解决方案3】:

        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;
        	-webkit-transiton: opacity 0.2s;
        	-moz-transition: opacity 0.2s;
        	-ms-transition: opacity 0.2s;
        	-o-transition: opacity 0.2s;
        	-transition: opacity 0.2s;
        	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: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
                	<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>
                <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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-21
          • 2021-07-03
          相关资源
          最近更新 更多