如何在html中创建子菜单内的子菜单

ghhkc1vu  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(150)

目前我正在做侧边栏菜单。当我悬停链接3我想弹出另一个子菜单的右侧。
我尝试将标记更改为
1.标签,但它仍然不工作。
我能知道怎么做吗?
下面是我的代码:

<style>
.dropbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  float: right;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  left: 100%;
  top:0;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {background-color: #ddd;}

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: #3e8e41;}
</style>
</head>
<body>

<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>

先谢了。

ie3xauqp

ie3xauqp1#

你可以这样做:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
  background-color: #04AA6D;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
  left: 100%;
  top: 0;
  list-style: none;
}

.sub-menu {
  position: absolute;
  left: 100%;
  display: none;
  background-color: #f1f1f1;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown-content a:hover, .sub-menu a:hover {
  background-color: #ddd;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.sub-menu a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown-content li:hover > .sub-menu {
  display: block;
  top: 0;
  left: 100%;
  margin-top: -${"calc(" + document.querySelector(".sub-menu").offsetHeight + "px / 2)"};
}

.dropdown-content > li {
  position: relative;
}

</style>
</head>
<body>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a>
      <div class="sub-menu">
        <a href="#">Sub-link 1</a>
        <a href="#">Sub-link 2</a>
        <a href="#">Sub-link 3</a>
      </div>
    </li>
    <li><a href="#">Link 3</a>
      <div class="sub-menu">
        <a href="#">Sub-link 1</a>
        <a href="#">Sub-link 2</a>
        <a href="#">Sub-link 3</a>
      </div>
    </li>
  </div>
</div>

</body>
</html>

相关问题