What's the exact containing block of an absolutely positioned element in the case the containing block is inline?
Asked Answered
C

1

7
<style type="text/css">
body > div {
    display: inline;
    position: relative;
}
body > div > div {
    position: absolute;
}
</style>
<div>
    <div></div>
</div>

What's the exact containing block of the inner div?

In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.

What's the meaning of "the bounding box around the padding boxes of the first and the last inline boxes generated for that element" in the text above?

Chorography answered 1/6, 2016 at 7:10 Comment(0)
D
3

An inline element cannot "contain" a block-level box in the traditional sense. What happens is that that inline element gets split into individual inline boxes, which live in anonymous block boxes surrounding that block-level box. See section 9.2.1.1 past the first example:

When an inline box contains an in-flow block-level box, the inline box (and its inline ancestors within the same line box) are broken around the block-level box (and any block-level siblings that are consecutive or separated only by collapsible whitespace and/or out-of-flow elements), splitting the inline box into two boxes (even if either side is empty), one on each side of the block-level box(es). The line boxes before the break and after the break are enclosed in anonymous block boxes, and the block-level box becomes a sibling of those anonymous boxes. When such an inline box is affected by relative positioning, any resulting translation also affects the block-level box contained in the inline box.

(There is no translation happening here, so the last sentence is impertinent.)

Your case however is a little more special, since the only thing between the start tag of the outer div and start tag of the inner div, and end tag of the inner div and end tag of the outer div, is inter-element whitespace (including line breaks and indentation), which gets collapsed under normal circumstances. So the inline boxes that are generated end up being

  1. empty, and
  2. in the same position: the same position as the strut.

And since the inline element has no padding, the padding boxes of the empty inline boxes is measured to be zero in length, times the line height. (Note that some browser developer tools may render this as a single point marked by a crosshair when you inspect the outer div.) The containing block of the inner (absolutely positioned) div is defined by the perimeter of those padding boxes combined, and any offsets on the inner div (top, right, bottom, left) are relative to that perimeter. Note, however, that since the inline boxes don't actually have content, the actual position of the inner div is the same as the outer div, had the outer div had content.

Defector answered 9/1, 2017 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.