css - 有没有办法让 CSS 边距通过字段集边界折叠?

标签 css margin fieldset

在 CSS 中,相邻的垂直边距通常会相互“折叠”(即元素之间的垂直间距将等于最大边距,而不是边距之和)。

但是,与大多数其他元素不同,fieldset 元素不允许其子元素上的边距与其兄弟元素上的边距折叠:

<div>Before div</div>
<div style="margin:0; border:0; padding:0; background:#ccc;">
    <div style="margin:20px 0;">This div has a top and bottom margin, which has collapsed outside of the parent div.</div>
</div>
<div>After div</div>

<div>Before fieldset</div>
<fieldset style="margin:0; border:0; padding:0; background:#ccc;">
    <div style="margin:20px 0;">This div has a top and bottom margin, but the parent fieldset prevents it from collapsing.</div>
</fieldset>
<div>After fieldset</div>

我认为(但不确定)这是因为字段集正在创建一个新的 block 格式化上下文 — the CSS spec doesn’t currently define whether fieldsets should or not , 但是 the HTML5 spec says it “expects” them to .

有什么方法可以防止 fieldsets 阻止他们的 child 和他们的 sibling 之间的边缘崩溃?

最佳答案

是的,fieldset 元素建立了新的 block 格式化上下文(此行为首先在浏览器中实现,因此规范将此功能合并为“预期默认呈现”的一部分)。

不幸的是,除了通过将 fieldset 元素的框设置为 display:contents 来完全删除它之外,我不知道有什么方法可以用 CSS“撤消”它。 ,目前仅在打开“实验性 Web 平台功能”标志的情况下在 Chrome 中给出了预期的结果(Firefox,虽然在 2015 年实现了 display:contents,但尚未更新其实现以适用于“不寻常的元素”,如根据 recent addition to the spec 的表单控件)。

关于css - 有没有办法让 CSS 边距通过字段集边界折叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46365175/

相关文章:

html - IOS上下巨大空间渲染的SVG图片

css - 为什么在查看源代码时外部样式表显示为内联?

asp.net - 如何从 ascx 控件注册一个 css 页面?

css - Twitter bootstrap margin ul 标签

jquery - 从字段集中获取选定的选项

html - 如何解决ie中的下拉位置问题?

css - margin-bottom 有点跨浏览器的灰色区域

css - 使具有顶部边距的 div 忽略其上方的 div

html - 如何在 `fieldset legend `中的中间位置设置文本

html - 是否可以将任何 HTML 元素设置为字段集/图例的样式?