最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Css外边距折叠(collapsedmargin)浅析_html/css

来源:动视网 责编:小采 时间:2020-11-27 16:06:45
文档

Css外边距折叠(collapsedmargin)浅析_html/css

Css外边距折叠(collapsedmargin)浅析_html/css_WEB-ITnose:Css 外边距折叠(collapses margin ) a.先来看看w3c 文档对于外边距折叠的定义: In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a sing
推荐度:
导读Css外边距折叠(collapsedmargin)浅析_html/css_WEB-ITnose:Css 外边距折叠(collapses margin ) a.先来看看w3c 文档对于外边距折叠的定义: In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a sing


Css 外边距折叠(collapses margin )

a.先来看看w3c 文档对于外边距折叠的定义:

In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

翻译一下:在Css中,相邻盒子(有可能是同辈元素,也可能不是)的外边距可以合并成为一个外边距。 外边距的这种合并方式称为 折叠,这种合并过的外边距称为 外边距折叠。

举个例子:

 div1 div2

运行结果如下图。从图中我们可以看出来,div1 的margin-bottom和 div2 的margin-top 合并了,结果 div1和div2 之间只有10px。我们本来是要他们之间间隔20px,如何实现呢?可以给div1 的class中添加:display:inline-block。 这就涉及到如何解决或避免外边距折叠的问题了。

下面来看看w3c文档的具体注意事项:

Note the above rules imply that:

  • Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).
  • 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).
  • Margins of inline-block boxes do not collapse (not even with their in-flow children).
  • The bottom margin of an in-flow block-level element always collapses with the top margin of its next in-flow block-level sibling, unless that sibling has clearance.
  • The top margin of an in-flow block element collapses with 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.
  • The bottom margin of an in-flow block box with a 'height' of 'auto' and a 'min-height' of zero collapses with its last in-flow block-level child's bottom margin if the box has no bottom padding and no bottom border and the child's bottom margin does not collapse with a top margin that has clearance.
  • A box's own margins collapse 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) collapse.
  • 1. 浮动的盒子和其它盒子之间不会发生折叠。(甚至 浮动的盒子和它的正常流中子元素之间 也不会 发生折叠). 举个例子,下面例子中 父元素 和子元素 之间 会发生外边缘折叠,这时可以父元素或子元素 设置成浮动,就可以显示正常。

      div1

    2. 为元素的外边距 建立一个新的 block formatting contexts(块级格式上下文),这样就不会和 它的正常流中的子元素发生折叠。如上面的例子中,可以在外面的div 添加一个属性overflow:hidden

    3. 绝对定位的盒子不会发生折叠。上面的例子中,可以给父元素或者子元素 添加属性:position:absolute 或者 fixed.

    4. 在一个正常的文档流中,块级元素的底部外边距 回和它的后面的兄弟元素的 上边距发生折叠,除非 你设置了 clearance。见文章顶部的例子。

    5. 块级元素的 top margin 会和它的第一个块级元素的子元素的 top margin 发生外边距折叠,如果这个块级元素没有设置 top border, no top padding ,子元素没有设clearance. 举例:tip 1 中已经有了这个例子。

    6. 块级元素的bottom margin 有可能会和它的最后一个块级子元素的 bottom margin 发生外边距折叠,如果 这个块级元素没有设置 bottom padding,bottom border 并且 height:auto, min-height:0

    7. 这样的盒子会外边距折叠 , min-height:0, 并且没有设置 top or bottom borders 或者 top or bottom padding, 并且height:0 or auto 并且它不包含 line box 并且 它所有的子元素外边距折叠。

    详细信息请参考w3c文档: http://www.w3.org/TR/CSS2/box.html#collapsing-margins

    文档

    Css外边距折叠(collapsedmargin)浅析_html/css

    Css外边距折叠(collapsedmargin)浅析_html/css_WEB-ITnose:Css 外边距折叠(collapses margin ) a.先来看看w3c 文档对于外边距折叠的定义: In CSS, the adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a sing
    推荐度:
    标签: html 边距 htm
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top