HTML+CSS實現簡單下拉選單
HTML+CSS實現簡單的下拉選單
1.下拉選單例項
程式碼如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<style>
*{
margin:0;
padding:0;
text-decoration:none;
list-style:none;
}
body{
text-align:center;
}
.header{
display:inline-block;
position:relative;
background-color:#4CAF50;
}
.header:hover .downbtn{
display:block;
background-color: #f1f1f1;
}
.header:hover{
background-color: #3e8e41;
}
.header span{
padding:15px;
line-height:61px;
cursor:pointer;
color: white;
}
.header .downbtn{
display:none;
position:absolute;
background-color:#f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
min-width: 160px;
}
.header .downbtn li{
line-height:30px;
text-align:left;
padding-left:5px;
}
.header .downbtn a:hover{
text-decoration:underline;
color:#f00;
}
.header .downbtn a{
display:block;
color:black;
width:100%;
}
</style>
</head>
<body>
<div class="header">
<span>下拉選單</span>
<div class="downbtn">
<ul>
<li><a href="#">下拉選單01</a></li>
<li><a href="#">下拉選單02</a></li>
<li><a href="#">下拉選單03</a></li>
<li><a href="#">下拉選單04</a></li>
<li><a href="#">下拉選單05</a></li>
</ul>
</div>
</div>
</body>
</html>
效果圖如下:
2.技術要點
- 下拉選單需邊的子選單隱藏(使用display:none;將元素隱藏)
- 滑鼠懸浮樣式(div:hover)
- 父元素相對定位(position:relative;)
- 子元素絕對定位(position:absolute;)