CSS:UL 导航 - 悬停时,背景颜色错误
悬停时,背景颜色似乎超出了我的导航项目的“框”。我尝试过调整一切。这是我的 CSS 和 HTML...
<div id="menuTop">
<ul id="menuOne" class="menuHoriz">
<li><a href="index.html">home</a></li>
<li><a href="communication.html">about us</a></li>
<li><a href="about.html">services</a></li>
<li><a href="help.html">samples</a></li>
<li><a href="contact.html">contact</a></li>
</ul>
</div>
#menuTop {
clear: both;
padding-top: 18px;
height: 55px;
font-size: 12pt;
background-color: #000;
}
#menuTop ul, #menuTop li {
margin: 0;
padding: 4px 0 0 0;
}
#menuTop ul {
list-style-type: none;
}
#menuTop li {
display: block;
background-color: #3C87D1;
text-align: center;
width: 197px;
height: 30px;
margin: 0 0px 0 0;
padding: 4px 0 0 0;
border: 1px solid #2A5E92;
}
#menuTop a {
display: block;
margin: 0;
padding: 4px 0 0 0;
}
#menuTop a:link, #menuTop a:visited {
width: 197px;
height: 30px;
padding: 4px 0 0 0;
margin: 0;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
color: #fff;
text-decoration: none;
}
#menuTop a:hover {
width: 197px;
height: 30px;
padding: 4px 0 0 0;
margin: 0;
color: #fff;
background-color: #5F9FFF;
}
ul.menuHoriz li {
float: left;
}
On hover the background color seems to come outside of the "boxes" that are my navigation items. I have tried tweaking everything. Here is my CSS and HTML...
<div id="menuTop">
<ul id="menuOne" class="menuHoriz">
<li><a href="index.html">home</a></li>
<li><a href="communication.html">about us</a></li>
<li><a href="about.html">services</a></li>
<li><a href="help.html">samples</a></li>
<li><a href="contact.html">contact</a></li>
</ul>
</div>
#menuTop {
clear: both;
padding-top: 18px;
height: 55px;
font-size: 12pt;
background-color: #000;
}
#menuTop ul, #menuTop li {
margin: 0;
padding: 4px 0 0 0;
}
#menuTop ul {
list-style-type: none;
}
#menuTop li {
display: block;
background-color: #3C87D1;
text-align: center;
width: 197px;
height: 30px;
margin: 0 0px 0 0;
padding: 4px 0 0 0;
border: 1px solid #2A5E92;
}
#menuTop a {
display: block;
margin: 0;
padding: 4px 0 0 0;
}
#menuTop a:link, #menuTop a:visited {
width: 197px;
height: 30px;
padding: 4px 0 0 0;
margin: 0;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
color: #fff;
text-decoration: none;
}
#menuTop a:hover {
width: 197px;
height: 30px;
padding: 4px 0 0 0;
margin: 0;
color: #fff;
background-color: #5F9FFF;
}
ul.menuHoriz li {
float: left;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我从样式表中删除了不需要的/双重定义并修复了错误。
以下是一些注意事项:
*
将所有元素的边距和填充设置为零会很方便。a
元素(最嵌套的元素)。周围的 li 元素将采用相同的大小。还可以使用line-height
而不是height
,因为它会自动使文本垂直居中。:link
、:visited
、:hover
或:active
中重新定义样式(例如尺寸和字体)。它为浏览器提供了不必要的计算。I removed the unneeded/double definitions from your stylesheet and fixed the bug.
Here are some notes:
*
, before designing your layout.a
element only (the most nested element). The surroundingli
element will take the same size. Also useline-height
instead ofheight
because it automatically makes your text vertically centered.:link
,:visited
,:hover
or:active
(for example dimensions and font). It gives unnecessary calculations for the browser.如果您的问题只是框外的颜色,请将其添加到您的 #menuTop li
Works for me 中。 :)
If your problem is only the color coming outside the boxes, add this to your #menuTop li
Works for me. :)
试试这个:jsfiddle 示例
Try this one: jsfiddle example