简体   繁体   English

引导下拉按钮更改 Position

[英]Bootstrap Dropdown Button Changes Position

I was following the documentation found here: https://getbootstrap.com/docs/4.1/components/dropdowns/ and added the following dropdown button:我正在关注此处找到的文档: https://getbootstrap.com/docs/4.1/components/dropdowns/并添加了以下下拉按钮:

<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Example 1</button>
</div>

The issue I'm having is that when the button is clicked on to display the dropdown-menu, the button changes it's position.我遇到的问题是,当单击按钮以显示下拉菜单时,按钮更改为 position。 This happens with both dropdown buttons I have.这发生在我拥有的两个下拉按钮上。

Buttons Without Dropdown Clicked没有点击下拉菜单的按钮

没有点击下拉菜单的按钮

Dropdown Clicked & Button Changed Position Example 1单击下拉菜单并更改按钮 Position 示例 1

下拉点击和按钮更改位置示例 1

Dropdown Clicked & Button Changed Position Example 2单击下拉菜单并更改按钮 Position 示例 2

下拉单击和按钮更改位置示例 2

You can see example code of this issue occurring: https://codepen.io/danfuentes/pen/MWaYOgz您可以看到发生此问题的示例代码: https://codepen.io/danfuentes/pen/MWaYOgz

How can I get it so that the button stays in place with the menu items appearing below it?如何获取它以使按钮保持在原位,并且菜单项显示在其下方?

You will need to provide the HTML that surrounds the button element along with the css for all of those elements.您需要提供围绕按钮元素的 HTML 以及所有这些元素的 css。

Can you update your question with this information so that we can see what's going on beyond just the button tag?您能否使用此信息更新您的问题,以便我们可以看到除了按钮标签之外发生了什么?

Update after seeing codepen:看到codepen后更新:

You had a couple of div tags in the incorrect place, please see below for the solution:您在不正确的位置放置了几个 div 标签,请参阅下面的解决方案:

 <div class="btn-group">
 <div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Example 1</button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
      <a class="dropdown-item" href="#">Dropdown tesdt</a>
      <a class="dropdown-item" href="#">Dropdown 2</a>
      <a class="dropdown-item" href="#">Dropdown 3</a>
      <a class="dropdown-item" href="#">Dropdown 4</a>
      <a class="dropdown-item" href="#">Dropdown 5</a>
    </div>
</div>

<div class="dropdown">
    <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Example 2</button>
    <div class="dropdown-menu" aria-labelledby="dropdownMenu2">
      <a class="dropdown-item" href="#">Dropdown 1</a>
      <a class="dropdown-item" href="#">Dropdown 2</a>
      <a class="dropdown-item" href="#">Dropdown 3</a>
      <a class="dropdown-item" href="#">Dropdown 4</a>
      <a class="dropdown-item" href="#">Dropdown 5</a>
    </div>
  </div>

      <button type="button" class="btn btn-primary" onclick="#">Regular Button 1</button>
    <button type="button" class="btn btn-primary" onclick="#">Regular Button 2</button>
</div>

Basically you want to wrap all of your buttons within a single div with a class of "dropdown".基本上,您希望使用“下拉”的 class 将所有按钮包装在单个 div 中。

The dropdown class declares the css position:relative which will ensure the positioning is correct for the dropdowns to display correctly without shifting other elements on the page around.下拉菜单 class 声明了 css position:relative 这将确保下拉菜单的位置正确显示,而无需移动页面上的其他元素。

Update 2: I've updated the HTML above to resolve the new problem you encountered.更新 2:我已经更新了上面的 HTML 以解决您遇到的新问题。 You will need to wrap the individual dropdowns in div's with the class of "dropdown" to ensure they can have individual links.您需要使用“下拉列表”的 class 将各个下拉列表包装在 div 中,以确保它们可以具有单独的链接。 To make sure they do not shift from their intended positions all of the buttons are now wrapped in a div with the class of "btn-group".为了确保它们不会从预期位置移动,所有按钮现在都包裹在一个 div 中,其中 class 为“btn-group”。

The "btn-group" class ensures the position is set to relative however by default it introduces a few other styling quirks such as removing the spacing between the buttons. “btn-group” class 确保 position 设置为相对,但默认情况下它引入了一些其他样式怪癖,例如删除按钮之间的间距。 In order to override this I've also added the following to the css:为了覆盖这一点,我还在 css 中添加了以下内容:

button {
  margin: 10px !important
}

The above grabs all buttons on the page and gives them an all-round margin of 10px.上面抓取页面上的所有按钮,并为它们提供 10px 的全方位边距。

If you want to overwrite any bootstrap class stylings you will need to set the.important property on those elements.如果要覆盖任何引导程序 class 样式,则需要在这些元素上设置 .important 属性。

Codepen with the solution: Codepen 与解决方案:

https://codepen.io/pen/pojvprg https://codepen.io/pen/pojvprg

Surround your elements in a parent div element with btn-grp class like so:使用btn-grp class 将您的元素包围在父 div 元素中,如下所示:

<div class="btn-group" role="group">
    <button id="btnGroupDrop1" type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
      Dropdown
    </button>
    <div class="dropdown-menu" aria-labelledby="btnGroupDrop1">
      <a class="dropdown-item" href="#">Dropdown link</a>
      <a class="dropdown-item" href="#">Dropdown link</a>
    </div>
  </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM