如何使用 CSS 建立分割按鈕下拉選單?


在網頁的選單部分,您一定見過下拉選單。當滑鼠游標停留在下拉選單按鈕上時,子選單就會顯示出來。這種分割按鈕看起來像是同一按鈕內的不同部分。這可以透過箭頭鍵或底部箭頭來表示。讓我們看看如何使用 HTML 和 CSS 建立這樣的選單。

建立下拉選單按鈕

使用 <button> 元素為下拉選單建立一個按鈕 -

<button>Button</button>

像這樣設定按鈕的樣式 -

button {
   background-color: rgb(18, 5, 95);
   color: white;
   padding: 16px;
   font-size: 20px;
   font-weight: bolder;
   font-family: monospace, sans-serif;
   border: none;
   outline: none;
}

建立分割按鈕

使用箭頭鍵來表示帶有菜單鏈接的下拉選單。我們設定了一個 div 容器 -

<div class="dropMenu">
   <button>>></button>
   <div class="dropContent">
      <a href="#">First Link</a>
      <a href="#">Second Link</a>
      <a href="#">Third Link</a>
   </div>
</div>

定位下拉選單

使用 position 屬性和 absolute 值來定位下拉選單。選單的 display 屬性設定為 inline-block -

.dropMenu {
   position: absolute;
   display: inline-block;
}
.dropMenu button {
   border-left: 1px solid #0d8bf2;
}

定位下拉內容

下拉選單中的連結(即菜單鏈接)設定為絕對定位。預設情況下,下拉內容(即菜單鏈接)是不可見的,即 display 屬性設定為 none。只有當滑鼠游標放置在上面時才會顯示。-

.dropContent {
   display: none;
   position: absolute;
   background-color: #f1f1f1;
   min-width: 160px;
   z-index: 1;
}
.dropContent a {
   color: black;
   background-color: rgb(184, 253, 255);
   font-size: 18px;
   font-weight: bold;
   padding: 12px 16px;
   text-decoration: none;
   display: block;
}

懸停時顯示下拉選單

當滑鼠懸停時,下拉菜單鏈接會顯示出來。為此,使用了 :hover 選擇器 -

.dropContent a:hover {
   background-color: rgb(48, 0, 110);
   color: white;
}
.dropMenu:hover .dropContent {
   display: block;
}

示例

以下是使用 CSS 建立分割按鈕下拉選單的程式碼 -

<!DOCTYPE html>
<html>
<head>
   <style>
      body {
         font-family: "Lucida Sans", "Lucida Sans Regular", "Lucida Grande", "Lucida Sans Unicode", Geneva, Verdana, sans-serif;
      }
      button {
         background-color: rgb(18, 5, 95);
         color: white;
         padding: 16px;
         font-size: 20px;
         font-weight: bolder;
         font-family: monospace, sans-serif;
         border: none;
         outline: none;
      }
      .dropMenu {
         position: absolute;
         display: inline-block;
      }
      .dropMenu button {
         border-left: 1px solid #0d8bf2;
      }
      .dropContent {
         display: none;
         position: absolute;
         background-color: #f1f1f1;
         min-width: 160px;
         z-index: 1;
      }
      .dropContent a {
         color: black;
         background-color: rgb(184, 253, 255);
         font-size: 18px;
         font-weight: bold;
         padding: 12px 16px;
         text-decoration: none;
         display: block;
      }
      .dropContent a:hover {
         background-color: rgb(48, 0, 110);
         color: white;
      }
      .dropMenu:hover .dropContent {
         display: block;
      }
      .btn:hover,
      .dropMenu:hover .btn {
         background-color: #0b7dda;
      }
   </style>
</head>
<body>
   <h1>Split Button dropMenu Example</h1>
   <button>Button</button>
   <div class="dropMenu">
      <button>
         >>
      </button>
      <div class="dropContent">
         <a href="#">First Link</a>
         <a href="#">Second Link</a>
         <a href="#">Third Link</a>
      </div>
   </div>
   <h1>Hover over '>>' to open dropMenu menu</h1>
</body>
</html>

更新於: 2023-12-14

788 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.