下拉菜单

时间:2016-04-28 09:44:47

标签: html css drop-down-menu

我现在正在编写一个关于uni的网站,除了下拉菜单问题外,它的进展相当顺利。我已经在某些菜单选项中为li属性提供了“已选择”的ID(当用户在相关页面上时),因此它将颜色更改为深粉色。

然而,在“已选择”的li id下面的下拉选项也会变成深粉红色。当我在html中的“about”末尾添加一个封闭的li标签时,下拉列表根本不会出现。

我会发布相关的html和CSS以及截图,希望有人可以帮助我!

以下是所有下拉代码:

/*----- Menu Outline -----*/
.menu-wrap {
    width:100%;
    box-shadow:0px 1px 3px rgba(0,0,0,0.2);
    background:#ffeff8;
}
 
.menu {
    width:1000px;
    margin:0px auto;
}
 
.menu li {
    margin:0px;
    list-style:none;
    font-family:"Luna";
    text-transform: lowercase;
    border:1px solid #ed85c4; 
}
 
.menu a {
    transition:all linear 0.15s;
    color:#ed85c4;
    background:#ffeff8;
}
 
.menu li:hover > a, .menu .current-item > a {
    text-decoration:none;
    color:#d771ae;
}
 
.menu .arrow {
    font-size:10px;
    line-height:0%;
}
 
/*----- Top Level -----*/
.menu > ul > li {
    float:left;
    display:inline-block;
    position:relative;
    font-size:14px;
}
 
.menu > ul > li > a {
    padding:3px 40px;
    display:inline-block;
    text-shadow:0px 0px 0px rgba(0,0,0,0.4);
}
 
.menu > ul > li:hover > a, .menu > ul > a {
    background:#f1dae8;
}

.menu > ul > li#selected a{
    color: #ffeff8;
    background: #c864a1;
}

 
/*----- Bottom Level -----*/
.menu li:hover .sub-menu {
    z-index:1;
    opacity:1;
}
 
.sub-menu {
    width:100%;
    padding:0px 0px;
    position:absolute;
    top:100%;
    left:0px;
    z-index:-1;
    opacity:0;
    transition:opacity linear 0.15s;
    box-shadow:0px 0px 0px rgba(0,0,0,0.2);
}
 
.sub-menu li {
    display:block;
    font-size:12px;
    text-transform: lowercase;
    font-kerning: auto;
}
 
.sub-menu li a {
    padding:10px 30px;
    display:block;
    background: #ffeff8;
}
 
.sub-menu li a:hover, .sub-menu a {
    background: #f1dae8;
}
<div class="menu-wrap">
    <nav class="menu">
        <ul class="clearfix">
            <li id="selected">
                <a href="/about-us.html">about <span class="arrow">&#9660;</span></a>
 
                  <ul class="sub-menu">
                    <li><a href="/history.html">history</a></li>
                    <li><a href="/values.html">values</a></li>
                    <li><a href="/truck.html">the truck</a></li>
                    <li><a href="/produce.html">produce info.</a></li>
                </ul>
            </li>

2 个答案:

答案 0 :(得分:1)

您未正确选择#selected列表项下方的链接。

.menu > ul > li#selected a /*This selects all links below the selected li, including links in child list items*/

应该是

.menu > ul > li#selected > a /* this selects the immediate descendent link only */

答案 1 :(得分:1)

&#13;
&#13;
/*----- Menu Outline -----*/
.menu-wrap {
    width:100%;
    box-shadow:0px 1px 3px rgba(0,0,0,0.2);
    background:#ffeff8;
}
 
.menu {
    width:1000px;
    margin:0px auto;
}
 
.menu li {
    margin:0px;
    list-style:none;
    font-family:"Luna";
    text-transform: lowercase;
    border:1px solid #ed85c4; 
}
 
.menu a {
    transition:all linear 0.15s;
    color:#ed85c4;
    background:#ffeff8;
}
 
.menu li:hover > a, .menu .current-item > a {
    text-decoration:none;
    color:#d771ae;
}
 
.menu .arrow {
    font-size:10px;
    line-height:0%;
}
 
/*----- Top Level -----*/
.menu > ul > li {
    float:left;
    display:inline-block;
    position:relative;
    font-size:14px;
}
 
.menu > ul > li > a {
    padding:3px 40px;
    display:inline-block;
    text-shadow:0px 0px 0px rgba(0,0,0,0.4);
}
 
.menu > ul > li:hover > a, .menu > ul > a {
    background:#f1dae8;
}

.menu > ul > li#selected >a{
    color: #ffeff8;
    background: #c864a1;
}

 
/*----- Bottom Level -----*/
.menu li:hover .sub-menu {
    z-index:1;
    opacity:1;
}
 
.sub-menu {
    width:100%;
    padding:0px 0px;
    position:absolute;
    top:100%;
    left:0px;
    z-index:-1;
    opacity:0;
    transition:opacity linear 0.15s;
    box-shadow:0px 0px 0px rgba(0,0,0,0.2);
}
 
.sub-menu li {
    display:block;
    font-size:12px;
    text-transform: lowercase;
    font-kerning: auto;
}
 
.sub-menu li a {
    padding:10px 30px;
    display:block;
    background: #ffeff8;
}
 
.sub-menu li a:hover, .sub-menu a {
    background: #f1dae8;
}
&#13;
<div class="menu-wrap">
    <nav class="menu">
        <ul class="clearfix">
            <li id="selected">
                <a href="/about-us.html">about <span class="arrow">&#9660;</span></a>
 
                  <ul class="sub-menu">
                    <li><a href="/history.html">history</a></li>
                    <li><a href="/values.html">values</a></li>
                    <li><a href="/truck.html">the truck</a></li>
                    <li><a href="/produce.html">produce info.</a></li>
                </ul>
            </li>
&#13;
&#13;
&#13;