css导航栏定位

时间:2015-11-11 20:36:20

标签: html css

我正在尝试为爱好网站制作一个导航栏来显示我的摄影和我的其他兴趣,我对我所拥有的下拉栏的位置有轻微的问题。

how the menu shows up when I hover over the music item

我想知道如何定位下拉菜单,所以当我将鼠标悬停在音乐项目上时,它会直接显示在它下面。我尝试了位置:相关,它确实有效但它将所有项目向左移动以与下拉菜单对齐。

http://jsfiddle.net/daCrosby/Ly8wuws1/

body{
        background-color: #333;
}
 
nav ul {
          list-style: none;
          margin: 10px;
          padding: 0;
          font-family: 'Roboto', sans-serif;
          font-weight: bold;
}
nav li {
          display: inline-block;
          margin: 0 5px;
          -webkit-transition: all 0.3s;
          -moz-transition: all 0.3s;
          -o-transition: all 0.3s;
          transition: all 0.3s;
}
nav a {
          color: black;
          -webkit-transition: all 0.3s;
          -moz-transition: all 0.3s;
          -o-transition: all 0.3s;
          transition: all 0.3s;
}
nav a:hover {
                color: #12242d;
}
nav span {
                display: block;
                color: rgba(255,255,255,0.6);
}
 
.Inav ul {
        list-style: none;
        margin-top: 20%;
        padding: 0;
        text-align: left;
        top: 0;
        left: 0;
        position: fixed;
        right: 0;
        text-align: center;
        font-family: 'Roboto', sans-serif;
        font-weight: bold;
}
.Inav nav ul li {
          display: inline-block;
          margin: 0 5px;
          -webkit-transition: all 0.3s;
          -moz-transition: all 0.3s;
          -o-transition: all 0.3s;
          transition: all 0.3s;
}
.Inav nav a {
          color: black;
          position: block
          -webkit-transition: all 0.3s;
          -moz-transition: all 0.3s;
          -o-transition: all 0.3s;
          transition: all 0.3s;
          text-decoration: none;
}
.Inav nav a:hover {
                color: #12242d;
}
.Inav nav span {
                color: rgba(255,255,255,0.6);
}
 
.Inav ul li:hover ul{
        display: block;
        margin: 0px;
        padding: 0px;
}
.Inav ul li ul{
        display: none;
        position: absolute;
}
 
.Inav ul li ul li{
        display: block;
 
}
.Inav ul ul a{
        color: white;
}
<div class="Inav">
    <nav>
        <ul>
            <li><a href="./index.html"><span>Home</span></a></li>
            <li><a href="./faq.html"><span>FAQ</span></a></li>
            <li><a href="#"><span>Honesty</span></a></li>
            <li><a href="#"><span>Rand2</span></a></li>
            <li><a href="#"><span>Rand3</span></a></li>
            <li><a href="$"><span>Music</span></a>
                <ul>
                    <li><a href="#">Kanye</a></li>
                    <li><a href="#">Drake</a></li>
                </ul>
            </li>
        </ul>  
    </nav>
</div>

1 个答案:

答案 0 :(得分:1)

您需要将position: relevant添加到父列表项,然后在子菜单上添加定位topleft

nav li {
    position: relative;
}
.Inav ul li ul{
    position: absolute;
    top: 100%;   /* modify as necessary */
    left: 0px;   /* modify as necessary */
}

http://jsfiddle.net/daCrosby/Ly8wuws1/1/