Collapsing Margins

本文解析了CSS中边距重叠(margin collapsing)的现象,包括其定义、发生条件及解决方法,并针对IE浏览器的特殊情况提供了应对策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/professorsheep/article/details/6302962

前端开发中总会遇到这样那样的问题,而margin collapsing(我译为边距重叠)总会给你带来惊喜:或者是需要紧连着的元素怎么中间突然多了一块空白;或者是我需要边距的地方它给我去除了。这些都源于CSS的margin collapsing。

     首先熟悉一下margin collapsing的概念,下面是W3G CSS 2.1的定义:

8.3.1 

In this specification, the expression  means that adjoining margins (no non-empty content, padding or border areas or clearance separate them) of two or more boxes (which may be next to one another or nested) combine to form a single margin.

In CSS 2.1,  never collapse.

 may collapse between certain boxes:

  • Two or more adjoining vertical margins of block-level boxes in the normal flow collapse. The resulting margin width is the maximum of the adjoining margin widths. In the case of negative margins, the maximum of the absolute values of the negative adjoining margins is deducted from the maximum of the positive adjoining margins. If there are no positive margins, the absolute maximum of the negative adjoining margins is deducted from zero. Note. Adjoining boxes may be generated by elements that are not related as siblings or ancestors.

         以普通布局邻接的盒元素按照两者间最大的margin 重叠,如果有一个盒元素是负值,则结果margin为两者相加的绝对值,如果都是负值,则为0

  • Vertical margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).

         float 盒元素与其他盒间的margin不重叠

  • Vertical margins of elements that establish new block formatting contexts (such as floats and elements with 'overflow' other than 'visible') do not collapse with their in-flow children.

        新建的块结构和内部子元素不重叠

  • Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).

        absolute 布局的盒元素不重叠margin

  • Margins of  elements do not collapse (not even with their in-flow children).

        inline-block 不

  • If the top and bottom margins of a box are adjoining, then it is possible for margins to collapse through it. In this case, the position of the element depends on its relationship with the other elements whose margins are being collapsed.
    • If the element's margins are collapsed with its parent's top margin, the top border edge of the box is defined to be the same as the parent's.
    • Otherwise, either the element's parent is not taking part in the margin collapsing, or only the parent's bottom margin is involved. The position of the element's top border edge is the same as it would have been if the element had a non-zero bottom border.

                如果元素的与它的父元素的上边距重叠,那么它的上边框和它父节点的上边框相同

                否则,或者是元素不与父元素重叠,或者只和父元素的下边距重叠,它的上边框和它的下边框相同

 

An element that has clearance never collapses its top margin with its parent block's bottom margin.

               有间隙的元素的上边距与父元素的下边距不重叠

Note that the positions of elements that have been collapsed through have no effect on the positions of the other elements with whose margins they are being collapsed; the top border edge position is only required for laying out descendants of these elements.

  • Margins of the root element's box do not collapse.

          根元素的边距不重叠

The bottom margin of an in-flow block-level element is always adjoining to the top margin of its next in-flow block-level sibling, unless that sibling has clearance.

         块级 in-flow 元素的下边距总是与下一个邻接的块级元素重叠,除非它有间隙

The top margin of an in-flow block box is adjoining to its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.


        块级 in-flow元素的上边距与它的第一个子元素的上边距重叠,前提是它没有上边框,上间距,和子元素没有间隙

The bottom margin of an in-flow block box with a 'height' of 'auto' is adjoining to its last in-flow block-level child's bottom margin if the element has no bottom padding or border.


       高度设为:auto 的块级 in-flow 元素的下边距与它的最后一个子元素的下边距重叠,前提是无边框,无间距

 

An element's own margins are adjoining if the 'min-height' property is zero, and it has neither top or bottom borders nor top or bottom padding, and it has a 'height' of either 0 or 'auto', and it does not contain a line box, and all of its in-flow children's margins (if any) are adjoining.

       可邻接的边距是指该元素的‘min-height’为0,并且无上下边框,无上下间距,‘height’设置为0或者是auto,不能包含有line box,并且它的所有子元素的边距必须是邻接的

When an element's own margins collapse, and that element has clearance, its top margin collapses with the adjoining margins of subsequent siblings but that resulting margin does not collapse with the bottom margin of the parent block.

       当元素有间隙,它能与紧接着的元素重叠,但是不能与父节点的下边距重叠

Collapsing is based on the used value of 'padding''margin', and 'border' (i.e., after resolving any percentages). The collapsed margin is calculated over the used value of the various margins.

 

Margin Collapsing

我们来看一个例子:

 

这是一段简单的html代码,为这段代码配上些简单的样式:

 

我们希望的效果是h1 到它的父元素div#masthead 的上边距应该为20px,但是效果却是:

margin

                              图 1 masthead 的样式

实际上,h1与div的上边距远没有20px,这是为什么呢?

而这些都是margin collapsing搞的怪,我们原本希望看到的样式是这样的:

margin

                            图2 masthead 的修正样式

灰色虚线表示div的margin边界,红色虚线表示h1的margin边界,蓝色虚线表示p 的margin边界,可以发现,h1的上边距与div的重叠了,最后显示的是div的上边距,同理h1与p。

 

Uncollapsing

 

那如何使边距不重合呢,前面提到的W3C的CSS2.1 定义了几种情况,简单为元素添加边框或者间距(border or padding)就可以不使边距重叠。我们为div#masthead 添加一个小的间距:

 

margin

                              图3 这才是我们需要的样式

 

 

IE7-/Win: Margin collapsing and hasLayout

IE的haslayout是个很纠结的东西,Internet Explorer 中有很多奇怪的渲染问题可以通过赋予其”layout”得到解决。“Layout”是一个 Internet Explorer for Windows的私有概念,它决定了一个元素如何显示以及约束其包含的内容、如何与其他元素交互和建立联系、如何响应和传递应用程序事件、用户事件等。这种渲染特性可以通过某些 CSS 属性被不可逆转地触发。而有些 HTML 元素则默认就具有”layout”。

 

 

In IE7-/Win this type of margin collapsing (between a box and its first/last child) is badly affected by the hasLayout property of the boxes.


             在IE7-/Win 类型中,边距重叠被hasLayout 属性干扰了

  •  A hasLayout outer box always prevents margins on the inner box to stick out of it. In absence of margin/padding this is wrong.

             hasLayout 外部盒阻止内部盒与它重叠,这时缺少margin/padding 是错误的

  •  A hasLayout inner box inside a non hasLayout one always has its bottom margin sticking out of the outer box. In presence of margin/padding this is wrong. This can be fixed by an empty element between the closing tags of the two boxes.

non-hasLayout盒中的hasLayout内部盒,下边距总是与它的外部盒重叠,使用margin/padding 是错误的。在两个盒间加一个空元素能够修复错误

  •  In the case of a hasLayout box inside a non hasLayout one with a top border or padding there may also be loss of margins. This behavior is dependent on presence/absence of white space and/or comments between the opening tags of the two boxes. The rows marked with "& comment" have a comment suitably placed in the markup to fix this loss of margins.

            在non-hasLayout盒中的hasLayout内部盒,如果外部盒有上边框或者间距,那么它的边距可能会丢失,这取决于两个盒之间是否有空白或者注释

  •  In the case of a hasLayout outer box, its top padding collapses with the top margin of the inner box, this is always wrong since padding and margin should never collapse.

            在hasLayout 外部盒中,它的上间距与内部盒的上边距重叠,但这是错误的,因为边距与间距不能重叠

详细说明可参见 IE7-/Win: hasLayout and margins of nested boxes


参考资料:On having layout


资源下载链接为: https://pan.quark.cn/s/140386800631 通用大模型文本分类实践的基本原理是,借助大模型自身较强的理解和推理能力,在使用时需在prompt中明确分类任务目标,并详细解释每个类目概念,尤其要突出类目间的差别。 结合in-context learning思想,有效的prompt应包含分类任务介绍及细节、类目概念解释、每个类目对应的例子和待分类文本。但实际应用中,类目和样本较多易导致prompt过长,影响大模型推理效果,因此可先通过向量检索缩小范围,再由大模型做最终决策。 具体方案为:离线时提前配置好每个类目的概念及对应样本;在线时先对给定query进行向量召回,再将召回结果交给大模型决策。 该方法不更新任何模型参数,直接使用开源模型参数。其架构参考GPT-RE并结合相关实践改写,加入上下文学习以提高准确度,还使用BGE作为向量模型,K-BERT提取文本关键词,拼接召回的相似例子作为上下文输入大模型。 代码实现上,大模型用Qwen2-7B-Instruct,Embedding采用bge-base-zh-v1.5,向量库选择milvus。分类主函数的作用是在向量库中召回相似案例,拼接prompt后输入大模型。 结果方面,使用ICL时accuracy达0.94,比bert文本分类的0.98低0.04,错误类别6个,处理时添加“家居”类别,影响不大;不使用ICL时accuracy为0.88,错误58项,可能与未修改prompt有关。 优点是无需训练即可有较好结果,例子优质、类目界限清晰时效果更佳,适合围绕通用大模型api打造工具;缺点是上限不高,仅针对一个分类任务部署大模型不划算,推理速度慢,icl的token使用多,用收费api会有额外开销。 后续可优化的点是利用key-bert提取的关键词,因为核心词语有时比语意更重要。 参考资料包括
内容概要:本文详细介绍了哈希表及其相关概念和技术细节,包括哈希表的引入、哈希函数的设计、冲突处理机制、字符串哈希的基础、哈希错误率分析以及哈希的改进与应用。哈希表作为一种高效的数据结构,通过键值对存储数据,能够快速定位和检索。文中讨论了整数键值和字符串键值的哈希方法,特别是字符串哈希中的多项式哈希及其优化方法,如双哈希和子串哈希的快速计算。此外,还探讨了常见的冲突处理方法——拉链法和闭散列法,并提供了C++实现示例。最后,文章列举了哈希在字符串匹配、最长回文子串、最长公共子字符串等问题中的具体应用。 适合人群:计算机科学专业的学生、算法竞赛选手以及有一定编程基础并对数据结构和算法感兴趣的开发者。 使用场景及目标:①理解哈希表的工作原理及其在各种编程任务中的应用;②掌握哈希函数的设计原则,包括如何选择合适的模数和基数;③学会处理哈希冲突的方法,如拉链法和闭散列法;④了解并能运用字符串哈希解决实际问题,如字符串匹配、回文检测等。 阅读建议:由于哈希涉及较多数学知识和编程技巧,建议读者先熟悉基本的数据结构和算法理论,再结合代码实例进行深入理解。同时,在实践中不断尝试不同的哈希策略,对比性能差异,从而更好地掌握哈希技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值