Bootstrap 4实现div内三个按钮的响应式布局与中大屏同行对齐方法
问题定位
- 按钮所在容器未使用Bootstrap 4内置弹性布局,仅靠自定义
align-center类无法约束inline类型按钮的排列规则,当容器宽度不足时按钮会自动换行、错位 - 按钮容器栅格设置为
col-lg-3 col-md-12,中等尺寸屏幕下虽占满整行,但未指定内部元素不换行的规则,极易出现布局错乱
调整方案
- 替换按钮容器的自定义类为Bootstrap 4内置弹性布局类,指定中等及以上屏幕下按钮不换行、居中对齐
- 为按钮增加统一间距,避免元素紧贴导致的排列异常
- 可选调整输入控件的栅格规则,优化中等屏幕下的整体布局密度
调整后完整代码
<div class="col-12" style="margin-bottom: 15px !important;"> <form method="get" action="{{route('invoice.index')}}" id="invoice_search"> <div class="row" style="background: #f8f9fa !important; padding: 10px;"> <div class="col-lg-3 col-md-12" style="margin-bottom: 5px; margin-top: 5px;"> <select name="provider" class="form-control"> <option value="">{{__('dashboard.invoicesView.provider')}}</option> @forelse($providers as $provider) <option value="{{$provider->provider}}" {{(request()->get('provider') === $provider->provider) ? 'selected' : ''}}>{{{$provider->provider}}}</option> @empty @endforelse </select> </div> <div class="col-lg-3 col-md-12" style="margin-bottom: 5px; margin-top: 5px;"> <input type="text" readonly style="background: #00000000 !important;" class="form-control invoice_date_range" value="{{request()->has('start_date') ? \Carbon\Carbon::parse(request()->get('start_date'))->format('m/d/Y') : '01/01/2021'}} - {{request()->has('end_date') ? \Carbon\Carbon::parse(request()->get('end_date'))->format('m/d/Y') : '01/15/2021'}}" /> <input type="hidden" name="start_date" id="start_date"> <input type="hidden" name="end_date" id="end_date"> </div> <div class="col-lg-3 col-md-12 irs-demo" style="margin-bottom: 5px; margin-top: 5px;"> <input type="text" id="range_03" value="" /> <input type="hidden" name="min_amount" id="min_amount"> <input type="hidden" name="max_amount" id="max_amount"> </div> <!-- 按钮容器调整为弹性布局 --> <div class="col-lg-3 col-md-12 d-flex justify-content-center align-items-center flex-md-nowrap flex-wrap" style="margin-top: 5px;"> <button type="submit" class="btn btn-default mr-2">{{__('dashboard.invoicesView.search')}}</button> <a href="{{route('invoice.index')}}" class="btn btn-default mr-2" title="Clear Filter"> <img src="{{asset('img/icon/clear-filters.png')}}" id="clear_filter" style="width: 20px; height: 20px;"> </a> <button type="button" name="invoice_upload" class="btn btn-default" data-toggle="modal" data-target=".upload-invoice-modal">{{__('dashboard.invoicesView.uploadInvoice')}}</button> </div> </div> </form> </div>
效果说明
- 核心生效类
flex-md-nowrap:保证中等及以上尺寸(≥768px)屏幕下,弹性容器内的按钮不会自动换行,始终保持同行排列 d-flex:将容器设置为弹性盒模型,替代原自定义对齐类的无效布局,配合justify-content-center实现按钮水平居中、align-items-center实现垂直对齐mr-2:给非末尾按钮增加统一右边距,避免按钮紧贴造成的视觉错乱- 小尺寸(<768px)屏幕下,容器启用
flex-wrap规则,按钮宽度超出容器时自动换行,不会出现溢出、错位问题 - 如需优化中等屏幕下的布局密度,可将三个输入控件的栅格类从
col-lg-3 col-md-12改为col-lg-3 col-md-6,让输入控件在中等屏幕下两列排布,减少整体纵向占用高度
内容的提问来源于stack exchange,提问作者Irshad Khan
相关产品推荐
相关产品推荐

