CSS-Dropdown-Menü

使用 CSS 创建可悬停的下拉列表。

演示:下拉式案例

Example

请把鼠标指针移动到下面的例子上:

基础的下拉菜单

创建当用户将鼠标移到元素上时出现的下拉框。

Example

<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;
  z-index: 1;
}
.dropdown:hover .dropdown-content {
  display: block;
}
</style>
<div class="dropdown">
  <span>Mouse over me</span>
  <div class="dropdown-content">
    <p>Hello World!</p>
  </div>
</div>

Try It Yourself

例子解释:

HTML

使用任何元素打开下拉菜单内容,例如 <span> 或 <button> 元素。

使用容器元素(如 <div>)创建下拉内容,并在其中添加任何内容。

用 <div> 元素包围这些元素,使用 CSS 正确定位下拉内容。

CSS

.dropdown 类使用 position:relative,当我们希望将下拉内容放置在下拉按钮的正下方(使用 position:absolute)时,需要使用该类。

.dropdown-content 类保存实际的下拉菜单内容。默认情况下它是隐藏的,并将在悬停时显示(请看下文)。请注意,min-width 设置为 160px。可随时更改此设置。提示:如果您希望下拉内容的宽度与下拉按钮的宽度一样,请将宽度设置为 100%(设置 overflow:auto 可以在小屏幕上滚动。

Wir haben CSS verwendet box-shadow Eigenschaft, anstatt eines Rands, so dass das Dropdown-Menü wie eine "Karte" aussieht.

wenn der Benutzer den Mauszeiger auf den Dropdown-Pfeil bewegt:hover Der Selector wird verwendet, um das Dropdown-Menü anzuzeigen.

Dropdown-Menü

Erstellen Sie ein Dropdown-Menü und ermöglichen Sie dem Benutzer, eine Option aus der Liste auszuwählen:

Dieses Beispiel ist ähnlich wie das vorherige, mit dem Unterschied, dass wir im Dropdown-Feld Links hinzugefügt und ihnen Stile zugewiesen haben, um dem Dropdown-Pfeil zu entsprechen:

Example

<style>
/* Stile für den Dropdown-Pfeil setzen */
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}
/* Container <div> - Der Dropdown-Inhalt muss hier platziert werden */
.dropdown {
  position: relative;
  display: inline-block;
}
/* Dropdown-Inhalt (standardmäßig versteckt) */
.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}
/* Dropdown-Links */
.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}
/* Ändern Sie die Farbe des Dropdown-Links, wenn es gehovered wird */
.dropdown-content a:hover {background-color: #f1f1f1}
/* Zeige das Dropdown-Menü, wenn es gehovered wird */
.dropdown:hover .dropdown-content {
  display: block;
}
/* Ändern Sie die Hintergrundfarbe des Dropdown-Pfeils, wenn das Dropdown-Inhalt angezeigt wird */
.dropdown:hover .dropbtn {
  background-color: #3e8e41;
}
</style>
<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

Try It Yourself

Right-aligned Dropdown Menu Content

If you want the dropdown menu to open from right to left instead of left to right, please add right: 0;:

Example

.dropdown-content {
  right: 0;
}

Try It Yourself

More Examples

1 - Dropdown Images

How to Add Images and Other Content in Dropdown Boxes.

Please hover the mouse pointer over the image:

Try It Yourself

2 - Dropdown Navigation

How to Add Dropdown Menus in the Navigation Bar.

Try It Yourself