Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<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?

like image 741
Weapon Avatar asked Jun 01 '16 07:06

Weapon


People also ask

How do you designate the containing block for an absolutely positioned element?

Identifying the containing block If the position property is absolute , the containing block is formed by the edge of the padding box of the nearest ancestor element that has a position value other than static ( fixed , absolute , relative , or sticky ).

What is block and inline block?

A block element has some whitespace above and below it and does not tolerate any HTML elements next to it. An inline-block element is placed as an inline element (on the same line as adjacent content), but it behaves as a block element.

What is block-level elements and inline elements in HTML?

There are two display values: block and inline. A block-level element always starts on a new line and takes up the full width available. An inline element does not start on a new line and it only takes up as much width as necessary.

What is absolute position in CSS?

An absolutely positioned element is an element whose computed position value is absolute or fixed . The top , right , bottom , and left properties specify offsets from the edges of the element's containing block. (The containing block is the ancestor relative to which the element is positioned.)

What is the relative position of the containing block?

(The containing block is the ancestor relative to which the element is positioned.) If the element has margins, they are added to the offset. The element establishes a new block formatting context (BFC) for its contents. A stickily positioned element is an element whose computed position value is sticky.

What is the containing block of an element?

So, the small box's containing block is the middle box, whose containing box is the large box. It's important to understand this concept because when you begin to use positioning, the containing block of an element can have a lot to do with how the element or elements it contains are actually positioned. Step back for a moment to Example 12-1.

What determines the final position of a block?

It is positioned relative to its closest positioned ancestor, if any; otherwise, it is placed relative to the initial containing block. Its final position is determined by the values of top, right, bottom, and left.

What is an absolutely positioned element?

An absolutely positioned element is an element whose computed position value is absolute or fixed. The top, right, bottom, and left properties specify offsets from the edges of the element's containing block. (The containing block is the ancestor relative to which the element is positioned.)


1 Answers

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.

like image 80
BoltClock Avatar answered Sep 22 '22 14:09

BoltClock