CSS 下拉式選單
CSS 下拉選單
使用 CSS 建立一個滑鼠移動上去後顯示下拉式選單的效果。
基本下拉式選單
當滑鼠移到指定元素上時,會出現(xiàn)下拉式選單。
實例
<!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>鼠標移動到我這!</span> <div class="dropdown-content"> <p> PHP中文網(wǎng)(php.cn)</p> <p>m.miracleart.cn</p> </div> </div> </body> </html>
運行程式嘗試
##實例解析
HTML 部分:我們可以使用任何的HTM,元素來開啟下拉式選單,如:<span>, 或a <button> 元素。 使用容器元素 (如: <div>) 來建立下拉式選單的內(nèi)容,並放在任何你想放的位置上。 使用 <div> 元素來包裝這些元素,並使用 CSS 來設定下拉內(nèi)容的樣式。 CSS 部分:.dropdown?類別使用?position:relative, 這將設定下拉式選單的內(nèi)容放置在下拉按鈕 (使用?position:absolute) 的右下角位置。 .dropdown-content?類別中是實際的下拉式選單。預設是隱藏的,在滑鼠移動到指定元素後會顯示。 注意?min-width?的值設定為 160px。你可以隨意修改它。?注意:?如果你想設定下拉內(nèi)容與下拉按鈕的寬頻一致,可設定?width?為 100% (?overflow:auto?設定可以在小尺寸螢幕上捲動)。 我們使用?box-shadow?屬性讓下拉式選單看起來像一個"卡片"。 :hover?選擇器用於在使用者將滑鼠移到下拉按鈕上時顯示下拉式選單。下拉選單#
建立下拉式選單,並允許使用者選取清單中的某一項,我們在下拉清單中新增了鏈接,並設定了樣式:
<!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>鼠標移動到按鈕上打開下拉菜單。</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>
執(zhí)行程式嘗試
下拉內(nèi)容對齊方式
左?float:left;
右?# #float:right;
實例
#<!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)容的對齊方式</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>
執(zhí)行程式嘗試
更多實例
此實例示範如何在導覽列上新增下拉式功能表。
<!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="">主頁</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>
執(zhí)行程式嘗試一下
實例
如何如何在下拉式選單中新增圖片。
<!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>移動鼠標到圖片上顯示下拉內(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>
執(zhí)行程式嘗試