简体   繁体   English

块级元素与块格式化上下文

[英]Block Level Element vs Block Formatting Context

What is the difference between a HTML element that is a block level element and a HTML element that forms a block formatting context? 作为块级元素的HTML元素和形成块格式化上下文的HTML元素之间有什么区别?

Can a HTML element be both a block level element and form a block formatting context? HTML元素既可以是块级元素,也可以形成块格式化上下文?

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) (对于某些情况,我一直在尝试阅读学习CSS布局 - 迂腐之道,但遵循第1章有点挑战)

Note that this answer uses the term "box" in lieu of "element", as CSS makes a distinction between elements and boxes. 请注意,此答案使用术语“框”代替“元素”,因为CSS区分元素和框。 For the purposes of this answer, an HTML element is represented by a single box in CSS layout. 出于本答案的目的,HTML元素由CSS布局中的单个框表示。 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. 实际上,元素可以生成任意数量的框(或者根本没有框,如display: none ),但这不在本问题的范围之内。

Can a HTML element be both a block level element and form a block formatting context? HTML元素既可以是块级元素,也可以形成块格式化上下文?

Yes. 是。 The criteria in which a block box (ie a block-level block container box) may establish a BFC are stated in section 9.4.1 of CSS2.1, namely: CSS2.1 第9.4.1节规定了块方框(即块级块容器盒)可以建立BFC的标准,即:

  • 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 带有CSS属性的块框display: block; overflow: visible; float: none; position: static display: block; overflow: visible; float: none; position: static display: block; overflow: visible; float: none; position: static (or position: relative ) does not establish a BFC. display: block; overflow: visible; float: none; position: static (或position: relative )不建立BFC。
  2. Conversely, an inline-block is an example of a box that establishes a BFC, but is itself inline-level, not block-level. 相反,内联块是建立BFC的框的示例,但它本身是内联级的,而不是块级的。

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 ). 内联框是一个内联级别的框,其内容直接参与其父级的内联格式化上下文(请参阅第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. 内联框和内联块之间的区别在于内联块的内容参与它建立的BFC,而不是父级的IFC。 Instead, only the inline-block itself participates in its parent's IFC. 相反,只有内联块本身参与其父级的IFC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM