Block Formatting Contexts

2024-05-27 05:18
文章标签 block contexts formatting

本文主要是介绍Block Formatting Contexts,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Block Formatting Contexts

转自:Block Formatting Contexts

A block formatting context is a box that satisfies at least one of the following:

  • the value of “float” is not “none”,
  • the used value of “overflow” is not “visible”,
  • the value of “display” is “table-cell”, “table-caption”, or “inline-block”,
  • the value of “position” is neither “static” nor “relative”.

When it comes to the visual formatting model (this is how user agents process the document tree for visual media), block formatting contexts are big players. So it is crucial for CSS authors to have a solid understanding of their relationship with the flow, floats, clear, and margins.

How block formatting contexts flow

The positioning scheme to which block formatting contexts belong is normal flow. Therefore, the “block” of a block formatting context is positioned in the flow of the page as you’d expect with block boxes, inline formatting of inline boxes, relative positioning of block or inline boxes, and positioning of run-in boxes. Simply put, they are part of the page flow.

What triggers block formatting contexts

ection 9.4.1 says that the following will establish new block formatting contexts:

  • floats,
  • absolutely positioned elements,
  • inline-blocks,
  • table-cells,
    – table-captions,
  • elements styled with “overflow” (any value other than “visible”)
  • elements styled with “display:flex” or “inline-flex” (flex boxes)

But according to the CSS level 3 specification, a block formatting context (a “flow root” in CSS3 speak) is created when the following condition is met:

  • The value of ” position” is neither “static” nor “relative” (see [CSS3POS]).

This definition means that position:fixed establishes a new block formatting context, too. This is not a miss in section 9.4.1, though; fixed positioning is a subcategory of absolute positioning (9.6.1) and references in the specification to an absolutely positioned element (or its box) imply that the element’s ” position” property has the value “absolute” or “fixed” .

Note that display:table does not establish block formatting contexts per se. But because it can generate anonymous boxes, one of them (with display:table-cell) establishes a new block formatting context. In other words, the trigger is the anonymous box, not display:table. This is something authors should keep in mind, because even if both styles establish new block formatting contexts (implicitly or explicitly), clear does not work the same with display:table as it does with display:table-cell.

A final trigger is the fieldset element. Oddly enough, there was no information on www.w3.org about this behavior until the HTML5 specification. There were browser bugs ( Webkit, Mozilla) that mentioned this, but nothing “official”. Actually, even if fieldsets establish new block formatting contexts in most browsers, as per section 3.2 (UA conformance), authors were not supposed to take this for granted:

CSS 2.1 does not define which properties apply to form controls and frames, or how CSS can be used to style them. User agents may apply CSS properties to these elements. Authors are recommended to treat such support as experimental. A future level of CSS may specify this further.

What block formatting contexts do

Block formatting contexts contain floats because of the way they flow, and per section 9.4.1, they prevent collapsing margins and do not overlap floats:

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. Vertical margins between adjacent block boxes in a block formatting context collapse.

In a block formatting context, each box’s left outer edge touches the left edge of the containing block (for right-to-left formatting, right edges touch). This is true even in the presence of floats (although a box’s line boxes may shrink due to the floats), unless the box establishes a new block formatting context (in which case the box itself may become narrower due to the floats).

Block formatting contexts behave more or less like any block box, apart from these important exceptions:

Block formatting contexts prevent margin collapsing

Vertical margins between adjacent block boxes collapse, but only if they are in the same block formatting context. In other words, if the adjacent boxes do not belong to the same block formatting context, their margin will not collapse.

Example:
This is a paragraph inside a DIV with a blue background, styled with margin:20px.

This is a paragraph inside a DIV with a blue background, styled with margin:20px.

This is a paragraph inside a DIV with a blue background, it is styled with margin:20px, The parent DIV is styled with overflow:hidden;zoom:1.

Between the two first blue boxes above, the bottom and top margin of the paragraphs collapse (the gap equals 20 pixels, not 40 pixels), but because the last DIV creates a new block formatting context, the margins of the third paragraph do not collapse, hence they do not “stick out” of the paragraph’s container but instead are part of that block box.

Note: in IE6, without explicit margins the DIV would fail to enclose the margins.

When it comes to collapsing margins, creating a new block formatting context acts the same as applying border or padding to the element.

Block formatting contexts contain floats

I am sure you have heard of the sentence ” a float always contains floats “, or maybe heard of the FNE ( float nearly everything) method. But the basis of this is that floats are block formatting contexts, so a better way to formulate this is to say that ” a block formatting context always contains floats “.

Example:
This paragraph is a float inside a DIV with a blue background, it is styled with margin:20px
This paragraph is a float inside a DIV with a blue background, it is styled with margin:20px. The parent DIV is styled with overflow:hidden;zoom:1.
The first paragraph is a float so it is removed from the flow and its container collapses, hence the background of this container does not show.

The second paragraph is also a float, but it is contained inside a DIV that creates a new block formatting context, hence that container encloses the child’s “margin box”. You should also note that, unlike with the first paragraph, there is no need to clear the previous box. This is often referred to as “self-clearing”, which makes lot of sense considering that block formatting contexts are a normal part of the flow.

Note: clear only clears floats within the same block formatting context.

Block formatting contexts do not overlap floats

This one is my favorite. According to the spec, the border-box of a block formatting context must not overlap the margin-box of floats in the same block formatting context as the element itself. What this means is that browsers create implicit margins on block formatting contexts to prevent them from overlapping the margin-box of floats. For this very reason, negative margins should have no effect when applied to a block formatting context next to floats (WebKit and IE6 have a problem with this though - see test case).

