Bootstrap按钮滚动溢出时圆角消失问题求助
问题:滚动容器内Bootstrap按钮圆角消失的解决办法
我用Bootstrap构建页面,通过滚动条处理元素溢出。原本设置圆角样式的按钮,当页面元素过多触发滚动条后,圆角消失变为矩形,但其他样式均保留。这是overflow-y: scroll的特性吗?能否避免该问题?已尝试调整按钮宽度、更换overflow的不同实现方式,移除滚动条后按钮显示正常,但页面占用空间过大,因此不考虑该方案。
HTML代码
<div class="container px-4 px-lg-5 d-flex h-100 align-items-center justify-content-center"> <div class="d-flex justify-content-center"> <div class="text-center"> <h6 class="text-white-50 mb-2 mx-auto">Your Ordered Playlist:</h6> <div class="scrol"> {%for i in rankings%} <div class="row"> <input autocomplete="off" value="{{i}}" class="btn btn-primary mt-3 text-wrap" name="playlist"/> </div> {%endfor%} </div> </div> </div> </div>
CSS代码
.scrol { height: 400px; overflow-y: scroll; } .align-items-center { align-items: center !important; } .justify-content-center { justify-content: center !important; } .btn { --bs-btn-padding-x: 0.75rem; --bs-btn-padding-y: 0.375rem; --bs-btn-font-family: ; --bs-btn-font-size: 1rem; --bs-btn-font-weight: 400; --bs-btn-line-height: 1.5; --bs-btn-color: #212529; --bs-btn-bg: transparent; --bs-btn-border-width: 1px; --bs-btn-border-color: transparent; --bs-btn-border-radius: 0.375rem; --bs-btn-hover-border-color: transparent; --bs-btn-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.15), 0 1px 1px rgba(0, 0, 0, 0.075); --bs-btn-disabled-opacity: 0.65; --bs-btn-focus-box-shadow: 0 0 0 0.25rem rgba(var(--bs-btn-focus-shadow-rgb), .5); display: inline-block; padding: var(--bs-btn-padding-y) var(--bs-btn-padding-x); font-family: var(--bs-btn-font-family); font-size: var(--bs-btn-font-size); font-weight: var(--bs-btn-font-weight); line-height: var(--bs-btn-line-height); color: var(--bs-btn-color); text-align: center; text-decoration: none; vertical-align: middle; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; user-select: none; border: var(--bs-btn-border-width) solid var(--bs-btn-border-color); border-radius: var(--bs-btn-border-radius); background-color: var(--bs-btn-bg); transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; } .btn-primary { --bs-btn-color: #fff; --bs-btn-bg: #64a19d; --bs-btn-border-color: #64a19d; --bs-btn-hover-color: #fff; --bs-btn-hover-bg: #558985; --bs-btn-hover-border-color: #50817e; --bs-btn-focus-shadow-rgb: 123, 175, 172; --bs-btn-active-color: #fff; --bs-btn-active-bg: #50817e; --bs-btn-active-border-color: #4b7976; --bs-btn-active-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.125); --bs-btn-disabled-color: #fff; --bs-btn-disabled-bg: #64a19d; --bs-btn-disabled-border-color: #64a19d; }
解决方案及原因
这不是overflow-y: scroll的固有特性,问题根源是Bootstrap的.row类默认带有负margin,导致按钮超出了.scrol容器的可视范围。当滚动条出现时,容器的水平可用空间被滚动条占用,按钮的圆角部分被容器的溢出裁剪规则隐藏,看起来就像圆角消失了。
可以通过以下几种方式解决:
- 方案1:给滚动容器添加内边距抵消row的负margin
修改.scrol的CSS,添加左右内边距:
.scrol { height: 400px; overflow-y: scroll; padding-left: 12px; padding-right: 12px; }
方案2:移除不必要的
.row容器
单个按钮不需要使用.row布局(.row是为多列布局设计的),直接删除包裹按钮的<div class="row">,让按钮直接作为.scrol的子元素即可。方案3:调整按钮的布局类
给按钮添加mx-auto d-block类,让按钮以块级元素居中显示,避免被容器宽度挤压:
<input autocomplete="off" value="{{i}}" class="btn btn-primary mt-3 text-wrap mx-auto d-block" name="playlist"/>
以上三种方案任选其一即可恢复按钮的圆角显示,同时保留滚动条功能。
内容的提问来源于stack exchange,提问作者thetaco
相关产品推荐
相关产品推荐

