overflow创建的BFC和float创建的BFC 为什么会有这样的区别?
现在的css布局,浮动用的比较少了,但是还是想 问一下 浮动的BFC。
创建BFC的情况https://developer.mozilla.org/zh-CN/docs/Web/CSS/CSS_display/...
overflow:hidden 创建的BFC
https://play.tailwindcss.com/kTidNfCuyn
特点:即便img 右侧没有足够的空间,这个BFC还是选择了在img的右侧,通过延申高度来放下文字
Float创建的BFC
https://play.tailwindcss.com/T78TTJPgxf文字比较多的受直接到下一行显示,而不是在img右侧延申高度。
文字比较少的时候:
文字 再少
问题: 为什么会有这样的区别?
回复
1个回答

test
2024-06-19
overflow创建的BFC和float创建的BFC它们都是BFC应该没有区别。故这里的行为不同跟是怎么产生的BFC是没有关系的。
这应该和block width
的计算(width:auto
or width: fit-content
)、float
元素流出normal flow
以及float
与之后的bfc
不会发生重叠有关。
行为1:
float
元素是流出normal flow
,故后来的在normal flow
中元素应该会视它不存在。- 和
float
相邻的bfc不会和该float
元素的margin-box
有重叠(设置上面图片的margin-right: 10px
的话,图片的右侧和右侧的bfc之间会有空隙。)。 既要视该
float
元素不存在又不和它重叠,该bfc会变窄,变窄到占据该行剩余的宽度。前提应该是
width
为auto
的时候。- 此时,
'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
,这里的width of containing block
的值应该会自动变小,其他不设置都为0,进而width
最终的计算值会变小。(如何你设置padding的话,会发现文字空间会被挤得更窄) - 如果该元素的最小内容宽度溢出了,也不会换行的,此时设置了
overflow:hiddden
溢出的部分被隐藏了。
- 此时,
如果我们显示设置了
width
的值话,是不会自动调节它的大小的。如果设置width
为200px
,此时剩余空间不足,当前行溢出?or移到下一行?- 应该会移动到下一行,这样显示的话比溢出的效果要好。
相关文档规定:css2 9.4.1 block formatting context
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)
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. ""If necessary, implementations should clear
the said element by placing it below any preceding floats, but may place it adjacent to such floats if there is sufficient space.They may even make the border box of said element narrower than defined by section 10.3.3. CSS2 does not define when a UA may put said element next to the float or by how much said element may become narrower.
行为2:
相关官方规定:css2 blockwidth Floating, no-replaced elements
If 'width' is computed as 'auto', the used value is the "shrink-to-fit" width.Calculation of the shrink-to-fit width is similar to calculating the width of a table cell using the automatic table layout algorithm. Roughly: calculate the preferred width by formatting the content without breaking lines(解读:应该说的是最大内容宽度
) other than where explicit line breaks occur, and also calculate the preferred minimum width(解读:应该说的是最小内容宽度
), e.g., by trying all possible line breaks. CSS 2.1 does not define the exact algorithm. Thirdly, find the available width(解读:这里应该就是包含块的大小,300px,这里没有变窄?
): in this case, this is the width of the containing block minus the used values of 'margin-left', 'border-left-width', 'padding-left', 'padding-right', 'border-right-width', 'margin-right', and the widths of any relevant scroll bars.Then the shrink-to-fit width is:min(max(preferred minimum width, available width), preferred width)(解读:当文本比较长的时候,最后取值应该是available width 。当文本较短的时,最后取值应该是preferred with。
shrink-to-fit width应该和
width:fit-content是一样的吧?长度算好了,就看它在哪一行了,当前行还是另起一行?另一个解答中说明该问题。)
.
回复

适合作为回答的
- 经过验证的有效解决办法
- 自己的经验指引,对解决问题有帮助
- 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
- 询问内容细节或回复楼层
- 与题目无关的内容
- “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容