Because this behavior is attached to the “border box” (not the “margin box”), to create space (e.g., a 20px gap) between the pink box and its siblings, authors would need to either:

Set a 20px margin on the floats
Set margin values on the pink box greater than the width of the floats (i.e., margin:0 220px)
Yes, you’d use 220px, not 20px. Because it is the border-box that tries to fit between the floats, not the margin-box. And if I say it tries it is because that container would drop if there was not enough room for it between the two floats.

In other words, if the pink box was given a 400 pixels width, that box should drop when the parent container is narrower than 770 pixels (180px + 180px + 400px + 10px). As a side note, in a few instances, this behavior appears to be broken in Firefox (at least in v.3.5.9) (i.e., when the above construct is the first child of body - see test case).

Note: the space that shows in IE6 between the pink box and the two floats is due to the three pixel jog bug.

hasLayout versus block formatting context

As you may have noticed, all previous examples are styled using overflow:hidden;zoom:1. The former declaration establishes a new block formatting context in modern browsers while the latter triggers hasLayout in Internet Explorer (IE 5.5/6/7). This is because these renderings are very close ( similarities with the CSS specs). Like block formatting contexts, elements that are given a layout appear to prevent collapsing margins, to contain floats, and to not overlap floats.

Properties/declarations that give elements a layout
The lists below show that the properties that establish a new block formatting context also trigger hasLayout, at least the ones supported by the browser, with the exception of overflow in IE < 7.

这篇关于Block Formatting Contexts的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1006597

相关文章

[Linux Kernel Block Layer第一篇] block layer架构设计

目录 1. single queue架构 2. multi-queue架构(blk-mq)  3. 问题 随着SSD快速存储设备的发展,内核社区越发发现,存储的性能瓶颈从硬件存储设备转移到了内核block layer,主要因为当时的内核block layer是single hw queue的架构,导致cpu锁竞争问题严重,本文先提纲挈领的介绍内核block layer的架构演进,然

block对变量捕获的方式

之前见很多文章对block捕获变量的方法,会进行诸如此类的描述:“block会捕获被引用的变量, 并对其进行copy操作, 因此, 可能会导致其引用计数加1,如果处理不好, 可能因循环引用导致内存泄漏。” 实际上, 这种说法并不严谨。block对变量的捕获, 根据变量类型的不同,会采用不同的捕获方式。 (1)静态或者全局变量, 在block中直接是指针传递的方式传入block中,对其进行的操作

Linux block_device gendisk和hd_struct到底是个啥关系

本文的源码版本是Linux 5.15版本,有图有真相: 1.先从块设备驱动说起 安卓平台有一个非常典型和重要的块设备驱动:zram,我们来看一下zram这个块设备驱动加载初始化和swapon的逻辑,完整梳理完这个逻辑将对Linux块设备驱动模型有深入的理解。 zram驱动加载的时候会调用zram_add函数,源码如下: 1887/*1888 * Allocate and initia

Oracle - ORA-01789: Query block has incorrect number of result columns

一、原因     这个错误一般是在执行表之间的相加(union),相减(minus)等SQL语句时,两个个查询块具有不一致的结果列数所导致的。 二、方案     只要将两段SQL语句的列数调整为一致就可以解决。使用union时,要注意数据库字段的格式要一致,如varchar和nvarchar是不一样的。

ARC下的block导致的循环引用问题解析

引言 使用block已经有一段时间了,感觉自己了解的还行,但是几天前看到CocoaChina上一个关于block的小测试主题:【小测试】你真的知道blocks在Objective-C中是怎么工作的吗?,发现竟然做错了几道,才知道自己想当然的理解是错误的,所以抽时间学习了下,并且通过一些测试代码进行测试,产生这篇博客。 Block简介(copy一段) Block作为C语言的扩展,并不是高新

前端面试:对BFC规范(块级格式化上下文:block formatting context)的理解

块级格式化上下文(BFC)是一个独立的渲染区域,具有特定的布局规则。理解BFC对于前端开发非常重要,因为它影响元素的布局和定位。以下是对BFC的一些关键理解: 定义:BFC是一个HTML文档中的部分区域,内部的元素在该区域内独立于外部元素进行布局。BFC的创建可以通过特定的CSS属性,如overflow(非visible)、display: flow-root、position: absolut

猫猫学iOS 之BLOCK的妙用_利用block实现链式编程

猫猫分享,必须精品 原创文章,欢迎转载。转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:场景 我们有个对象人,他有两个方法,一个是学习study,一个是跑步run, 这个人有个怪癖,跑完步之后必须学习,为了实现这个方法并且能调用方便,我们让跑步和学习都回返回自己这个对象作为下一次调用的快捷方式,代码如下: 调用: int main(

iOS CoreAnimation专题——原理篇(二) UIView block动画实现原理

前言CALayer的可动画属性UIView的block动画 注意 再次深入总结 前言 上一章中我们深入研究了UIView和它持有的那个CALayer之间的关系,知道了我们对UIView的各种属性的操作实际上都是间接的操作了CALayer对应的属性。 这一章中我们将进一步探究iOS动画,看看UIView是如何将CoreAnimation封装成block动画的。 CALaye

OceanBase block_file与log过大 的问题

一、说明 block_file 是存放sstable的数据文件,由datafile_disk_percentage 参数与datafile_size参数决定,两个参数同时配置,以datafile_size为主。 datafile_disk_percentage 默认值是90 datafile_size 默认值是0M到正无穷 因为block_file 的大小是预分配的,支持调大,也支持参数调小,但是