如何在模态框(modal)中将div定位至表格右下角相邻位置
实现方案
你不需要使用fixed定位,仅调整Bootstrap栅格结构即可实现需求,核心是利用flex对齐规则让右侧信息块和表格底部对齐:
- 给
modal-body内部新增一层<div class="row align-items-end">容器,将原有的表格列(col-md-10)和信息列(col-md-2)都放入这个容器中。align-items-end是Bootstrap内置的flex工具类,会让行内所有列的底部自动对齐,右侧信息块就会自然贴合在表格右下角相邻位置。 - 小屏幕下(宽度小于768px),
col-md-*的栅格规则会自动失效,两个列都会变为100%宽度,表格占满整行后信息块紧随在表格下方展示,完全符合你要求的小屏布局。
如果需要调整信息块和表格的水平间距,给右侧col-md-2添加自定义的padding或margin属性调整即可。
完整修改后代码
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"> <!-- Button trigger modal --> <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalLong"> Launch demo modal </button> <!-- Modal --> <div class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">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 class="row align-items-end"> <div class="col-md-10"> <div class="table-responsive"> <table id="items-job-payments-modal-table" class="table table-striped"> <thead> <tr> <th>Date</th> <th>Amount</th> <th>Type</th> <th>Note</th> </tr> </thead> <tbody> <tr> <td>Example</td> <td>Example</td> <td>Example</td> <td>Example</td> </tr> </tbody> </table> </div> </div> <div class="col-md-2"> <div> <div class="form-group"> <label id="">Total Cost</label> <label id="">10</label> </div> <div class="form-group"> <label id="">Balance</label> <label id="">5</label> </div> </div> </div> </div> </div> <div class="modal-footer"> <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> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
内容的提问来源于stack exchange,提问作者Mohamed
相关产品推荐
相关产品推荐

