下拉菜单背景颜色

时间:2016-11-27 13:10:52

标签: html css

我正在尝试将下拉列表的 background-color 填充为红色,但我的编码无效。以下是我目前使用的编码。

HTML

<div class="divMenu">
    <ul class="menu">
        <li><a href="index.html">HOME</a></li>
        <li>
            <a href="brands.html">BRANDS</a>
            <ul>
                <li><a>Brand 1</a></li>
                <li><a>Brand 2</a></li>
                <li><a>Brand 3</a></li>
            </ul>
        </li>
        <li>
            <a href="categories.html">CATEGORIES</a>
            <ul>
                <li><a>Category 1</a></li>
                <li><a>Category 2</a></li>
                <li><a>Category 3</a></li>
            </ul>
        </li>
        <li><a href="new_arrivals.html">NEW ARRIVALS</a></li>
        <li><a href="contact.html">CONTACT</a></li>
    </ul>
</div>

CSS

.divMenu ul.menu > li {list-style: none; color: #000; font-weight: bold; float: left; padding: 5px 25px;}
.divMenu ul.menu > li a {text-decoration: none; color: #000;}
.divMenu ul.menu > li:hover {background-color: #f44336; cursor: pointer;}
.divMenu ul.menu > li a:hover {background-color: #f44336; color: #fff!important; cursor: pointer;}
.divMenu ul.menu ul {display: none; list-style-type: none; background-color: #f44336;}
.divMenu ul.menu > li:hover ul {display: block; padding: 0; margin: 0; z-index: 10; position: absolute;}

结果,请参考以下图片:

enter image description here

2 个答案:

答案 0 :(得分:1)

.divMenu ul.menu > li {list-style: none; color: #000; font-weight: bold; float: left; position: relative;}
.divMenu ul.menu > li a {text-decoration: none; color: #000;padding: 5px 25px;display: block;}
.divMenu ul.menu > li:hover {background-color: #f44336; cursor: pointer;}
.divMenu ul.menu > li a:hover {background-color: #f44336; color: #fff!important; cursor: pointer;}
.divMenu ul.menu ul {display: none; list-style-type: none; background-color: #f44336;padding: 0;width: 100%;}
.divMenu ul.menu > li:hover ul {display: block; padding: 0; margin: 0; z-index: 10; position: absolute;}
<div class="divMenu">
<ul class="menu">
    <li><a href="index.html">HOME</a></li>
    <li>
        <a href="brands.html">BRANDS</a>
        <ul>
            <li><a>Brand 1</a></li>
            <li><a>Brand 2</a></li>
            <li><a>Brand 3</a></li>
        </ul>
    </li>
    <li>
        <a href="categories.html">CATEGORIES</a>
        <ul>
            <li><a>Category 1</a></li>
            <li><a>Category 2</a></li>
            <li><a>Category 3</a></li>
        </ul>
    </li>
    <li><a href="new_arrivals.html">NEW ARRIVALS</a></li>
    <li><a href="contact.html">CONTACT</a></li>
</ul>
</div>

答案 1 :(得分:0)

.divMenu ul.menu > li {list-style: none; color: #000; font-weight: bold; float: left;  }
.divMenu ul.menu > li a { padding:  15px; display: block; }
.divMenu ul.menu > li a {text-decoration: none; color: #000;}
.divMenu ul.menu > li:hover {background-color: #f44336; cursor: pointer;}
.divMenu ul.menu > li:hover {background-color: #f44336; color: #fff!important; cursor: pointer;}
.divMenu ul.menu ul {display: none; list-style-type: none; background-color: #f44336;}
.divMenu ul.menu > li:hover ul {display: block;  z-index: 10; }

.divMenu ul.menu > li a:hover {color: #fff!important;}

.divMenu ul.menu > li {
    display:    block;
}

.divMenu ul.menu > li ul {
    padding:            0px;
    margin:             0px;
    margin-top:         0px;
}

.divMenu ul.menu > li li {
    display:    block;
    padding:    0px;
    width:      100%;
    margin:     0px;
}