CSS dropdown menu
CSS drop-down menu
Use CSS to create an effect that displays the drop-down menu after the mouse is moved up.
Drop-down menu example
Basic drop-down menu
When the mouse moves to the specified When on an element, a drop-down menu appears.
Instance
<!DOCTYPE html> <html> <head> <title>下拉菜单实例|php中文网(runoob.com)</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); padding: 12px 16px; } .dropdown:hover .dropdown-content { display: block; } </style> </head> <body> <h2>鼠标移动后出现下拉菜单</h2> <p>将鼠标移动到指定元素上就能看到下拉菜单。</p> <div class="dropdown"> <span>鼠标移动到我这!</span> <div class="dropdown-content"> <p>php 中文网</p> <p>www.php.cn</p> </div> </div> </body> </html>
Run instance»
Click "Run" Example" button to view online examples
Example analysis
HTML part:
We can use any HTM element to open the drop-down menu , such as: <span>, or a <button> element.
Use container elements (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:
.dropdown
class uses position:relative
, which will set the content of the drop-down menu to be placed in The lower right corner position of the drop-down button (using position:absolute
).
.dropdown-content
Inside the class is the actual dropdown menu. It is hidden by default and will be displayed after the mouse moves to the specified element. Note that the value of min-width
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 bandwidth of the drop-down button, you can set width
to 100% ( overflow:auto
The setting can be used on small screens scroll).
We use the box-shadow
property to make the drop-down menu look like a "card".
:hover
The selector is used to display a 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:
This example is similar to the previous example. When we added a link to the drop-down list and set the style:
Example
<!DOCTYPE html> <html> <head> <title>下拉菜单实例|php中文网</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.runoob.com">php.cn 1</a> <a href="http://www.runoob.com">php.cn 2</a> <a href="http://www.runoob.com">php.cn 3</a> </div> </div> </body> </html>
Run instance»
Click the "Run instance" button to view the online instance
Alignment of drop-down content
Instance
<!DOCTYPE html> <html> <head> <title>下拉菜单实例|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.cn 1</a> <a href="#">php.cn 2</a> <a href="#">php.cn 3</a> </div> </div> <div class="dropdown" style="float:right;"> <button class="dropbtn">右</button> <div class="dropdown-content"> <a href="#">php.cn 1</a> <a href="#">php.cn 2</a> <a href="#">php.cn 3</a> </div> </div> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance
More examples
Example: Image drop-down
<!DOCTYPE html> <html> <head> <title>下拉菜单实例</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> <h2>下拉图片</h2> <p>移动鼠标到图片上显示下拉内容。</p> <div class="dropdown"> <img src="https://img.php.cn/upload/article/000/000/015/5c6a73f219783131.png" alt="Trolltunga Norway" width="100" height="50"> <div class="dropdown-content"> <img src="https://img.php.cn/upload/article/000/000/015/5c6a73f219783131.png" alt="Trolltunga Norway" width="400" height="200"> <div class="desc">学的不仅是技术,更是梦想!</div> </div> </div> </body> </html>
Run Example ?
Click the "Run Example" button to view the online example
This example demonstrates how to add images to the drop-down menu.
Instance: Navigation bar drop-down
<!DOCTYPE html> <html> <head> <title>下拉菜单实例</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="#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> <h3>导航栏上的下拉菜单</h3> <p>鼠标移动到 "下拉菜单" 链接先显示下拉菜单。</p> </body> </html>
Run instance?
Click the "Run Instance" button to view the online instance
This example demonstrates how to add a drop-down menu to the navigation bar.