用::after伪元素替代border-bottom实现隔项分割线更精准可控——仅对非末项添加,支持虚线/点线、响应式缩进及高清屏适配,避免末项多余线条与错位问题。
卡片列表中隔项加分割线,用传统 border-bottom 容易在最后一项多画一条线,或因 margin/padding 错位导致虚线对不齐。用 ::after 伪元素控制更精准——它只作用于指定项,位置、长度、样式完全自主。
避免最后一项误加线,核心是限定作用范围:
position: relative
position: relative(为 ::after 定位提供参照).card:not(:last-child)::after 精准选中除最后一项外的所有卡片示例 CSS:
.card:not(:last-child)::after {border 不好做等距虚线,但 background 可控性强:
repeating-linear-gradient 实现标准虚线(如 4px 实+4px 空)linear-gradient 搭配透明色,做出“中间实、两边淡出”的柔和分隔效果虚线写法示例:
.card:not(:last-child)::after {卡片左右有 padding 时,分割线若从 left: 0 开始会顶到边缘。用 calc() 自动避让:
left: calc(1rem) 对应 padding-leftwidth: calc(100% - 2rem) 确保线不超宽left: 12px; width: calc(100% - 24px)
::after 渲染轻量,无重排,但注意两点:
position: relative,否则 absolute 定位会相对于最近定位祖先,容易错位基本上就这些。用 ::after 替
代 border-bottom,不是绕路,而是把控制权真正拿回来——线在哪、多长、多虚、是否避让,全由你定。