如何使下拉菜单的高度与bootstrap 4中子菜单的高度相同

时间:2018-03-27 09:00:42

标签: html css drop-down-menu bootstrap-4 multi-level

我正在创建一个带有多级下拉菜单的导航栏。我想使下拉菜单的高度与子菜单的高度相同。

喜欢这个:

Like This

隐藏子菜单时,下拉菜单将返回原始大小。 喜欢这个: Like This 这是我的代码



.dropdown-submenu a::after {
  transform: rotate(-90deg);
  right: 3px;
  top: 40%;
}

.dropdown-submenu:hover .dropdown-menu,
.dropdown-submenu:focus .dropdown-menu {
  display: block;
  margin-top: -1px;
  margin-left: -1px;
  top: 0px;
  left: 100%;
  min-height: 100%;
}

.dropdown-menu {
  min-height: 200%;
}

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-sm navbar-light ">

  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            
            </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown link
                    </a>

        <ul class="dropdown-menu " aria-labelledby="navbarDropdownMenuLink">
          <li><a class="dropdown-item" href="#">Action</a></li>
          <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" data-toggle="dropdown" href="#">Something else here</a>
            <div class="dropdown-menu top">
              <a class="dropdown-item" href="#">A</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a>


            </div>
          </li>
        </ul>
      </li>
    </ul>
</nav>
&#13;
&#13;
&#13;

我该怎么做?

1 个答案:

答案 0 :(得分:1)

这是你想要的吗?我添加了一个班级.add-to-ul和一个班级.add-to-dropdown-div,名字就是这样说的。并给它一些风格。

我还在nav添加了一个ID,这样我就可以更轻松地设置bootstrap元素的样式。

&#13;
&#13;
.dropdown-submenu a::after {
  transform: rotate(-90deg);
  right: 3px;
  top: 40%;
}

.dropdown-submenu:hover .dropdown-menu,
.dropdown-submenu:focus .dropdown-menu {
  display: block;
  margin-top: -1px;
  margin-left: -1px;
  top: 0px;
  left: 100%;
  min-height: 100%;
}

.dropdown-menu {
  min-height: 200%;
}

#bootstrap-edits .add-to-ul{
    position: relative;
    width: 100%;
    top: 0;
}

#bootstrap-edits .add-to-dropdown-div{
  min-height: 100%;
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<nav class="navbar navbar-expand-sm navbar-light " id="bootstrap-edits">

  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            
            </button>

  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      Dropdown link
                    </a>

        <ul class="dropdown-menu add-to-ul" aria-labelledby="navbarDropdownMenuLink">
          <li><a class="dropdown-item" href="#">Action</a></li>
          <li class="dropdown-submenu"><a class="dropdown-item dropdown-toggle" data-toggle="dropdown" href="#">Something else here</a>
            <div class="dropdown-menu top add-to-dropdown-div">
              <a class="dropdown-item" href="#">A</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a>
              <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a> <a class="dropdown-item" href="#">b</a>


            </div>
          </li>
        </ul>
      </li>
    </ul>
</nav>
&#13;
&#13;
&#13;