Angular:如何调整ng2-search-filter搜索筛选管道的输入框宽度
原因
你使用的Bootstrap .form-control 类默认设置了width: 100%,会覆盖input原生的size属性,所以调整size不生效。
解决方法
方法1:使用Bootstrap内置宽度类
直接在输入框的class属性中添加Bootstrap预设的宽度类即可,可选w-25(占父容器25%宽度)、w-50(50%)、w-75(75%),按需选择即可。
修改后的input代码示例:
<input type="text" class="form-control w-50" placeholder="Search..." [(ngModel)]="filterTerm">
方法2:自定义CSS固定宽度
在app.component.css文件中添加自定义样式:
.search-input { width: 320px; /* 自行调整想要的宽度数值 */ max-width: 100%; /* 避免小屏幕下内容溢出 */ }
再把样式类加到input标签上:
<input type="text" class="form-control search-input" placeholder="Search..." [(ngModel)]="filterTerm">
如果样式没有生效,是选择器优先级不够,可以修改CSS选择器提高优先级:
.form-control.search-input { width: 320px; max-width: 100%; }
方法3:栅格系统实现响应式宽度
如果需要适配不同屏幕尺寸,可以用Bootstrap栅格系统包裹输入框:
<div class="container mt-5"> <div class="row"> <!-- 大屏占4格、中屏占6格、小屏占8格,自动适配宽度 --> <div class="col-lg-4 col-md-6 col-sm-8"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search..." [(ngModel)]="filterTerm"> </div> </div> </div> <!-- 后续表格代码保持不变 --> <table class="table"> ... </table> </div>
内容的提问来源于stack exchange,提问作者a_l_e_x
相关产品推荐
相关产品推荐

