2

我得到了一个带有这段代码的 CSS3 菜单:

HTML:

<div id="navi">
    <ul>
        <li><a href="#">First</a></li>
        <li class="active"><a href="#">Second</a>
            <ul class="child">
                <li><a href="#">First child</a></li>
            </ul>
        </li>
        <li><a href="#">Third</a></li>
    </ul>
</div>

CSS:

#navi {
    height: auto;
    width: auto;
}
#navi ul {
    margin: 0px;
    padding: 0px;
    list-style-type: none;
}
#navi ul li {
    float: left;
    position: relative;
}
#navi ul li a {
    line-height: 30px;
    text-decoration: none;
    text-align: center;
    display: block;
    width: 100px;
    height: 30px;
    border: thin solid #999;
    color: #FFF;
    background-color: #0CF;
    -webkit-transition: all 1s;
    -moz-transition: all 1s;
    -ms-transition: all 1s;
    -o-transition: all 1s;
    transition: all 1s;
}
#navi ul li a:hover {
    background-color: #0C3;
}
#navi ul li ul {
    position: absolute;
    -webkit-transition: height 1s linear 0s;
    -moz-transition: height 1s linear 0s;
    -ms-transition: height 1s linear 0s;
    -o-transition: height 1s linear 0s;
    transition: height 1s linear 0s;
    height: 0px;
    overflow:hidden;
}
#navi ul li:hover ul {
    height: 100px;
    -webkit-transition: height 1s linear 0s;
    -moz-transition: height 1s linear 0s;
    -ms-transition: height 1s linear 0s;
    -o-transition: height 1s linear 0s;
    transition: height 1s linear 0s;
}

小提琴Here

现在我希望菜单不是水平的。它应该是垂直的。我试图删除float:left;,但子菜单不显示。子菜单应按下下方的所有菜单项。有什么建议么?会很好。

4

3 回答 3

2

如果你这样做,它会给它完美的外观并将你的nav三件物品向下推。将此高度添加到您的特定班级:

#navi ul li:hover ul {
    height:32px;
}

但是,如果您在其他主要nav项目中有其他子菜单项,其中包含不同的项目,那么您应该为所有特定元素创建类并根据它们给它们高度。看看这个:

#navi ul li:hover .class {
    height:64px; /*for two items in sub menu and like this for other*/ 
}
于 2013-10-18T06:23:10.470 回答
2
<!DOCTYPE html>
<html>
<head>
<style> 
#navi {
    height: auto;
    width: auto;
}
#navi ul {
    margin: 0px;
    padding: 0px;
    list-style-type: none;
}
#navi ul li {

}

#navi ul li a {
    line-height: 30px;
    text-decoration: none;
    text-align: center;
    display: block;
    width: 100px;
    height: 30px;
    border: thin solid #999;
    color: #FFF;
    background-color: #0CF;
    -webkit-transition: all 1s;
    -moz-transition: all 1s;
    -ms-transition: all 1s;
    -o-transition: all 1s;
    transition: all 1s;
}
#navi ul li a:hover {
    background-color: #0C3;
}
#navi ul li ul {
    position: relative;
    -webkit-transition: height 1s linear 0s;
    -moz-transition: height 1s linear 0s;
    -ms-transition: height 1s linear 0s;
    -o-transition: height 1s linear 0s;
    transition: height 1s linear 0s;
    height: 0px;
    overflow:hidden;
}
#navi ul li:hover ul {
    height: 100px;
    -webkit-transition: height 1s linear 0s;
    -moz-transition: height 1s linear 0s;
    -ms-transition: height 1s linear 0s;
    -o-transition: height 1s linear 0s;
    transition: height 1s linear 0s;
}
</style>
</head>
<body>
<div id="navi">
    <ul>
        <li><a href="#">First</a></li>
        <li class="active"><a href="#">Second</a>
            <ul class="child">
                <li><a href="#">First child</a></li>
            </ul>
        </li>
        <li><a href="#">Third</a></li>
    </ul>
</div>

</body>
</html>

检查这个

于 2013-10-18T06:19:09.223 回答
0

你的CSS:

#navi {
    height: auto;
    width: auto;
}
#navi ul {
    margin: 0px;
    padding: 0px;
    list-style-type: none;
}
#navi ul li {
    position: relative;
}
#navi ul li a {
    line-height: 30px;
    text-decoration: none;
    text-align: center;
    display: block;
    width: 100px;
    height: 30px;
    border: thin solid #999;
    color: #FFF;
    background-color: #0CF;
    -webkit-transition: all 1s;
    -moz-transition: all 1s;
    -ms-transition: all 1s;
    -o-transition: all 1s;
    transition: all 1s;
}
#navi ul li a:hover {
    background-color: #0C3;
}
#navi ul li ul {
    position:absolute;
    -webkit-transition: height 1s linear 0s;
    -moz-transition: height 1s linear 0s;
    -ms-transition: height 1s linear 0s;
    -o-transition: height 1s linear 0s;
    transition: height 1s linear 0s;
    height: 0px;
    overflow:hidden;
}
#navi ul li:hover ul {
    position:absolute;
    left: 100px;
    top: 0px;
    height:50px;
    -webkit-transition: height 1s linear 0s;
    -moz-transition: height 1s linear 0s;
    -ms-transition: height 1s linear 0s;
    -o-transition: height 1s linear 0s;
    transition: height 1s linear 0s;
}

你的html:

<div id="navi">
    <ul>
        <li><a href="#">First</a></li>
        <li class="active"><a href="#">Second</a>
            <ul class="child">
                <li><a href="#">First child</a></li>
            </ul>
        </li>
        <li><a href="#">Third</a></li>
    </ul>
</div>

复制并粘贴此编码。它的工作。我想你想要这样的垂直菜单栏???

于 2013-10-18T06:17:22.360 回答