CSS dropdown menu
Use CSS to create an effect that displays a drop-down menu when the mouse is moved up.
Drop-down menu example:
When the mouse moves over the specified element, a drop-down menu will appear.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style type="text/css"> ul,ol,li {list-style:none;padding:0px;margin:0px;} #menu * {line-height:30px;z-index:10;} #menu a { text-decoration:none; display:block; } #menu ul { text-align:left; background:#333; } #menu .arrow /* 菜单项的右侧小箭头 */ { float:right; padding-right:5px; } #menu li:hover>ul {display:block;} #menu>ul{height:30px;} /* 即使没有菜单项也能保持顶级菜单栏的高度。 */ /* 一级菜单 */ #menu>ul>li { text-align:center; display:inline-block; width:80px; } #menu>ul>li>a {color:#fff;} #menu>ul>li:hover {background:#666;} /* 下拉的菜单栏 */ #menu>ul>li ul { display:none; width:150px; position:absolute; background:#c1cd94; box-shadow:2px 2px 2px #000; -webkit-box-shadow:2px 2px 2px #000; -moz-box-shadow:2px 2px 2px #123; } /* 下拉菜单的菜单项 */ #menu>ul>li>ul li {padding-left:5px; position:relative;} #menu>ul>li>ul li>a {color:#000;} #menu>ul>li>ul li:hover {background:#d3dbb3;} </style> <body> <div id="menu"> <ul> <li><a href="">菜单一</a> <ul> <li><a href="">子菜单1</a></li> <li><a href="">子菜单2</a> <ul><li><a href="">子菜单7</a></li></ul> </li> <li><a href="">子菜单3</a></li> </ul> </li> <li><a href="">菜单二</a> <ul> <li><a href="">子菜单4</a></li> <li><a href="">子菜单5</a></li> <li><a href="">子菜单6</a></li> </ul> </li> </ul> </div> </body> </html>
Create a drop-down menu and allow the user to select an item in the list:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php.cn</title> <style type="text/css"> /* 下拉按钮样式 */ .dropbtn { background-color: black; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } /* 容器 <div> - 需要定位下拉内容 */ .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: yellow;} /* 在鼠标移上去后显示下拉菜单 */ .dropdown:hover .dropdown-content { display: block; } /* 当下拉内容显示后修改下拉按钮的背景颜色 */ .dropdown:hover .dropbtn { background-color: #3e8e41; } </style> <body> <div class="dropdown"> <button class="dropbtn">下拉菜单</button> <div class="dropdown-content"> <a href="#">菜单栏 1</a> <a href="#">菜单栏 2</a> <a href="#">菜单栏 3</a> </div> </div> </body> </html>
Comments:
HTML part:
We can use any HTM element to open the drop-down menu , such as: <span>, or a <button> element.
Use a container element (such as <div>) to create the content of the drop-down menu and place it wherever you want.
Use <div> elements to wrap these elements, and use CSS to style the dropdown content.
CSS part: The
.dropdown class uses position:relative, which will set the content of the drop-down menu to be placed in the lower right corner of the drop-down button (using position:absolute). The
.dropdown-content class is the actual drop-down menu. It is hidden by default and will be displayed after the mouse moves to the specified element. Note that the min-width value is set to 160px. You can modify it as you like. Note: If you want to set the drop-down content to be consistent with the width of the drop-down button, you can set width to 100% (the overflow:auto setting can scroll on small screen sizes).
We use the box-shadow attribute to make the drop-down menu look like a "card".