javascript - 我如何使用 HTML 和 CSS 在一行中均匀地拆分按钮?

标签 javascript jquery html css twitter-bootstrap-3

在我的 HTML 代码中,我试图在模式中创建几个按钮。我为此使用 Bootstrap。在页 footer 分,我试图在模态(或模态页脚)的宽度之间平均拆分这些按钮。

我可以使用 Bootstrap 列来实现此目的,但填充(在包含 .col-* 类的元素之间)对于此目的可能有点太大。我可以指定一个宽度,但这不会在可用空间量中平均分配元素。

这是我到目前为止尝试过的:

$(document).ready(function() {
  $(".modal").modal();
});
.foo {
  /* Hardcoded width, how could I make it that when adding buttons, width is split among amount of buttons? */
  float: left;
  width: 30%;
  margin: 0 5px;
}

.bar {
  /* Not working to center the buttons: */
  margin: 0 auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>&hellip;</p>
      </div>
      <div class="modal-footer">
        <!-- Content with three evenly spread/divided buttons on a single line -->
        <div class="bar">
          <div class="foo">
            <button type="button" class="btn btn-default btn-block">Button 1</button>
          </div>
          <div class="foo">
            <button type="button" class="btn btn-default btn-block">Button 2</button>
          </div>
          <div class="foo">
            <button type="button" class="btn btn-default btn-block">Button 3</button>
          </div>
        </div>
        <!-- End of content -->
      </div>
    </div>
  </div>
</div>

JSFiddle

将宽度设置为 30% 显然并不总是使按钮均匀分布(例如,如果我添加另一个按钮,按钮不再均匀分布)。另请注意,按钮有点向左倾斜。我希望按钮随着模式的大小而增长(也就是响应能力)。在模态框页脚内的一行中拆分/展开按钮的最佳方式是什么?

最佳答案

在栏 div 上添加 display: flex 并在子 div 上添加 flex: 1 0 auto;

那时你不需要 30% 的宽度。

.bar {
  display: flex;
}

.bar > div {
  flex: 1 0 auto;
}

$(document).ready(function() {
  $(".modal").modal();
});
.foo {
  margin: 0 5px;
}

.bar {
  display: flex;
}

.bar>div {
  flex: 1 0 auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>&hellip;</p>
      </div>
      <div class="modal-footer">
        <!-- Content with three evenly spread/divided buttons on a single line -->
        <div class="bar">
          <div class="foo">
            <button type="button" class="btn btn-default btn-block">Button 1</button>
          </div>
          <div class="foo">
            <button type="button" class="btn btn-default btn-block">Button 2</button>
          </div>
          <div class="foo">
            <button type="button" class="btn btn-default btn-block">Button 3</button>
          </div>
        </div>
        <!-- End of content -->
      </div>
    </div>
  </div>
</div>

已更新 jsfiddle

关于javascript - 我如何使用 HTML 和 CSS 在一行中均匀地拆分按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55629936/

相关文章:

javascript - jQuery 加载并不总是与 Chrome 中的 <picture> 元素一起触发

javascript - 使用多个 if 语句过滤 json 输出

javascript - 按照格式将json数据导出到html表中

javascript - CSS 转换在 IE-11 中不起作用

javascript - Rails 如何通过 :update controller action 中的 Ajax 调用返回 javascript

javascript - 我正在使用 HTML、CSS 和 Javascript 创建一个计算器。我被困在平方根函数

CSS DIV 溢出

javascript - html5不兼容浏览器测试

javascript - 这个 "delay"函数是如何工作的

javascript - JQuery:如何触发两个动画?