CSS - 有人可以帮我找出我的下拉菜单导航错误的地方吗?

时间:2015-11-10 01:46:46

标签: javascript jquery html css drop-down-menu

我想学习如何以最简单,最干净的方式创建下拉导航栏。我按照wiki how仅限CSS的下拉菜单指南,但似乎我的代码与建议的添加内容不兼容。

有人可以告诉我代码中的冲突发生在哪里,或者是否有更简单的方法来实现下拉菜单(熟悉jQuery / JavaScript,如果这样更容易)?

HTML:

<!DOCTYPE html>

<html>

<head>

    <link type="text/css" rel="stylesheet" href="template_ss.css"/>

    <title></title>


    </div>
</head>

<body>

<!--------------------------header--------------------------->
    <div id="headerDiv">
        <div id="titleDiv">
            <p id= "titleText"><span>Your</span>Uni<span>verse</span></p>
        </div>

        <div class="navDiv">    
            <ul class="navUL">
                <li><a href="#!">Home</a></li>  
                <li>
                    <a href="#!">Universe</a>
                    <ul class="dropdown">
                        <li><a href="#!">1</a></li>
                        <li><a href="#!">2</a></li>
                        <li><a href="#!">3</a></li> 
                    </ul>
                </li>
                <li>
                    <p><a href="#!">Browse</a></p>
                    <ul class="dropdown">
                        <li><a href="#!">Planets</a></li>
                        <li><a href="#!">Galaxies</a></li>
                        <li><a href="#!">Odities</a></li>
                    </ul>   
                </li>
                <li>
                    <p><a href="#!">Random</a></p>
                    <ul class="dropdown">
                        <li><a href="#!">Quantum</a></li>
                        <li><a href="#!">Solar</a></li>
                        <li><a href="#!">Projectile</a></li>
                    </ul>   
                </li>
                <li>
                    <p><a href="#!">Profile</a></p>
                    <ul class="dropdown">
                        <li><a href="#!">My profile</a></li>
                        <li><a href="#!">Edit profile</a></li>
                    </ul>
                </li>
                <li>
                    <p><a href="#!">How it works</a></p>
                </li>
            </ul>
        </div>
        <div class="searchDiv">
            <form>
                <input type="text" placeholder="explore..." required>
                <input type="button" value="Search">
            </form>
        </div>
<!---------------------------body--------------------------->
    <div class="bodyDiv"></div>
</body>
</html>

CSS:

/*-------------------header----------------------*/
body{
margin:0px;
}
#headerDiv{
position: fixed;
height:12%;
width:100%;
background-image:url("deepspace.png");
background-repeat:repeat-x;
text-align: center;
}
#titleDiv{ 
width: auto;
margin: auto 0;
}
#titleText{
color:white;
font-size:130%;
text-allign:center;
font-family:verdana,san serif;
}
span:first-child{    
color:blue;
}
span{
color:purple;
}
.navDiv{
display:inline-block;
}
.navUL{
position:relative;
display:inline-block;
list-style-type:none;
margin: auto 0;
padding:0;
border-top:1 solid;
border-right:1 solid;
border-left:1 solid;
width:100%;
}
.navUL:after{
content:"";
display:table;
}
.navUL li{
padding: .2em 2em;
margin:2em,2em,2em,2em;
color: #fff;
background-color: #036;
display:inline-block;
text-align:center;
}
.dropdown{
position:absolute;
display:none;
background-color:white;
}
.navUL ul li:hover > ul{
display:inline;
}
.navUL>ul>li:after{
content:"\25BC";
font-size:.5em;
display:inline;
position:relative;
}
.searchDiv{
display:inline-block;
}
/*------------------body--------------------*/
.bodyDiv{
text-align:center;
float:left;
background-color:grey;
height:80%;
width:70%;
position:relative;
top:80%;
left:50%;
-ms-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}

1 个答案:

答案 0 :(得分:0)

级别1的li应具有属性position:relative,以便子级ul能够根据其父级设置位置。我做了一些修改,看到小提琴:click here