【发布时间】:2013-12-18 14:30:29
【问题描述】:
我正在使用 CSS 列布局模块创建一个流畅的布局,当 2 个或更多元素位于同一列的“行”中时,我看到了意想不到的结果。元素之间将出现 3 到 4 px 的间隙。我在 IE11、FireFox 24、Chrome 31 和 Safari 5.1.7 中进行了测试,它们都表现出相同的行为。
<div class="tile-container">
<div class="tile-large">1</div>
<div class="tile-wide">2</div>
<div class="tile-small">3</div>
<div class="tile-small">4</div>
<div class="tile-small">5</div>
<div class="tile-small">6</div>
<div class="tile-wide">7</div>
<div class="tile-large">8</div>
</div>
.tile-container {
-moz-column-width: 250px;
-webkit-column-width: 250px;
column-width: 250px;
column-fill: auto;
height: 502px;
background-color: gray;
}
.tile-large {
width: 250px;
height: 250px;
-webkit-column-break-inside: avoid;
break-inside: avoid;
display: inline-block;
background-color: green;
}
.tile-wide {
width: 250px;
height: 125px;
-webkit-column-break-inside: avoid;
break-inside: avoid;
display: inline-block;
background-color: blue;
}
.tile-small {
width: 125px;
height: 125px;
-webkit-column-break-inside: avoid;
break-inside: avoid;
display: inline-block;
background-color: red;
}
我不想开始使用负边距来缩小差距,因为我想引入拖放行为。向左浮动将消除差距,但这会带来另一组问题。奇怪的是,当我使用 jQueryUI 可排序时,在 drop 事件和 jQueryUI 排列元素之后,差距不再存在。
【问题讨论】:
-
边距/填充/边框?
-
这是您的行内块元素之间“不可见”间距的结果。如果要删除 div 之间的换行符,也会删除空格。请参阅副本了解一些常见的解决方法。
-
@TravisJ 感谢您的帮助,您链接的那篇文章是正确的。
-
-1 StackOverflow 上有大约 100 个关于这个问题的问题。这是你可以做的,css-tricks.com/fighting-the-space-between-inline-block-elements。