开发者

CSS: UL Navigation - on hover, background color error

开发者 https://www.devze.com 2023-03-09 02:24 出处:网络
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...

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;
paddi开发者_开发技巧ng: 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;
}


I removed the unneeded/double definitions from your stylesheet and fixed the bug.

* {
    margin: 0;
    padding: 0
}
#menuTop {
    font: 12pt 'Trebuchet MS', Helvetica, sans-serif;
    padding-top: 18px;
    height: 55px;
    background: #000
}
#menuTop ul {
    padding-top: 4px;
    list-style: none
}
#menuTop li {
    background: #3C87D1;
    border: 1px solid #2A5E92;
    text-align: center
}
#menuTop a {
    display: block;
    width: 197px;
    line-height: 30px
}
#menuTop a:link, #menuTop a:visited {
    color: #fff;
    text-decoration: none
}
#menuTop a:hover {
    background-color: #5F9FFF
}
ul.menuHoriz li {
    float: left
}

Here are some notes:

  • It is handy to set the margin and padding for all elements to zero by using *, before designing your layout.
  • Set the width and height for your menu item to the a element only (the most nested element). The surrounding li element will take the same size. Also use line-height instead of height because it automatically makes your text vertically centered.
  • Don't redefine styles in :link, :visited, :hover or :active (for example dimensions and font). It gives unnecessary calculations for the browser.


If your problem is only the color coming outside the boxes, add this to your #menuTop li

overflow:hidden;

Works for me. :)


Try this one: jsfiddle example

#menuTop {
    clear: both;
    padding-top: 18px;
    height: 55px;
    font-size: 12pt;
    background-color: #000;
    text-align: center;
    font-family: 'Trebuchet MS', Helvetica, sans-serif;
    line-height: 34px;    
}
#menuTop a {
    float: left;
    border: 1px solid #2A5E92;
    background-color: #3C87D1;
    width: 197px;
    color: #fff;
    text-decoration: none;
}
#menuTop a:hover {
    background-color: #5F9FFF;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号