纯css子菜单垂直

时间:2013-10-18 05:59:57

标签: html css css3

我有一个带有此代码的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;,但子菜单不显示。子菜单应按下面的所有菜单项。有什么建议?会很棒。

3 个答案:

答案 0 :(得分: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>

检查

答案 1 :(得分: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*/ 
}

答案 2 :(得分: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>

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