Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can block container establish both block and inline formatting contexts at the same time?

Tags:

css

w3c

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?

like image 511
mlst Avatar asked May 21 '19 12:05

mlst


People also ask

What is block formatting context how does it work?

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> ).

How are boxes laid out in the normal flow for block level elements?

"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.

What is a block formatting context?

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.

What is context in CSS a block?

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.

What is an inline formatting context?

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.

What are the formatting context rules?

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.


1 Answers

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.

like image 83
Alohci Avatar answered Nov 15 '22 10:11

Alohci