FNC能够撑开float父元素,但是无法撑开绝对定位元素的父元素

原文链接:https://segmentfault.com/q/1010000000686154/a-1020000000686760

W3C标准的算法里,float可能参与父级高度计算,但position:absolute;始终不参与父级计算。就是这样设计的,没辙:

W3C CSS2.1 10.6.3规定了未创建BFC的非替换块级元素的高度计算规则:

This section also applies to block-level non-replaced elements in normal flow when 'overflow' does not compute to 'visible' but has been propagated to the viewport.

If 'margin-top', or 'margin-bottom' are 'auto', their used value is 0. If 'height' is 'auto', the height depends on whether the element has any block-level children and whether it has padding or borders:

The element's height is the distance from its top content edge to the first applicable of the following:

  1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
  2. the bottom edge of the bottom (possibly collapsed) margin of its last in-flow child, if the child's bottom margin does not collapse with the element's bottom margin
  3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
  4. zero, otherwise

如上述分点,分项1定义了line-box撑高容器,分项2和3定义了常规流块级元素撑高容器(margin折叠与否的两种情况),否则为0。

W3C CSS2.1 10.6.7规定了创建了BFC的元素(block formatting context roots)的高度计算规则:

If it only has inline-level children, the height is the distance between the top of the topmost line box and the bottom of the bottommost line box.
If it has block-level children, the height is the distance between the top margin-edge of the topmost block-level child box and the bottom margin-edge of the bottommost block-level child box.
Absolutely positioned children are ignored, and relatively positioned boxes are considered without their offset. Note that the child box may be an anonymous block box.
In addition, if the element has any floating descendants whose bottom margin edge is below the element's bottom content edge, then the height is increased to include those edges. Only floats that participate in this block formatting context are taken into account, e.g., floats inside absolutely positioned descendants or other floats are not.

这里规则大致跟上方一样,不同点在于margin不再折叠和float高度参与计算。

总之呢,在“撑高父容器圈”里面没有绝对定位元素的地位。

你可能感兴趣的:(css)