`

margin collapse

    博客分类:
  • css
阅读更多
“collapsing margins”(折叠margin)的意思是:2个或以上盒模型之间(关系可以是相邻或嵌套)相邻的margin属性(这之间不能有非空内容、padding区域、border边框或使用清除分离方法)结合表示为一个单独的margin。

在css2.1中,水平的margin不会被折叠。
垂直margin可能在一些盒模型中被折叠:
1、在常规文档流中,2个或以上的块级盒模型相邻的垂直margin会被折叠。

最终的margin值计算方法如下:

a、全部都为正值,取最大者;

b、不全是正值,则都取绝对值,然后用正值减去最大值;

c、没有正值,则都取绝对值,然后用0减去最大值。

注意:相邻的盒模型可能由DOM元素动态产生并没有相邻或继承关系。

2、相邻的盒模型中,如果其中的一个是浮动的(floated),垂直margin不会被折叠,甚至一个浮动的盒模型和它的子元素之间也是这样。
3、设置了overflow属性的元素和它的子元素之间的margin不会被折叠(overflow取值为visible除外)。
4、设置了绝对定位(position:absolute)的盒模型,垂直margin不会被折叠,甚至和他们的子元素之间也是一样
5、设置了display:inline-block的元素,垂直margin不会被折叠,甚至和他们的子元素之间也是一样。
6、如果一个盒模型的上下margin相邻,这时它的margin可能折叠覆盖(collapse through)它。在这种情况下,元素的位置(position)取决于它的相邻元素的margin是否被折叠。


a、如果元素的margin和它的父元素的margin-top折叠在一起,盒模型border-top的边界定义和它的父元素相同。

b、另外,任意元素的父元素不参与margin的折叠,或者说只有父元素的margin-bottom是参与计算的。如果元素的border-top非零,那么元素的border-top边界位置和原来一样。

一个应用了清除操作的元素的margin-top绝不会和它的块级父元素的margin-bottom折叠。

注意,那些已经被折叠覆盖的元素的位置对其他已经被折叠的元素的位置没有任何影响;只有在对这些元素的子元素定位时,border-top边界位置才是必需的。

7、根元素的垂直margin不会被折叠。


浮动的块级元素的margin-bottom总是与它后面的浮动块级兄弟元素(floated next in-flow block-level sibling)的margin-top相邻,除非那个同级元素使用了清除操作。

浮动的块级元素的margin-top和它的第一个浮动块级子元素(floated first in-flow block-level child)的margin-top相邻(如果该元素没有border-top,没有padding-top,并且子元素没有使用清除操作)。

浮动的块级元素的margin-bottom如果符合下列条件,那么它和它的最后一个浮动块级子元素的margin-bottom相邻(如果该元素没有指定padding-bottom或border):

a、指定了height:auto

b、min-height小于元素的实际使用高度(height)

c、max-height大于元素的实际使用高度(height)

如果一个元素的min-height属性设置为0,那么它所拥有的margin是相邻的,并且它既没有border-top和border-bottom,也没有padding-top和padding-bottom,它的height属性可以是0或auto,它不能包含一个内联的盒模型(line box),它所有的浮动子元素(如果有的话)的margin也都是相邻的。

当一个元素拥有的margin折叠了,并且它使用了清除操作,那么它的margin-top会和紧随其后的兄弟元素的相邻margin折叠,但结果是它的margin将无法和其块级父元素的margin-bottom折叠。

折叠操作是以padding、margin、border的值为基础的(即在浏览器解析所有这些值之后),折叠后的margin计算将覆盖已使用的不同margin的值。
分享到:
评论

相关推荐

    C++编写小游戏

    BORDER-COLLAPSE: collapse; BORDER-LEFT: #ffffff 0px solid; BORDER-RIGHT: #ffffff 0px solid; BORDER-TOP: #ffffff 0px solid; FONT-SIZE: 70%; MARGIN-LEFT: 10px } .issuetable { BACKGROUND-COLOR: #...

    仿谷歌导航(图片变换大小)

    border-collapse:collapse;} td{text-align:center;padding:0;margin:0;} form{margin:0;} .bgp a,.bgp span{display:block;cursor:pointer;} .bgp .bgp-fr{margin:0 auto;} #wrapper{text-align:ce 部分代码

    jQuery的手机端九宫格转盘抽奖代码.zip

    <... <head> <meta charset="utf-8">...meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no...border-collapse: collapse; text-align: center; }

    html转pdf,ITEXT转HTML为PDF解决中文不换行问题

    css +=" border-collapse: collapse;"; css +=" font-size: 15px;"; css +=" width: 98%;"; css +="}"; css +=""; css +="td{"; css +=" border: 1px solid #ddd;"; css +=" text-align: left;"; css +=" ...

    less 实例 z.less

    table { border-collapse:collapse; } body { color:#333; font:12px/20px "SimSun","宋体","Arial Narrow",HELVETICA; background:#fff; } a { color:#666; text-decoration:none; } a:visited { color:#666; }...

    三星9305收索

    display:inline-block}#mCon .pinyin{display:inline-block}#mCon .c-icon-chevron-unfold2{margin-left:5px}#mMenu a{width:100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:...

    【JavaScript源代码】JavaScript实现简易购物车最全代码解析(ES6面向对象).docx

    JavaScript实现简易购物车最全代码解析(ES6面向对象)  本文实例为大家分享了JavaScript实现简易购物车的具体代码,供大家参考,具体内容如下  代码: <!... position: relative;... border-collapse:

    HTML table行距的改变方法示例

    在使用HTML表的时候有时候需要我们改变行距,但是改变margin,padding,collapse等方法效果都不是很好。在这里我找到了一个实用的小技巧。利用display属性+margin来实现。 例子: tr{margin-top:0px;padding:0px;...

    HTML 网页设计

    table{border-collapse:collapse;border-spacing:0;} /* start editing from here */ a{text-decoration:none;} .txt-rt{text-align:right;}/* text align right */ .txt-lt{text-align:left;}/* text align left */...

    jQuery表格(Table)基本操作实例分析

    本文实例讲述了jQuery表格(Table)基本... border-collapse: collapse; border-spacing: 0; margin-right: auto; margin-left: auto; width: 800px; } th, td { border: 1px solid #b5d6e6; font-size: 12px;

    InspiniaAdmin 2.7 Full Project

    Fix missing .m class for general margin Fix sidebar-panel heihgt in Angular project Fix loading buttons in Angular project Fix closing secend level menu in Rails project Fix menu navigation strucutre ...

    InspiniaAdmin 2.7 Seed Project

    Fix missing .m class for general margin Fix sidebar-panel heihgt in Angular project Fix loading buttons in Angular project Fix closing secend level menu in Rails project Fix menu navigation strucutre ...

    常用的HTML+CSS标签480个(带中文解释说明)

    leftmargin="0" topmargin="0" marginheight="0" marginwidth="0"去除页面边框 list-style-type:none;去除li点 "color:red;text-decoration:none;超链接颜色下划线 html,body{ height:100%;overflow:hidden; }...

    Javascript实现基本运算器

    用Javascript实现一个基本的运算器,具体内容如下 使用表格布局,JS添加事件 <!...<... <head lang="en">... border-collapse: collapse; margin: auto; } .trr{ width: 400px; height: 1

    经常使用的两个清爽的table样式

    附上两个我经常使用的table样式: 复制代码代码如下: <... border-collapse: collapse; margin:0 auto; } td{ height:30px; } h1,h2,h3{ font-size:12px; margin:0; padding:0; } .table{ border:1px solid #ca

    java 日历程序小代码

    border-collapse: collapse; /*合并表格相临边框*/ } /* 这个日历没有进行定义的内容包括: */ #calendar td{} /* 日历单元格样式 */ #calendar a{} /* 有日志的号数样式(链接样式) */ #calendar a:hover{}...

    WEB标准中细线表格的实现方法

    随着web标准的广泛传播,表格渐渐被我遗忘,但是表格还是有它优秀的一面,数据...border-collapse:collapse;}  td,th{border:1px solid #000;border-width:0 1px 1px 0;margin:2px 0 2px 0;text-align:left;}  th{text-

    java jbuilder日历小代码

    border-collapse: collapse; /*合并表格相临边框*/ } /* 这个日历没有进行定义的内容包括: */ #calendar td{} /* 日历单元格样式 */ #calendar a{} /* 有日志的号数样式(链接样式) */ #calendar a:hover{}...

    A global Particle Swarm-Based-Simulated Annealing Optimization technique for under-voltage load shedding problem

    sensitivity at the maximum loading point or the collapse point. The voltage stability criterion is modeled directly into the load-shedding scheme. In any UVLS scheme finding the global point is very ...

    vue2.0实现列表数据增加和删除

    本文实例为大家分享了vue... border-collapse: collapse; margin: 20px auto; } table th,table td{ background: #0094ff; color: white; font-size: 16px; padding: 5px; text-align: center; border: 1px so

Global site tag (gtag.js) - Google Analytics