What is the difference between 'containing block' and 'block container box' in CSS?

后端 未结 1 1117
夕颜
夕颜 2020-12-18 10:37

I am reading CSS specs, the section on Visual Formatting Model and trying to understand what is the difference between these two terms: containing block and

相关标签:
1条回答
  • 2020-12-18 10:58

    Let's start at the beginning. CSS is almost entirely about boxes. A box is just a rectangular area of the canvas. The canvas is the entire 2D space on which everything is drawn.

    CSS Boxes have a whole range of flavours. Block boxes, block-level boxes, inline boxes, inline-level boxes, content boxes, padding boxes, border boxes, margin boxes, table boxes, line boxes, flex boxes, and so on. They're all just rectangular areas.

    So a block is just one type of box. Many of the above boxes are characterized by two behaviours - how they are laid out relative to their containers and peers, and how their content is laid out within them. The CSS-display spec refers to these as display-outside and display-inside respectively

    Display-outside refers to the "*-level" nature of the boxes. They're not what we're interested in here.

    All block boxes, and some inline-level boxes are block container boxes. A block container box is a box of type "block container", not necessarily a box that contains blocks. Block containers that are not block-level boxes include those that are display:inline-block and display:table-cell

    The "block" in "block container" refers to its display-inside behaviour. Block boxes are laid out vertically within them and text flows horizontally, ordinarily limited by its edges of the rectangle.

    So a "block container box" is a type of box. In contrast, "containing block" refers to a specific box. Each box defined on the canvas has exactly one containing block, with just one exception, called the initial containing block, which has no containing block.

    Only a box of type "block container box" can be a box's containing block.

    Time for an example. Let's suppose we have the HTML below: I'm deliberately going to use <span> elements throughout, because this is all about CSS, and I don't want to confuse with HTML behaviour.

    <span id="level1">
      foo
      <span id="level2">
        bar
        <span id="level3">
          baz
          <span id="level4">
            qux
          </span>
        </span>
      </span>
    </span>
    

    The CSS is very simple. Just

    #level1 { display:inline-block }
    

    The other spans are the CSS default display setting, "inline".

    Now, consider the #level4 span. Its parent is the '#level3' span, which is display:inline so the '#level3' span does not form a block container box. Similarly, the #level2 span also does not form a block container box. But the #level1 element is display:inline-block. That forms an inline-level box, but one that is a block container box. (This is what "inline-block" means).

    So the containing block for all the inline boxes formed by the #level2, #level3, #level4 spans and their text content is the block container box formed by the #level1 element's box.

    0 讨论(0)
提交回复
热议问题