From the CSS Display Module Level 3 specs there is a note about block container
that says:
Note: A block container box can both establish a block formatting context and an inline formatting context simultaneously.
How is this possible conceptually?
And how are children boxes laid out then? For example, if we have both inline-level and block-level boxes inside block container, which formatting context is used in that case? Are both formatting contexts used at the same time or one of them "wins" and the other one is put aside?
A block formatting context (BFC) is a part of a visual CSS rendering of a web page. It's the region in which the layout of block boxes occurs and in which floats interact with other elements. A block formatting context is created by at least one of the following: The root element of the document ( <html> ).
"In a block formatting context, boxes are laid out one after the other, vertically, beginning at the top of a containing block. The vertical distance between two sibling boxes is determined by the 'margin' properties.
A block formatting context is an HTML box that satisfies at least one of the following conditions: The value of display is table-cell, table-caption, inline-block, flex, or inline-flex The value of overflow is not visible. A block formatting context can be explicitly triggered.
A Block Formatting Context is a part of the visible CSS that is to be displayed on the web page in which the block boxes are positioned outside. The normal flow is the positioning scheme for which it belongs. It is an area in which the block box layout takes place in which floats interact with other elements.
Inline formatting contexts exist inside other formatting contexts and can be thought of as the context of a paragraph. The paragraph creates an inline formatting context inside which such things as <strong>, <a>, or <span> elements are used on text. The box model does not fully apply to items participating in an inline formatting context.
Each formatting context has specific rules about how layout behaves when in that context. The outermost element in a document that uses block layout rules establishes the first, or initial block formatting context.
It's entirely possible, and even necessary for certain CSS rules. The easiest way to understand this is with an example of such a box.
<div style="overflow:auto">hello world</div>
Here we have an element with display:block
(by default for div elements) and overflow:auto
. This is one way that an element's rendered box will establish a block formatting context. This affects, for example, how the box's location and dimensions are affected by the presence of floats.
Compare these two examples:
.formatting.contexts {
overflow:visible;
}
.container {
width:70px;
}
img {
float:left;
margin-right:3px;
}
<div class="container">
<img src="http://placehold.it/16" alt="placeholder grey square less than one line in height">
<div class="formatting contexts">hello world</div>
</div>
.formatting.contexts {
overflow:auto;
}
.container {
width:70px;
}
img {
float:left;
margin-right:3px;
}
<div class="container">
<img src="http://placehold.it/16" alt="placeholder grey square less than one line in height">
<div class="formatting contexts">hello world</div>
</div>
In the first example, the text wraps underneath the image. That's because the div with class "formatting contexts" has overflow:visible
, so it doesn't form a block formatting context.
However, it contains only inline boxes - formed by the text content. So by the CSS rules, it establishes an inline formatting context. The text content can therefore be laid out horizontally in line boxes within this context. It is the first line box which is shrunk to avoid overlapping with the float.
In the second example, the text does not wrap underneath the image. That's because the div with class "formatting contexts" now has overflow:auto
which means that it establishes a block formatting context, and it is the block box that is shrunk to avoid it overlapping the float. But its contents are just the same, just inline boxes, so it also establishes an inline formatting context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With