css實現下拉選單
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
ul{
list-style: none;
}
.nav>li{
float: left;
}
ul a{
display: block;
text-decoration: none;
width: 100px;
height: 50px;
text-align: center;
line-height: 50px;
color: white;
background-color: #2f3e45;
}
.nav>li:first-child a{
border-radius: 10px 0 0 10px;
}
.nav>li:last-child a{
border-radius: 0 10px 10px 0;
}
.drop-down{
/*position: relative;*/
}
.drop-down-content{
padding: 0;
display: none;
/*position: absolute;*/
}
h3{
font-size: 30px;
clear: both;
}
.drop-down-content li:hover a{
background-color:red;
}
.nav .drop-down:hover .drop-down-content{
display: block;
}
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">下拉選單</a></li>
<li class="drop-down"><a href="#">下拉選單</a>
<ul class="drop-down-content">
<li><a href="#">我是1</a></li>
<li><a href="#">我是2</a></li>
<li><a href="#">我是3</a></li>
</ul>
</li>
<li><a href="#">下拉選單</a></li>
<li><a href="#">下拉選單</a></li>
<li><a href="#">下拉選單</a></li>
</ul>
<h3>我是測試文字</h3>
</body>
</html>
這是首先考慮到的實現方法,給 .drop-down-content 新增display:none,當懸浮在.drop-down上時 .drop-down-content的display變成block,缺點是不能新增過渡屬性,慢慢彈出下來選單。當.drop-down-content顯示時會把後面的盒子往下擠,因為.drop-down-content 顯示時是存在於文件流中的,給.drop-down設定position:relative,.drop-down-content設定position:absolute,使下拉選單脫離了文件流來解決,上面註釋的地方改過來即可