【问题标题】:CSS Float Left with marginal separationCSS向左浮动,边缘分隔
【发布时间】:2012-05-31 22:48:27
【问题描述】:

在处理数据驱动的内容时,我们不知道将生成的元素数量:

每个元素都应该浮动:左并排并排,直到它们排成一排,然后再排成另一排。

我想在每个元素之间进行一些分离。如果我应用 MARGIN 或 PADDING 它也会影响最后的元素。我想要的是在一行中的每个元素之间分离一定数量的像素。

例如:

.container {
   width:1000px;
}
.bubble {
   width:240px; height:200px; margin-right:10px;
}
<div class="container">
<div class="bubble"></div><div class="bubble"></div><div class="bubble"></div><div class="bubble"></div>(would put a margin here too which we do not want)
<div class="bubble"></div><div class="bubble"></div><div class="bubble"></div><div class="bubble"></div>(would put a margin here too which we do not want)
<div class="bubble"></div><div class="bubble"></div><div class="bubble"></div><div class="bubble"></div>(would put a margin here too which we do not want)
</div>

【问题讨论】:

  • 所以你需要一个边距但你不想要一个边距? ;)

标签: css css-float margin


【解决方案1】:

这是我遇到类似情况时通常使用的技巧。

.container {
    margin: -10px 0 0 -10px;
}
.bubble {
    margin: 10px 0 0 10px;
}

这将抵消容器和框将跟随。如果您不再对实际包装项目的 div 应用任何样式(在本例中为 .bubble),用户将看不到它,您将拥有完美的带有边距的网格!

【讨论】:

  • 我不知道你能做到 - CSS 中的数字
  • 当然可以!不要引用我的话;但是你可以对任何可以在 CSS 中取数值的东西使用负值。
  • @Filip 不。例如padding 只接受非负值,widthheight 等也是如此。
  • @duri 你当然是对的!我在那里写得太快了。感谢您的澄清!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多