Bootstrap 5列表组默认间距由.list-group-item的margin-bottom: 0.5rem控制;推荐改用.list-group的gap属性(因已设为flex容器),避免margin叠加问题,兼容性需注意Safari 14.1以下版本。
Bootstrap 5 的 .list-group 默认用 margin-bottom 给每个 .list-group-item 加了 0.5rem 间距,不是靠 padding 或 gap。直接改 .list-group-item 的 margin 会破坏首尾项的视觉节奏——顶部多出空隙、底部多出空隙,尤其嵌套或配合卡片使用时特别明显。
.list-group-item 的 margin-bottom 单独调,容易漏掉 :last-child 的重置.list-group 上用 gap(需确保父容器是 flex 或 grid)gap,只能靠覆盖 margin + :not(:last-child)
Bootstrap 5.3+ 的 .list-group 默认已是 display: flex; flex-direction: column;,天然支持 gap。这是最干净的方式:不干扰单个 item 样式,自动跳过首尾额外边距问题。
.list-group-gap-sm { gap: 0.25rem; },然后写在 HTML 里:<ul class="list-group list-group-gap-sm">
!important 去硬顶 Bootstrap 的 margin 规则,gap 优先级更高且逻辑更正交.list-group-gap-tight { gap: 0;}.list-group-gap-lg { gap: 1rem;}
Bootstrap 4 的 .list-group 是 block 流,item 之间靠 margin-bottom 分隔。想缩紧又不破坏结构,必须同时处理“非末项”和“首项”的 margin。
.list-group-no-separator > .list-group-item:not(:last-child) { margin-bottom: 0; }
:last-child,而要用 :nth-last-child(1) 更稳(但 IE 不支持)margin-top 统一加给除第一项外的所有 item,比清 margin-bottom 更可控:.list-group-item + .list-group-item { margin-top: 0.25rem; }
如果既设了 gap 又没清掉原始 margin-bottom,间距会叠加——比如 gap: 0.5rem + 默认 margin-bottom: 0.5rem = 实际 1rem 间距,而且首项顶部、末项底部各多出 0.5rem。
立即学习“前端免费学习笔记(深入)”;
.list-group-item 的实际 margin 值,别只信 CSS 规则顺序$list-group-item-margin-y 控制的就是这个 margin,覆盖它要小心影响所有 list-group 场景