如何在<li>标签上为背景菜单设置相同的背景</li>

时间:2013-11-23 00:41:41

标签: html css drop-down-menu

我正在尝试从头开始创建一个简单的下拉菜单,在阅读了一些类似的问题后,我似乎无法解决我的具体问题。

问题还有其他提及,使所有菜单项的下拉菜单背景宽度相同。

这是我现在正在运行的代码:demo

HTML:

<div class="menu_container">
            <ul> 
                <li><a class="menu_item" href="home.html">Home</a></li>
                <li>|</li>
                <li><a class="menu_item" href="#">Work</a>
                    <ul>
                        <li><a class="menu_item_drop" href="#">Web Design</a></li>
                        <li><a class="menu_item_drop" href="#">Graphic Design</a></li>
                        <li><a class="menu_item_drop" href="#">Animation</a></li>
                        <li><a class="menu_item_drop" href="#">Sound Design/Music</a></li>
                        <li><a class="menu_item_drop" href="#">Game Development</a></li>
                    </ul>
                </li>
                <li>|</li>
                <li><a class="menu_item" href="about_us.html">About Us</a></li>
                <li>|</li>
                <li><a class="menu_item" href="contact.html">Contact</a></li>
            </ul>
        </div>

CSS:

.menu_container{
    width: 750px;
    margin-left: auto;
    margin-right: auto;
}


.menu_container ul li{ 
    list-style:none;
    display: inline;
    margin: 30px;
    padding: 2px;
    width: 100%;
    }

.menu_item{
    text-decoration: none;
    color:#999;
    padding: 2px;
    }

.menu_item_selected{
    text-decoration: none;
    background-color: #333;
    color: #FFF;
    border-radius: 2px;
    padding: 2px;
    }


.menu_item:hover{
    text-decoration: none;
    color: #000;
    padding: 2px;   
}

.menu_container ul ul{
    position: absolute;
    float: left;
    display: none;
    list-style: none;
    padding: 6px;
    margin: 1px;
    }

.menu_container ul li ul li{
    width: 100%;
    display: block;
    }

.menu_container ul li:hover > ul{
    display: block;
    }

.menu_container ul li:hover > ul li{
    display: block;
    margin: 0px;
    position: relative;
    z-index: 5;
    left: 140px;
    top: -12px;
    width: 300px;
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 10px;
    margin-right: 10px;
    padding: 0px;
    }

.menu_item_drop{
    text-decoration: none;
    color:#ddd;
    background-color: #111;
    padding: 12px;
    }

.menu_item_drop:hover{
    text-decoration: none;
    color: #222;
    background-color: #bbb;
    padding: 12x;
    }

1 个答案:

答案 0 :(得分:0)

试试这个

.menu_container{
    width: 750px;
    margin-left: auto;
    margin-right: auto;
}


.menu_container ul li{ 
    list-style:none;
    display: inline;
    margin: 30px;
    padding: 2px;
    width: 100%;
    }

.menu_item{
    text-decoration: none;
    color:#999;
    padding: 2px;
    }

.menu_item_selected{
    text-decoration: none;
    background-color: #333;
    color: #FFF;
    border-radius: 2px;
    padding: 2px;
    }


.menu_item:hover{
    text-decoration: none;
    color: #000;
    padding: 2px;   
}

.menu_container ul ul{
    position: absolute;
    float: left;
    display: none;
    list-style: none;
    padding: 6px;
    margin: 6px 0 0 0;
    }

.menu_container ul li ul li{
    width: 100%;
    display: block;
    background-color: #111111;
    padding: 12px;
    }

.menu_container ul li:hover > ul{
    display: block;
    }

.menu_container ul li:hover > ul li{
    display: block;
    margin: 0;
    position: relative;
    z-index: 5;
    left: 140px;
    top: -12px;
    width: 300px;
    padding: 12px;
    }

.menu_container ul li > ul li:hover{
    background-color: #bbb;
}

.menu_container ul li > ul li:hover a{
    color: #000;
}

.menu_item_drop{
    text-decoration: none;
    color:#ddd;
    }

.menu_item_drop:hover{
    text-decoration: none;
    color: #222;
    }

主要的变化是将背景颜色和填充从a移动到li,并从嵌套的li中删除边距(下拉)

http://jsfiddle.net/uxckd/2/