CSS drop down menu main tab snag

2019-07-04 21:03发布

问题:

I have been building a CSS drop down menu and I have a slight snag and wondered if anyone could help me fix this. I have a menu that has this HTML

<div id="menu">
<ul>
<li><a class="active" href="#">Home</a>
<ul class="sub-menu">
<li><a href="#">This is sub</a></li>
<li><a href="#">sub menu item</a></li>
<li><a href="#">This is a long sub menu item</a></li>
</ul>
</li>  
<li><a href="#">Location</a></li>
<li><a href="#">another link</a></li>
</ul>
</div>

Currently when I hover over the main <li> 'Home' it's width expands a lot, I think to accommodate the sub-class menus that I have. How can I stop this from occurring? To achieve something like:

|Home| // Nice short main tab
|A sub menu link |
|Another sub menu link|

As opposed to this that is currently happening (forgive the crudeness of my diagram)

|Home....................| // really long messing up everything tab
|A sub menu link |
|Another sub menu link|

This is my CSS code for the menu:

#menu ul {
margin: 0;
list-style: none;
padding: 0 0 0 20px;
position: relative;
z-index: 40;
}
#menu ul li {
float: left;
padding: 10px 10px 13px 0;
position: relative;
z-index: 50;
}
#menu li a {
text-decoration: none;
font-family: Verdana, Geneva, sans-serif;

回答1:

I have basically the same markup so the following code should work well for what you need.

I left all of the color and image info in case you needed help there too.

#menu {
    background: none repeat scroll 0 0 #333333;
    border: 0 none;
    font: bold 14px "Lucida Sans Unicode","Bitstream Vera Sans","Trebuchet Unicode MS","Lucida Grande",Verdana,Helvetica,sans-serif;
    margin: 0;
    padding: 0;
    width: 100%;
}
#menu ul {
    background: none repeat scroll 0 0 #333333;
    height: 35px;
    list-style: none outside none;
    margin: 0 0 3px;
    padding: 0;
    width: 100%;
}
#menu li {
    float: left;
    padding: 0;
}
#menu li a {
    background: no-repeat scroll right bottom #333333;
    color: #CCCCCC;
    display: block;
    font-weight: normal;
    line-height: 35px;
    margin: 0;
    padding: 0 10px;
    text-align: center;
    text-decoration: none;
}
#menu li a:hover, #menu ul li:hover a {
    background: no-repeat scroll center bottom #2580A2;
    color: #FFFFFF;
    text-decoration: none;
}
#menu li ul {
    background: none repeat scroll 0 0 #333333;
    border: 0 none;
    display: none;
    height: auto;
    margin: 0;
    padding: 0;
    position: absolute;
    width: 225px;
    z-index: 200;
}
#menu li:hover ul {
    display: block;
}
#menu li li {
    display: block;
    float: none;
    margin: 0;
    padding: 0;
    width: 225px;
}
#menu li:hover li a {
    background: none repeat scroll 0 0 transparent;
}
#menu li ul a {
    display: block;
    font-size: 12px;
    font-style: normal;
    height: 35px;
    margin: 0;
    padding: 0 10px 0 15px;
    text-align: left;
}
#menu li ul a:hover, #menu li ul li:hover a {
    background: no-repeat scroll left center #2580A2;
    border: 0 none;
    color: #FFFFFF;
    text-decoration: none;
}


回答2:

The markup is weird man.

First of all, try to structure it a little better than having the <a> for the home link in the same <li> as another <ul>. Can this link be in a separate <li>?

Also, don't wrap the menu in a <div>. Just apply the styling directly to the parent <ul>.