Bootstrap 4 中左右对齐模态页脚按钮
Left & right align modal footer buttons in Bootstrap 4
Bootstrap 4 使用 flex-box 作为模态页脚。如果我想要两个按钮,一个在左边,一个在右边,我该如何让它正常工作?
下面的代码尝试将 div.row
与 col-sm-6
结合使用,但不起作用。
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<div class="row">
<div class="col-sm-6 text-left">
<button type="button" class="btn btn-primary">Save changes</button>
</div>
<div class="col-sm-6 text-right">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
既然 modal-footer
是 Bootstrap 4 中的 "display:flex",那么使用自动边距将是最简单的。使用左侧按钮上的 mr-auto
。
<div class="modal-footer">
<button type="button" class="btn btn-primary mr-auto">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
另见:Left align and right align within div in Bootstrap
跟进评论 "What if I need the button on the right to occupy all the space left?" - 使用 btn-block
class:
<div class="modal-footer">
<button type="button" class="btn btn-primary text-nowrap">Save changes</button>
<button type="button" class="btn btn-secondary btn-block ml-1" data-dismiss="modal">Close</button>
</div>
它在 IE 中工作的唯一方法是使用 w-100。 mx-auto 和 mr 以及 ml-auto 似乎都溢出了包含元素的按钮。
<div class="modal-footer">
<div class="w-100">
<button type="button" class="btn btn-default">Cancel</button>
<button type="button" class="btn btn-primary float-right">Save</button>
</div>
</div>
这很简单,对我有帮助
<div class="modal-footer">
<button id="pagePrintBtn" type="button" class="btn btn-primary" style= "float: left;">Print</button>
</div>
<div class="modal-footer justify-content-start">
<input type="hidden" value="" name="">
<button id="pagePrintBtn" type="button" class="btn btn-primary">Print</button>
<button type="button" class="btn btn-secondary ml-auto" data-dismiss="modal">Delete</button>
<button type="button" class="btn btn-primary" id=asd>Save</button>
</div>
modal-footer
是 display:flex;
。因此,对齐其元素(例如按钮)的更好方法是使用弹性规则。 Bootstrap 4 提供了新的实用程序 类 来修改那些弹性规则(例如 .justify-content-[modifier]
)。所以我认为更好的选择应该是:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
对于Bootstrap 4
我们使用 Spacing,Bootstrap 包括范围广泛的缩写和填充响应边距实用程序 类 来修改元素的外观。 类 的命名格式为 {属性}{sides}-{size} for xs 和 {属性}{sides}-{breakpoint}-{size} for sm, md 、lg 和 xl。
更多信息在这里:https://getbootstrap.com/docs/4.1/utilities/spacing/
<!--Footer-->
<div class="modal-footer">
<button type="submit" class="btn btn-primary ml-auto">Enviar</button>
<button type="button" class="btn btn-danger mr-auto" data-dismiss="modal">Cerrar</button>
</div>
对于 IE,您可以对父元素使用 .justify-content-between
帮助程序 class 形式 bootstrap。在本例中为 .modal-footer
.
例如:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
对于想要 3 个按钮的任何人,例如右侧 2 个和左侧 1 个:
<div class="modal-footer justify-content-between">
<button type="submit" class="btn btn-danger">Delete</button>
<div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
完全同意@Zim 和@jmboiton
如果这两个这些方法一起使用,它解决了每个人的问题(包括 IE 用户:P)
总体最佳解决方案是像这样组合 .justify-content-between
和 .mr-auto
类:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-secondary mr-auto" type="button">Left Button</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-secondary mr-auto" type="button">Left Button</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
这对我有用
<div class="modal-footer">
<button type="button" class="btn btn-primary d-flex flex-grow-1">Save changes</button>
<button type="button" class="btn btn-secondary d-flex flex-grow-1" data-dismiss="modal">Close</button>
</div>
`<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>`.
您可以创建一个 div 标签 (<div class="col-md-12 text-right></div>
) 并将您的按钮标签放在该 div 部分中。这将使按钮居中。如果您想移动任何其他位置和 css 样式 float: left or right
到我们新的 div 标签中的 class。
Bootstrap 4 使用 flex-box 作为模态页脚。如果我想要两个按钮,一个在左边,一个在右边,我该如何让它正常工作?
下面的代码尝试将 div.row
与 col-sm-6
结合使用,但不起作用。
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<div class="row">
<div class="col-sm-6 text-left">
<button type="button" class="btn btn-primary">Save changes</button>
</div>
<div class="col-sm-6 text-right">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
既然 modal-footer
是 Bootstrap 4 中的 "display:flex",那么使用自动边距将是最简单的。使用左侧按钮上的 mr-auto
。
<div class="modal-footer">
<button type="button" class="btn btn-primary mr-auto">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
另见:Left align and right align within div in Bootstrap
跟进评论 "What if I need the button on the right to occupy all the space left?" - 使用 btn-block
class:
<div class="modal-footer">
<button type="button" class="btn btn-primary text-nowrap">Save changes</button>
<button type="button" class="btn btn-secondary btn-block ml-1" data-dismiss="modal">Close</button>
</div>
它在 IE 中工作的唯一方法是使用 w-100。 mx-auto 和 mr 以及 ml-auto 似乎都溢出了包含元素的按钮。
<div class="modal-footer">
<div class="w-100">
<button type="button" class="btn btn-default">Cancel</button>
<button type="button" class="btn btn-primary float-right">Save</button>
</div>
</div>
这很简单,对我有帮助
<div class="modal-footer">
<button id="pagePrintBtn" type="button" class="btn btn-primary" style= "float: left;">Print</button>
</div>
<div class="modal-footer justify-content-start">
<input type="hidden" value="" name="">
<button id="pagePrintBtn" type="button" class="btn btn-primary">Print</button>
<button type="button" class="btn btn-secondary ml-auto" data-dismiss="modal">Delete</button>
<button type="button" class="btn btn-primary" id=asd>Save</button>
</div>
modal-footer
是 display:flex;
。因此,对齐其元素(例如按钮)的更好方法是使用弹性规则。 Bootstrap 4 提供了新的实用程序 类 来修改那些弹性规则(例如 .justify-content-[modifier]
)。所以我认为更好的选择应该是:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
对于Bootstrap 4
我们使用 Spacing,Bootstrap 包括范围广泛的缩写和填充响应边距实用程序 类 来修改元素的外观。 类 的命名格式为 {属性}{sides}-{size} for xs 和 {属性}{sides}-{breakpoint}-{size} for sm, md 、lg 和 xl。
更多信息在这里:https://getbootstrap.com/docs/4.1/utilities/spacing/
<!--Footer-->
<div class="modal-footer">
<button type="submit" class="btn btn-primary ml-auto">Enviar</button>
<button type="button" class="btn btn-danger mr-auto" data-dismiss="modal">Cerrar</button>
</div>
对于 IE,您可以对父元素使用 .justify-content-between
帮助程序 class 形式 bootstrap。在本例中为 .modal-footer
.
例如:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
对于想要 3 个按钮的任何人,例如右侧 2 个和左侧 1 个:
<div class="modal-footer justify-content-between">
<button type="submit" class="btn btn-danger">Delete</button>
<div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</div>
完全同意@Zim 和@jmboiton
如果这两个这些方法一起使用,它解决了每个人的问题(包括 IE 用户:P)
总体最佳解决方案是像这样组合 .justify-content-between
和 .mr-auto
类:
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-secondary mr-auto" type="button">Left Button</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-secondary mr-auto" type="button">Left Button</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
这对我有用
<div class="modal-footer">
<button type="button" class="btn btn-primary d-flex flex-grow-1">Save changes</button>
<button type="button" class="btn btn-secondary d-flex flex-grow-1" data-dismiss="modal">Close</button>
</div>
`<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>`.
您可以创建一个 div 标签 (<div class="col-md-12 text-right></div>
) 并将您的按钮标签放在该 div 部分中。这将使按钮居中。如果您想移动任何其他位置和 css 样式 float: left or right
到我们新的 div 标签中的 class。