如何使用HTML和CSS在一行上均匀地拆分按钮?

时间:2019-04-11 10:08:13

标签: javascript jquery html css twitter-bootstrap-3

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

我可能正在使用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%显然并不总是会使按钮均匀分裂(例如,如果我添加另一个按钮,按钮将不再均匀分布)。还要注意,按钮有些偏向左侧。我希望按钮随着模式的大小而增加(也就是响应性)。在Modal页脚内的单行上拆分/展开按钮的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

在栏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

答案 1 :(得分:0)

您可以为此使用Bootstrap:

    <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
      <div class="btn-group mr-2" role="group" aria-label="First group">
        <button type="button" class="btn btn-secondary">Button 1</button>

      </div>
      <div class="btn-group mr-2" role="group" aria-label="Second group">
        <button type="button" class="btn btn-secondary">Button 2</button>

      </div>
      <div class="btn-group" role="group" aria-label="Third group">
        <button type="button" class="btn btn-secondary">Button 3</button>
      </div>
    </div>

我要添加另一个按钮即可。

    <div class="btn-toolbar" role="toolbar" aria-label="Toolbar with button groups">
      <div class="btn-group mr-2" role="group" aria-label="First group">
        <button type="button" class="btn btn-secondary">Button 1</button>

      </div>
      <div class="btn-group mr-2" role="group" aria-label="Second group">
        <button type="button" class="btn btn-secondary">Button 2</button>

      </div>
      <div class="btn-group" role="group" aria-label="Third group">
        <button type="button" class="btn btn-secondary">Button 3</button>
      </div>
      <div class="btn-group" role="group" aria-label="Fourth group">
        <button type="button" class="btn btn-secondary">Button 4</button>
      </div>
    </div>