4

What is the difference between a HTML element that is a block level element and a HTML element that forms a block formatting context?

Can a HTML element be both a block level element and form a block formatting context?

Does being a block level element imply that it forms a block formatting context, or conversely, does forming a block formatting context imply that it must be a block level element?

In a similar vein, how does this translate to inline elements and elements that form an inline formatting context?

(For some context, I've been trying to read Learn CSS Layout - The Pedantic Way but it's been a bit challenging to follow Chapter 1)

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
wmock
  • 5,382
  • 4
  • 40
  • 62

1 Answers1

9

Note that this answer uses the term "box" in lieu of "element", as CSS makes a distinction between elements and boxes. For the purposes of this answer, an HTML element is represented by a single box in CSS layout. In reality an element may generate any number of boxes (or none at all, as in display: none), but that's outside the scope of this question.

Can a HTML element be both a block level element and form a block formatting context?

Yes. The criteria in which a block box (i.e. a block-level block container box) may establish a BFC are stated in section 9.4.1 of CSS2.1, namely:

  • floats,
  • absolutely positioned elements, and
  • "block boxes with 'overflow' other than 'visible' (except when that value has been propagated to the viewport)" (as directly quoted from the spec)

Does being a block level element imply that it forms a block formatting context, or conversely, does forming a block formatting context imply that it must be a block level element?

Neither:

  1. The above answer implies that not all block boxes establish block formatting contexts. A block box with the CSS properties display: block; overflow: visible; float: none; position: static (or position: relative) does not establish a BFC.
  2. Conversely, an inline-block is an example of a box that establishes a BFC, but is itself inline-level, not block-level.

In a similar vein, how does this translate to inline elements and elements that form an inline formatting context?

An inline box is an inline-level box whose contents participate directly in its parent's inline formatting context (see section 9.4.2). Notably, the only boxes that can establish inline formatting contexts are block container boxes.

The difference between an inline box and an inline-block is that an inline-block's contents participate in the BFC that it establishes, not in the parent's IFC. Instead, only the inline-block itself participates in its parent's IFC.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Should point 1 say "A block box with the CSS properties `display: block; overflow: visible; float:none; position:static;` does not establish a BFC? – Alohci Jan 31 '16 at 09:00
  • @Alohci: Indeed. Thanks. – BoltClock Jan 31 '16 at 09:01
  • This might be a silly question but what determines whether a box is block level? Is it whether that box has display: block applied to it? And if so, what does a block level box imply? – wmock Jan 31 '16 at 09:04
  • 1
    @wmock: A block-level box is any box that participates in some other element's BFC (whether or not it *establishes* its own), or more simply, participates in *block layout*. It can be determined by a number of things, see [§9.7](https://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo), but it mostly comes down to the `display` property. Any of `block`, `table`, or `flex` generates a block-level box (a block box, a block-level table, or a block-level flex container respectively). Also see [this question](http://stackoverflow.com/questions/24313271/display-property-differences-for-inline-something) – BoltClock Jan 31 '16 at 09:10
  • 1
    @wmock: If I'm not wrong, a block-level box does not imply anything else other than the fact that it participates in block layout. – BoltClock Jan 31 '16 at 09:17
  • @BoltClock just to confirm, participating in block layout simply means that the box and its sibling boxes will be stacked vertically on top of each other, right? – wmock Jan 31 '16 at 09:21
  • 1
    @wmock: Yes, in normal flow. Things are a little bit different once you yank a block-level box out of normal flow (e.g. by floating or absposing it). – BoltClock Jan 31 '16 at 09:22
  • @BoltClock in your answer, you mentioned how a box can form a BFC - on the other hand, how does a box form a inline formatting context? – wmock Jan 31 '16 at 16:27
  • 1
    @wmock: A block container box (a block box, an inline-block or a table cell) automatically forms inline formatting contexts for its inline descendants as necessary. – BoltClock Jan 31 '16 at 16:32