CSS 2.1 spec: rationale for not collapsing margins of parent (when parent is float or has overflow other than visible)

后端 未结 1 1596
情歌与酒
情歌与酒 2021-01-01 07:37

The CSS 2.1 specification, section 8.3.1 on collapsing margins states:

Margins of elements that establish new block formatting contexts (such as flo

相关标签:
1条回答
  • 2021-01-01 08:11

    The only plausible explanation I can give is that block formatting contexts are atomic, in the sense that boxes in one block formatting context can never interact with boxes in another block formatting context. This rule on collapsing margins appears to be in the same vein as the rule that floats never intrude into other block formatting contexts or exit their own block formatting contexts into ancestor block formatting contexts.

    To expand on this: the in-flow descendants of a box that establishes a new block formatting context participate in that box's block formatting context, whereas the box itself participates in a block formatting context that's established somewhere higher up in the layout tree. So the margins of the box itself are expected to collapse with boxes in that same block formatting context, but not descendant boxes.

    Section 9.4.1 seems to support this:

    Vertical margins between adjacent block-level boxes in a block formatting context collapse.

    Notice that it says "adjacent block-level boxes in a block formatting context" — this would imply that the fact that margins don't collapse across block formatting contexts is an inherent property of a block formatting context. But this is only my interpretation of the spec; I'm not the author so I can't say for sure if this was the intended meaning.

    0 讨论(0)
提交回复
热议问题