在我的 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">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>…</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>
将宽度设置为 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">×</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>…</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/