English  中文

CSS Visual Formatting Model

W3C: https://www.w3.org/TR/CSS2/visuren.html
MDN: https://developer.mozilla.org/docs/Web/Guide/CSS/Visual_formatting_model

Diagram

Block-level Box

(Anonymous) Block Box

Block Container Box

Atomic Inline-level Box

Inline-level Box

(Anonymous) Inline Box

Contents participate in container's
Inline Formatting Context

display { block | list-item | table }

Replaced Block-level Element, Table Element

Establish: Block Formatting Context (BFC)

Establish: Inline Formatting Context (IFC)

Non-replaced Inline Block, Non-replaced Table Cells

Replaced Inline-level Element, Inline-table Element

display { inline | inline-block | inline-table }

Non-replaced Inline Element


BFC
Block Container Box
Block Box
Block-level Box
or
Block Container Box
BFC
Block Box
Block-level Box
Anonymous Block Box
IFC
Inline-level Box
or
Block Container Box
IFC
Float Box
BFC
Line Box
Inline Box
Atomic Inline-level Box
Anonymous Inline Box




Case 1: Block-level Element in Inline-level Element

[W3C] 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.

  <span>
    <span>Inline-level Element</span>
    <p>Block-level Element</p>
    Inline Text
  </span>
Inline-level Element

Block-level Element

Inline Text
as
Inline Box
IFC
Line Box
Inline Box
Block-level Box
Anonymous Inline Box
↓↓↓
BFC
Anonymous Block Box
Inline Box
IFC
Line Box
Inline Box
Block-level Box
Anonymous Block Box
Inline Box
IFC
Line Box
Anonymous Inline Box



Case 2: float vs display:inline-block

[W3C] However, the current and subsequent line boxes created next to the float are shortened as necessary to make room for the margin box of the float.

  <div>
    <span style="float: left;">Float Box</span>
    <p style="display: inline-block;">Atomic Inline-level Box</p>
  </div>
Float Box

Atomic Inline-level Box
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur quas quisquam beatae illo, sit aliquid. Debitis architecto distinctio perspiciatis commodi!

Float Box
Lorem ipsum dolor sit amet commodi!

Atomic Inline-level Box

as
Block Box
BFC
Anonymous Block Box
IFC
Inline Box
Block Box
IFC
Anonymous Inline Box
↓↓↓
Block Box
IFC
Float Box
BFC
Anonymous Block Box
IFC
Anonymous Inline Box
Line Box
Atomic Inline-level Box
BFC
Anonymous Block Box
IFC
Anonymous Inline Box



Case 3: float vs overflow:hidden

[W3C] The border box of a table, a block-level replaced element, or an element in the normal flow that establishes a new block formatting context (such as an element with ‘overflow’ other than ‘visible’) must not overlap the margin box of any floats in the same block formatting context as the element itself.

  <div>
    <span style="float: left;">Float Element</span>
    <p style="overflow: hidden;">Block-level Element</p>
  </div>
Float ELement

Block-level Element
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Aspernatur quas quisquam beatae illo, sit aliquid. Debitis architecto distinctio perspiciatis commodi!

Float Element
Lorem ipsum dolor sit amet commodi!

Block-level Element

as
Block Box
BFC
Anonymous Block Box
IFC
Inline Box
Block Box
IFC
Anonymous Inline Box
↓↓↓
Block Box
BFC
Float Box
BFC
Anonymous Block Box
IFC
Anonymous Inline Box
Block Box
BFC
Anonymous Block Box
IFC
Anonymous Inline Box





Block-level Element [MDN]

Inline-level Element [MDN]




Block-level Box [W3C]

Participating in Block Formatting Context

1. Block Box

Block-level Box ∩ Block Container Box

2. Anonymous Block Box
3. Block Container Box

Contains only Block-level Box or only Inline-level Box


Inline-level Box [W3C]

1. Inline Box

Contents participate in container’s Inline Formatting Context

2. Anonymous Inline Box
3. Atomic Inline-level Box

Contents not participate in container’s Inline Formatting Context




Block Formatting Context [W3C, MDN]

Rules

Conditions

Establish a new BFC for contents


Inline Formatting Context [W3C]

Rules

Line Box

The rectangular area that contains the boxes that form a line