css - 如何在 div 之间应用间距而不会在右侧或左侧有多余的空间?

标签 css

这是我面临的常见问题之一,我最终使用讨厌的额外间隔标记来避免在右侧或左侧留有边距(如果是垂直菜单,也会在顶部或底部留有边距)。

是否有一种干净的 css 方法来避免为最后一个元素应用边距?


enter image description here

最佳答案

使用 :not(:last-child)

.box:not(:last-child) {
    margin-right: 10px;
}

或者,

.box:not(:first-child) {
    margin-left: 5px;
}

.box:not(:last-child) {
    margin-right: 5px;
}

或者,

.box {
    margin-right: 10px;
}
.box:last-child {
    margin-right: 0px;
}

或者,

.box {
    margin: 0 5px;
}

.box:first-child {
    margin-left: 0;
}

.box:last-child {
    margin-right: 0;
}

兼容性:

关于css - 如何在 div 之间应用间距而不会在右侧或左侧有多余的空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6484619/

相关文章:

jquery - 如何设计页面右侧的文本框mvc razor

javascript - Bootstrap 弹出消息

javascript - 无法对使用 js 创建的表应用 css

internet-explorer - 使用 svg 图像在 Internet Explorer 上保持纵横比

javascript - 重定向到网页没有发生

php - 如何过滤 WordPress 导航菜单中的子菜单类名?

javascript - 希望 div 在加载上面的图像后立即加载

jquery - 使用 CSS3 动画或 jQuery 将 div 向下滑动到可见区域

css - Angular 组件特定样式以使用 tailwind @apply 函数

css - 填充强制 block 元素换行