CSS dropdown menu
CSS Drop-down Menu
Use CSS to create an effect that displays a drop-down menu when the mouse is moved up.
Basic drop-down menu
When the mouse moves over the specified element, a drop-down menu will appear.
Example
<!DOCTYPE html> <html> <head> <title> PHP中文网(php.cn)</title> <meta charset="utf-8"> <style> .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f9f9f9; min-width: 160px; box-shadow: 0px 8px 16px 0px rgb(83, 251, 185); padding: 12px 16px; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <div class="dropdown"> <span>鼠标移动到我这!</span> <div class="dropdown-content"> <p> PHP中文网(php.cn)</p> <p>www.php.cn</p> </div> </div> </body> </html>
Run the program and try it
Example analysis
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 screens).
We use the box-shadow attribute to make the drop-down menu look like a "card".
:hover selector is used to display the drop-down menu when the user moves the mouse over the drop-down button.
Drop-down menu
Create a drop-down menu and allow users to select an item in the list. We added a link to the drop-down list and set the style:
<!DOCTYPE html> <html> <head> <title>PHP中文网(php.cn)</title> <meta charset="utf-8"> <style> .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; } </style> </head> <body> <h2>下拉菜单</h2> <p>鼠标移动到按钮上打开下拉菜单。</p> <div class="dropdown"> <button class="dropbtn">下拉菜单</button> <div class="dropdown-content"> <a href="http://www.php.cn">PHP中文网 1</a> <a href="http://www.php.cn">PHP中文网 2</a> <a href="http://www.php.cn">PHP中文网 </a> </div> </div> </body> </html>
Run the program to try it
Alignment of drop-down content
left float:left;
right float:right;
##Example
<!DOCTYPE html> <html> <head> <title>PHP中文网(php.cn)</title> <meta charset="utf-8"> <style> .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; } </style> </head> <body> <h2>下拉内容的对齐方式</h2> <p>left 和 right 属性指定了下拉内容是从左到右或从右到左。</p> <div class="dropdown" style="float:left;"> <button class="dropbtn">左</button> <div class="dropdown-content" style="left:0;"> <a href="#">PHP中文网 1</a> <a href="#">PHP中文网 2</a> <a href="#">PHP中文网 3</a> </div> </div> <div class="dropdown" style="float:right;"> <button class="dropbtn">右</button> <div class="dropdown-content"> <a href="#">PHP中文网 1</a> <a href="#">PHP中文网 2</a> <a href="#">PHP中文网 3</a> </div> </div> </body> </html>Run the program and try it
More examples
This example demonstrates how to add a drop-down menu to the navigation bar.<!DOCTYPE html> <html> <head> <title>PHP中文网(php.cn)</title> <meta charset="utf-8"> <style> 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: #f1f1f1} .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <ul> <li><a class="active" href="">主页</a></li> <li><a href="">新闻</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> <p>鼠标移动到 "下拉菜单" 链接先显示下拉菜单。</p> </body> </html>Run the program and try it
Example
How to add pictures in the drop-down menu.<!DOCTYPE html> <html> <head> <title>PHP中文网(php.cn)</title> <meta charset="utf-8"> <style> .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; } </style> </head> <body> <p>移动鼠标到图片上显示下拉内容。</p> <div class="dropdown"> <img src="https://img.php.cn/upload/course/000/000/006/580837363b987802.jpg" alt="Trolltunga Norway" width="100" height="50"> <div class="dropdown-content"> <img src="https://img.php.cn/upload/course/000/000/006/580837363b987802.jpg" alt="Trolltunga Norway" width="400" height="200"> <div class="desc">PHP中文网(php.cn)</div> </div> </div> </body> </html>Run the program and try it