简体   繁体   English

CSS下拉 <ul><li> 每个级别的菜单大小不同

[英]CSS drop down <ul> <li> menu different sizes of each level

Hi I cannot figure out how to change the size of the child item of the CSS menu (The parent has an image background, the child should have a background color). 嗨,我无法弄清楚如何更改CSS菜单的子项的大小(父项具有图像背景,子项应具有背景色)。 I tried something like: 我尝试了类似的东西:

ul > li > a:hover {
    display:inline-block;
    background:#000;
    float:left;
    width:120px;
    height:25px;
}

but it is all mixed with other elements. 但它们都与其他元素混合在一起。 Can you please help me figure it out? 你能帮我弄清楚吗?

HTML: HTML:

   <div id="menu">
   <ul>
    <li><a id="menu1" href="#">Parent1</a></li>

    <li><a href="#">Parent2</a>
        <ul>
            <li><a href="#">Child1</a></li>
            <li><a href="#">Child2</a></li>
            <li><a href="#">Child3</a></li>
        </ul>
    </li>
    <li><a href="#">Parent3</a></li>
    <li><a href="#">Parent4</a></li>
    <li><a href="#">Parent5</a></li>
   </ul>
   </div>

CSS: CSS:

#menu {
    position:absolute;
    top:133px;
    left:0px;
    width:900px;
    height:50px;
}
ul {
    font-family: Arial, Verdana;
    font-size: 14px;
    margin: 0;
    padding: 0;
    list-style: none;
}
ul li {
    display: block;
    float: left;
}
li ul {
    display: none;
}
ul li a {
    display: block;
    text-decoration: none;
    color: #ffffff;
    /*padding: 5px 15px 5px 15px;*/
    background-image: url(../images/menu1.png);
    white-space: nowrap;
    width:178px;
    height:50px;
}
ul li a:hover { 
    background: #BBBBBB;
}
li:hover ul {
    display: block;
    position: absolute;
    left:0px;
}
li:hover li {
    float: left;
    font-size: 11px;
}
li:hover a { 
    background: #3b3b3b; 
}
li:hover li a:hover {
    background: #CCC;
}

Thank you very much for your help in advance! 非常感谢您的提前帮助!

To add a background color to your child sub menu: 要将背景色添加到您的子菜单中:

ul li ul li {
    // rules
}

You can also add class to your list items to make it more manageable. 您还可以将类添加到列表项中,以使其更易于管理。 For example, you can add a class to the ul of your child menu: 例如,您可以将一个类添加到子菜单的ul中:

<li><a href="#">Parent2</a>
        <ul class="sub-menu">
            <li><a href="#">Child1</a></li>
            <li><a href="#">Child2</a></li>
            <li><a href="#">Child3</a></li>
        </ul>
    </li>

Then you can simplify the css to: 然后,您可以将CSS简化为:

.sub-menu li {
    display:inline-block;
    background:#000;
    float:left;
    width:120px;
    height:25px;
}

Hope following changes will work for you. 希望以下更改对您有用。 Here is the fiddle link . 这是小提琴的链接 Followings are your css chagnes 以下是您的CSS chagnes

#menu {
    /*position:absolute;
    top:0;
    left:0px;*/
    width:900px;
    height:22px;
    background-color:#000;
}
ul {
    font-family: Arial, Verdana;
    font-size: 14px;
    line-height:22px;
    margin: 0;
    padding: 0;
    list-style: none;
    text-align:center;
}
ul li {
    display: block;
    float: left;
    position:relative;
}
ul li a {
    display: block;
    text-decoration: none;
    color: #ffffff;
    /*padding: 5px 15px 5px 15px;*/
    background-image: url(../images/menu1.png);
    white-space: nowrap;
    width:178px;
    line-height:22px;
}
ul li:hover a { 
    background: #BBBBBB;
}
ul li ul {
    display: none;
    widht:200px;
    position:absolute;
    left:0;
    top:22px;
}
ul li ul li {
    float: none;
    font-size: 11px;
}
ul li ul li a {
  background: #3b3b3b;
    background-image:none!important;
}
ul li ul li a:hover {
    background: #CCC;
}
ul li:hover ul {
    display: block;
    position: absolute;
    left:0px;
}

It is good to retain the same width so that you can avoid that weird movement. 最好保持相同的宽度,以免发生奇怪的移动。

Try this 尝试这个

ul li ul li a:hover {
    display:block;
    background:#000;
    float:left;
    width:178px;
    height:25px;
}

DEMO DEMO

I don't know how you want the result to look exactly but you could also use the + selector in your css like this: 我不知道您希望结果如何看起来准确,但是您也可以在CSS中使用+选择器,如下所示:

ul > li > a:hover+ {
        display:inline-block;
        background:#000;
        float:left;
        width:120px;
        height:25px;
    }

The + selector as you see after a:hover means that the next element directly after the a:hover-tag will be selected and is this case it will be a ul 您在a:hover之后看到的+选择器意味着将选择紧接在a:hover-tag之后的下一个元素,在这种情况下,它将是ul

You can also specify the element with +ul if you want the ul to be a list and not a flat one like this: 如果您希望ul是一个列表而不是像这样的固定列表,则还可以使用+ ul来指定元素:

ul > li > a:hover+ul {
        display:inline-block;
        background:#000;
        float:left;
        width:120px;
        height:25px;
    }

Like this you know that you will only affect the element next to the hovered one 这样,您知道只会影响悬停的元素旁边的元素

Solution

ul > li:hover > li > a {
    display:inline-block;
    background:#000;
    float:left;
    width:120px;
    height:25px;

} }

It can be as per requirement 可以根据要求

ul > li:hover  li   a {  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM