如何在bootstrap 4中使下拉菜单的宽度等于其父菜单的宽度

时间:2018-09-09 08:30:31

标签: html css html5 css3 bootstrap-4

我正在引导程序4中创建一个下拉菜单。我将 dropdown-toggle按钮 dropdown-menu 放在了一个带有class =“ container”的 div元素中我希望下拉切换和下拉菜单采用其父元素的完整宽度。我为此写了这段代码

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<body>
  <div class="container">
    <button class="dropdown-toggle btn-block" data-toggle="dropdown">
      dropdown
    </button>
    <div class="dropdown-menu">
      <a href="" class="dropdown-item">one</a>
      <a href="" class="dropdown-item">two</a>
      <a href="" class="dropdown-item">three</a>
      <a href="" class="dropdown-item">four</a>
    </div>
    <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>

</body>

下拉切换按钮占用了其父div的整个宽度。但是,当我单击下拉切换按钮时,我观察到下拉菜单未完全显示。我希望下拉菜单的宽度等于其父级宽度,我该怎么做? (我尝试使用width:inherit;属性,但是工作不正常)

4 个答案:

答案 0 :(得分:1)

只需添加跟随CSS classs

  .dropdown-menu-width {
      width: 689px !important //your required width
    }

还有下面的html代码段

<div class="container">
    <button class="dropdown-toggle btn-block" data-toggle="dropdown">
      dropdown
    </button>
    <div class="dropdown-menu dropdown-menu-width">
      <a href="" class="dropdown-item">one</a>
      <a href="" class="dropdown-item">two</a>
      <a href="" class="dropdown-item">three</a>
      <a href="" class="dropdown-item">four</a>
    </div>
</div>

Live Preview

希望它可以解决您的问题!

答案 1 :(得分:1)

您不需要额外的CSS。只需使用display="static" dropdown option,然后在下拉列表中使用w-100position-static类,即可使其占据父级宽度的100%。

<div class="container">
    <button class="dropdown-toggle btn-block" data-toggle="dropdown" data-display="static">
        dropdown
    </button>
    <div class="dropdown-menu position-static w-100">
        <a href="" class="dropdown-item">one</a>
        <a href="" class="dropdown-item">two</a>
        <a href="" class="dropdown-item">three</a>
        <a href="" class="dropdown-item">four</a>
    </div>
</div>

https://www.codeply.com/go/5GjATutCBb


另请参阅:How to make a Bootstrap 4 full width dropdown in Navbar?

答案 2 :(得分:0)

您可以将min-width: 100%添加到dropdown-menu

答案 3 :(得分:-1)

下拉菜单具有position:absolute属性。因此,添加width:100%;将使下拉菜单占据页面的整个宽度,而不是占据整个容器的宽度。要使下拉菜单采用其父元素的宽度,请使父元素的位置相对。要解决此问题,首先将class="position-relative"属性添加到下拉菜单的父元素中。现在,将w-100添加到下拉菜单div的class属性中。

<div class="container position-relative">
<button class="dropdown-toggle btn-block" data-toggle="dropdown">
  dropdown
</button>
<div class="dropdown-menu w-100">
  <a href="" class="dropdown-item">one</a>
  <a href="" class="dropdown-item">two</a>
  <a href="" class="dropdown-item">three</a>
  <a href="" class="dropdown-item">four</a>
</div>