css - 如何使 bootstrap 下拉列表不随不同文本改变宽度或不影响其他元素?

标签 css html drop-down-menu bootstrap-4 width

我有一个带有不同选项的 Bootstrap 下拉菜单。当选择不同的选项时,它的宽度会改变(由于不同的文本长度)。这会导致它旁边的元素移动。如何防止它们移动?

目前,代码如下所示:

<div class="entry">
    <span>1</span> <!--This is the label that I want to fix -->
     <div class="btn-group">
         <button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Not Selected</button>
          <div class="dropdown-menu">
              <a class="dropdown-item">Not Selected</a>
              <a class="dropdown-item">short</a>
              <a class="dropdown-item">This is long text</a>
            </div>
       </div>
   </div>

这是我的 jsbin with all the code .

只要按钮的大小发生变化,数字标签 (1-5) 就会四处移动,使一切看起来杂乱无章且没有吸引力。我如何防止它移动或为保证适合所有文本的下拉菜单设置固定宽度?

最佳答案

向您的按钮类添加一个宽度,如下所示,这将阻止它根据所选值而改变。

.btn-success {
    width:250px;
    max-width:100%;
}

https://jsfiddle.net/0yur1s42/

关于css - 如何使 bootstrap 下拉列表不随不同文本改变宽度或不影响其他元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54220037/

相关文章:

javascript - CSS 文件 : SyntaxError: expected expression, 得到 '.'

css - 自动继承另一个div的top属性的div高度?

javascript - 停止 Div 滚动到另一个 div

c# - 如何绑定(bind)中继器 ItemDataBound,以在下拉列表 SelectedIndexChanged 上更新

javascript - 使该输入文本字段保持焦点并且在我单击其他内容时不会丢失(始终将焦点放在 ID 为 ="input_message"的输入上)?

c# - 通过该单元格顶部的突出显示后如何保持单元格的背景颜色?

Javascript - 在下拉列表中更改文本

html - Bootstrap 重置嵌套网格

php - 在 <select> 标签内而不是在 <option> 上定义选择的任何方式

javascript - 下拉分组 - 避免选择父项