1.基本下拉菜单
当鼠标移动到指定元素上时,会出现下拉菜单。
html:
<div class="dropdown"> <span>鼠标移动到我这!</span> <div class="dropdown-content"> <p>下拉菜单</p> <p>welcome</p> </div> </div>
css:
.dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: red; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); padding: 12px 16px; } .dropdown:hover .dropdown-content { display: block; }
效果图:
2.创建下拉菜单,并允许用户选取列表中的某一项。
html:
<div class="dropdown"> <button class="dropbtn">下拉菜单</button> <div class="dropdown-content"> <a href="https://i.cnblogs.com">加油</a> <a href="https://i.cnblogs.com">开心就好</a> <a href="https://i.cnblogs.com">博客园</a> </div> </div>
css:
.dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown-content a:hover {background-color: #f1f1f1} .dropdown:hover .dropdown-content { display: block; } .dropdown:hover .dropbtn { background-color: #3e8e41; }
效果图:
3.right: 0;
html:
<div class="dropdown" style="float:left;"> <button class="dropbtn">左</button> <div class="dropdown-content" style="left:0;"> <a href="#">water</a> <a href="#">jone</a> <a href="#">sun</a> </div> </div> <div class="dropdown" style="float:right;"> <button class="dropbtn">右</button> <div class="dropdown-content"> <a href="#">happy</a> <a href="#">html</a> <a href="#">book</a> </div> </div>
css:
.dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; right: 0; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown-content a:hover {background-color: #f1f1f1} .dropdown:hover .dropdown-content { display: block; } .dropdown:hover .dropbtn { background-color: #3e8e41; }
效果图:
4.导航栏上的下拉菜单
html:
<ul> <li><a class="active" href="#home">主页</a></li> <li><a href="#news">新闻</a></li> <div class="dropdown"> <a href="#" class="dropbtn">下拉菜单</a> <div class="dropdown-content"> <a href="#">链接 1</a> <a href="#">链接 2</a> <a href="#">链接 3</a> </div> </div> </ul>
css:
ul { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #333; } li { float: left; } li a, .dropbtn { display: inline-block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } li a:hover, .dropdown:hover .dropbtn { background-color: #111; } .dropdown { display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown-content a:hover {background-color: blue} .dropdown:hover .dropdown-content { display: block; }
效果图:
5.下拉图片
html:
<div class="dropdown"> <img src="http://fq.wc.lt//up/1520348846.png" width="100" height="50"> <div class="dropdown-content"> <img src="http://fq.wc.lt//up/1520348846.png" alt="Trolltunga Norway" width="400" height="200"> <div class="desc">welcome</div> </div> </div>
css:
.dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); } .dropdown:hover .dropdown-content { display: block; } .desc { padding: 15px; text-align: center; }
效果图: