如何展开边距?
CSS 中的折叠边距: http://www.w3.org/TR/ CSS21/box.html#collapsing-margins
我理解 该功能的目的,但我正在尝试进行布局,但我不知道如何关闭它。
CSS 教程中通常解释的方法是:
- 添加边框
- 添加填充
所有这些都会产生副作用,当您处理具有背景图像和固定填充的像素完美布局时,这些副作用会变得明显。
有没有什么方法可以简单地禁用折叠,而不必将额外的像素推入布局中?对我来说,必须在视觉上影响文档才能改变这样的行为是没有任何意义的。
Collapsing margins in CSS: http://www.w3.org/TR/CSS21/box.html#collapsing-margins
I understand the purpose of the feature, but I'm trying to do layout, and I can't figure out how to turn it off.
The way usually explained in CSS tutorials is to either:
- Add a border
- Add a padding
All of these have side effects that become obvious when you're dealing with pixel-perfect layouts with background images and fixed paddings.
Is there any way to simply disable the collapsing without having to shove extra pixels into the layout? It doesn't make any sense for me to have to visually affect the document to change behavior like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,你需要介于两者之间的东西来“打破”崩溃。
我的第一个想法是使用一个 div 并在其之间设置
display:none
,但这似乎不起作用。所以我尝试了:
这似乎很好地完成了这项工作(至少在 Firefox 中,没有在这里安装 Internet Explorer 来测试它......)
well you need something in between to "break" the collapsing.
my first thought was to use a div with
display:none
set in between, but that doesn't seem to work.so I tried:
which seems to do the job nicely (at least in firefox, don't have internet explorer installed here to test it...)
从 IE8 你可以这样做:
使用 CSS:
From IE8 you could do:
With CSS:
使用 Flex 或 Grid 布局。
在 Flex 和网格容器中,不存在边距折叠这样的事情。
规格中的更多详细信息:
Use Flex or Grid layout.
In flex and grid containers, there's no such thing as margin collapsing.
More details in the specs:
Eric Meyer 在他的文章Uncollapsing margins 中引用了您的确切观点。
他的方法请参见图 6 之后的文章正文。他提到 1px 填充/边框通常是可行的方法,但对于无法灵活添加额外像素的情况提供了一个非常简单的解决方案。
不过,它涉及手动覆盖每个元素的边距,所以我不确定它是否适用于您的特定情况。
Eric Meyer refers to your exact point in his article Uncollapsing margins.
See the text of the article after Figure 6 for his approach. He mentions that 1px padding/border is typically the way to go, but offers a pretty simple solution for instances where there's no flexibility in adding that additional pixel.
It involves manually overriding margins on each element though, so I'm not sure if it will work for your particular case.
据我所知,禁用没有视觉影响的边距折叠的一个巧妙技巧是将父级的填充设置为
0.05px
:填充不再为 0,因此折叠不会再发生但同时填充足够小,在视觉上它会向下舍入到 0。
如果需要其他填充,则仅将填充应用于不需要边距折叠的“方向”,例如
padding-顶部:0.05px;
。工作示例:
编辑:将值从
0.1
更改为0.05
。从 这个小测试来看,Firefox 似乎采用了0.1px
填充考虑在内。不过,0.05px
似乎可以解决问题。One neat trick to disable margin collapsing that has no visual impact, as far as I know, is setting the padding of the parent to
0.05px
:The padding is no longer 0 so collapsing won't occur anymore but at the same time the padding is small enough that visually it will round down to 0.
If some other padding is desired, then apply padding only to the "direction" in which margin collapsing is not desired, for example
padding-top: 0.05px;
.Working example:
Edit: changed the value from
0.1
to0.05
. From this small test, it seems that Firefox takes the0.1px
padding into consideration. Though,0.05px
seemes to do the trick.