CSS 下拉菜單
CSS 下拉菜單
使用 CSS 創(chuàng)建一個(gè)鼠標(biāo)移動(dòng)上去后顯示下拉菜單的效果。
基本下拉菜單
當(dāng)鼠標(biāo)移動(dòng)到指定元素上時(shí),會(huì)出現(xiàn)下拉菜單。
實(shí)例
<!DOCTYPE html> <html> <head> <title> PHP中文網(wǎng)(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>鼠標(biāo)移動(dòng)到我這!</span> <div class="dropdown-content"> <p> PHP中文網(wǎng)(php.cn)</p> <p>m.miracleart.cn</p> </div> </div> </body> </html>
運(yùn)行程序嘗試一下
實(shí)例解析
HTML 部分:
我們可以使用任何的 HTM,元素來(lái)打開(kāi)下拉菜單,如:<span>, 或 a <button> 元素。
使用容器元素 (如: <div>) 來(lái)創(chuàng)建下拉菜單的內(nèi)容,并放在任何你想放的位置上。
使用 <div> 元素來(lái)包裹這些元素,并使用 CSS 來(lái)設(shè)置下拉內(nèi)容的樣式。
CSS 部分:
.dropdown?類使用?position:relative, 這將設(shè)置下拉菜單的內(nèi)容放置在下拉按鈕 (使用?position:absolute) 的右下角位置。
.dropdown-content?類中是實(shí)際的下拉菜單。默認(rèn)是隱藏的,在鼠標(biāo)移動(dòng)到指定元素后會(huì)顯示。 注意?min-width?的值設(shè)置為 160px。你可以隨意修改它。?注意:?如果你想設(shè)置下拉內(nèi)容與下拉按鈕的寬帶一致,可設(shè)置?width?為 100% (?overflow:auto?設(shè)置可以在小尺寸屏幕上滾動(dòng))。
我們使用?box-shadow?屬性讓下拉菜單看起來(lái)像一個(gè)"卡片"。
:hover?選擇器用于在用戶將鼠標(biāo)移動(dòng)到下拉按鈕上時(shí)顯示下拉菜單。
下拉菜單
創(chuàng)建下拉菜單,并允許用戶選取列表中的某一項(xiàng),我們?cè)谙吕斜碇刑砑恿随溄?,并設(shè)置了樣式:
<!DOCTYPE html> <html> <head> <title>PHP中文網(wǎng)(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>鼠標(biāo)移動(dòng)到按鈕上打開(kāi)下拉菜單。</p> <div class="dropdown"> <button class="dropbtn">下拉菜單</button> <div class="dropdown-content"> <a href="http://m.miracleart.cn">PHP中文網(wǎng) 1</a> <a href="http://m.miracleart.cn">PHP中文網(wǎng) 2</a> <a href="http://m.miracleart.cn">PHP中文網(wǎng) </a> </div> </div> </body> </html>
運(yùn)行程序嘗試一下
下拉內(nèi)容對(duì)齊方式
左 ?float:left;
右 ?float:right;
實(shí)例
<!DOCTYPE html> <html> <head> <title>PHP中文網(wǎng)(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>下拉內(nèi)容的對(duì)齊方式</h2> <p>left 和 right 屬性指定了下拉內(nèi)容是從左到右或從右到左。</p> <div class="dropdown" style="float:left;"> <button class="dropbtn">左</button> <div class="dropdown-content" style="left:0;"> <a href="#">PHP中文網(wǎng) 1</a> <a href="#">PHP中文網(wǎng) 2</a> <a href="#">PHP中文網(wǎng) 3</a> </div> </div> <div class="dropdown" style="float:right;"> <button class="dropbtn">右</button> <div class="dropdown-content"> <a href="#">PHP中文網(wǎng) 1</a> <a href="#">PHP中文網(wǎng) 2</a> <a href="#">PHP中文網(wǎng) 3</a> </div> </div> </body> </html>
運(yùn)行程序嘗試一下
更多實(shí)例
該實(shí)例演示了如何在導(dǎo)航條上添加下拉菜單。
<!DOCTYPE html> <html> <head> <title>PHP中文網(wǎng)(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="">主頁(yè)</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>鼠標(biāo)移動(dòng)到 "下拉菜單" 鏈接先顯示下拉菜單。</p> </body> </html>
運(yùn)行程序嘗試一下
實(shí)例
如何如何在下拉菜單中添加圖片。
<!DOCTYPE html> <html> <head> <title>PHP中文網(wǎng)(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>移動(dòng)鼠標(biāo)到圖片上顯示下拉內(nèi)容。</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中文網(wǎng)(php.cn)</div> </div> </div> </body> </html>
運(yùn)行程序嘗試一下