如何禁用保证金折叠?

kjo*_*kjo 183 css margin css3 collapse

有没有办法完全禁用边缘折叠?我发现的唯一解决方案(名称为"uncollapsing")需要使用1px边框或1px填充.我发现这是不可接受的:无关的像素使计算变得复杂,没有充分的理由.是否有更合理的方法来禁用此边距折叠?

hqc*_*ova 236

保证金崩溃有两种主要类型:

  • 折叠相邻元素之间的边距
  • 折叠父元素和子元素之间的边距

使用填充或边框将仅防止在后一种情况下崩溃.此外,应用于父级的overflow默认值(visible)的任何值都将阻止崩溃.因此,无论是overflow: autooverflow: hidden将具有相同的效果.也许使用时唯一的区别hidden是如果父级具有固定高度,则隐藏内容的意外后果.

其他属性,一旦应用于父级,可以帮助解决此问题是:

  • float: left / right
  • position: absolute
  • display: inline-block

你可以在这里测试所有这些:http://jsfiddle.net/XB9wX/1/.

我应该补充一点,像往常一样,Internet Explorer是个例外.更具体地说,在IE 7中,当为父元素指定某种布局时,边距不会崩溃,例如width.

来源:Sitepoint的文章折叠边距

  • 请注意,`overflow:auto`会导致滚动条出现在父元素中,而不是让溢出内容按照overflow:visible`溢出. (3认同)
  • 请注意,如果填充不是零值,它也会影响这一点 (2认同)
  • 感谢您的展示:inline-block,它救了我:) (2认同)
  • 任何与其默认值不同的`flex`值也会禁用边距折叠 (2认同)
  • 一旦浏览器支持有所提高,`display: flow-root` 可能是首选方法。 (2认同)

小智 56

您也可以使用旧的micro clearfix.

#container:before, #container:after{
    content: ' ';
    display: table;
}
Run Code Online (Sandbox Code Playgroud)

查看更新的小提琴:http://jsfiddle.net/XB9wX/97/

  • 我不明白,当我看到这个例子时,边距正在崩溃(div之间只有10px的垂直空间而不是20px) (3认同)
  • 我可以更精确地说明这一点:clearfix方法只能防止父母和孩子之间的边缘崩溃.它不会影响相邻兄弟姐妹之间的崩溃. (3认同)

Nic*_*rdu 47

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:

.parentClass {
    padding: 0.05px;
}
Run Code Online (Sandbox Code Playgroud)

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:

.noCollapse {
  padding: 0.05px;
}

.parent {
  background-color: red;
  width: 150px;
}

.children {
  margin-top: 50px;

  background-color: lime;      
  width: 100px;
  height: 100px;
}
Run Code Online (Sandbox Code Playgroud)
<h3>Border collapsing</h3>
<div class="parent">
  <div class="children">
  </div>
</div>

<h3>No border collapsing</h3>
<div class="parent noCollapse">
  <div class="children">
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Edit: changed the value from 0.1 to 0.05. As Chris Morgan mentioned in a comment bellow, and from this small test, it seems that indeed Firefox takes the 0.1px padding into consideration. Though, 0.05px seemes to do the trick.

  • 这是一个狡猾的解决方案,因为由于高 DPI 显示器和子像素计算,它“确实”在各种情况下添加了额外的像素。(Firefox 长期以来一直采用子像素布局,我相信其他浏览器最近也纷纷效仿。) (4认同)
  • 这是我最喜欢的解决方案.您甚至可以将其包含为默认样式.为什么不?`*{填充顶:0.1px}`.我们确定它适用于所有浏览器吗? (2认同)
  • 非常好的解决方案,它似乎在大多数浏览器上按预期工作.谢谢你的分享! (2认同)

Lit*_*tek 20

overflow:hidden 防止崩溃边缘,但它没有副作用 - 即它......隐藏溢出.

除了这个和你所提到的,你只需要学习它并学习它们实际有用的这一天(每3到5年).

  • `overflow:auto`很好用于防止隐藏溢出并仍然可以防止边距崩溃. (5认同)

Chu*_*Sun 12

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
CSS*修复
显示:流根
\xe2\x9c\x85 父元素折叠
\xe2\x9d\x8c 兄弟元素折叠
显示:柔性
弹性方向:列;
\xe2\x9c\x85 父元素折叠
\xe2\x9c\x85 兄弟元素折叠
\n
\n

*现代浏览器(不包括 IE11)支持display: flow-rootdisplay: flex.

\n

例子

\n

\r\n
\r\n
display: flow-root;
\r\n
display: flex;
flex-direction: column;
\r\n
\r\n
\r\n

\n


Dan*_*ter 9

每个基于webkit的浏览器都应该支持-webkit-margin-collapse属性.还有一些子属性只能为顶部或底部边距设置它.您可以给它折叠值(默认值),丢弃(如果存在相邻边距,则将边距设置为0),并分开(防止边距崩溃).

我已经测试过这适用于2014版Chrome和Safari.不幸的是,我不认为这会在IE中得到支持,因为它不是基于webkit.

阅读Apple的Safari CSS Reference以获得完整的解释.

如果您查看Mozilla的CSS webkit扩展页面,他们会将这些属性列为专有属性,并建议不要使用它们.这是因为它们很可能不会很快进入标准CSS,只有基于webkit的浏览器才会支持它们.

  • 看起来属性 `-webkit-margin-collapse` 已在 Chrome v85 中删除。我在一些工具中使用了它,但测试现在失败了。 (2认同)

Dan*_*ter 9

实际上,有一种可以完美运行的方法:

显示:flex; flex-direction:列;

只要您可以只支持IE10或更高版本

.container {
  display: flex;
  flex-direction: column;
    background: #ddd;
    width: 15em;
}

.square {
    margin: 15px;
    height: 3em;
    background: yellow;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container">
    <div class="square"></div>
    <div class="square"></div>
    <div class="square"></div>
</div>
<div class="container">
    <div class="square"></div>
    <div class="square"></div>
    <div class="square"></div>
</div>
Run Code Online (Sandbox Code Playgroud)


Hes*_*sam 8

我知道这是一个非常古老的帖子,但只是想说在父元素上使用flexbox会禁用其子元素的边距折叠.