内层div/table的margin-top影响外层div

.inner-table {
  tbody{
    tr:nth-child(odd) {
      background-color: #ffffff;
    }
    tr:nth-child(even) {
      background-color: #f9f9f9;
    }
  }

  margin: 20 auto;
  width:90%;
}
div.mini-table{
  background-color:#ebf5fd;
  /*overflow:hidden; 可以避免外边距合并但影响箭头显示*/
  position:relative;
  padding-top:0.5px; /** avoid 外边距合并 **/
  &:after{
    bottom: 100%;
    left: -20px;
    border: solid transparent;
    content: " ";
    height: 0;
    width: 0;
    position: absolute;
    border-bottom-color: #bf1515;
    border-width: 10px;
  }
}

本来是要给内层table加margin, margin div 并没有起效果,反而是外层div边距出现问题。

内层div的margin-top影响外层div——引出外边距合并Collapsing margins

作者:zccst

今天才算是了解边距合并。正如一位前辈所言,每一个CSS的坑,都让你学到不少知识。

<style type="text/css">
body { margin:0; padding:0;}
div { margin:0; padding:0;}
.a { width:500px; margin:0px auto 30px; background:#eee;}
.d1 { height:100px; width:500px; margin-top:30px; margin-bottom:30px; }
.d2 { height:100px; width:500px; margin-top:30px; margin-bottom:30px; background-color:blue;}

</style>

<div class="a">
    <div class="d1"></div>
    <div class="d2"></div>
</div>

<style type="text/css">
.b { /*height:200px; background:#ddd;*/ margin-top:50px; margin-bottom:50px;}

</style>
<div class="b"></div>

/*
出现原因是符合垂直边距合并的条件。
(No.6)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.
顶部发生合并,也就是本文的原因。注意发生条件是:外部div没有border,没有padding,而且与第一个子div之间没有任何间隔

消除办法:

一、字面直观解决办法
1,给外面div加padding-top:1px可以解决。
对于margin-top分三种情况:
(1)两个margin-top都是正数。margin-top:1px不行,原因是合并取其大。
(2)一正一负。如margin-top:-30px;两者互相抵消。离顶部距离等于30-(40/30/20),如果是-40之类的话,顶部一部分被隐藏了
(3)两个都是负值。
2,给外面div加border:1px solied transparent;

另外,如果在内层div的上面有内容的话,也是正常的。由于这是不可能的,所以就没有单列一条。

二、非字面解决办法

3,为父元素添加float:left可以。但是仅设置子元素float:left不行。
(No.1)Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children).

[4],为父元素添加overflow:hidden;样式即可(完美)。overflow可以的原因是生成了新的块元素。
(No.2)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.

5,为父元素添加position:abosulte可以,relative不行。子元素设置position任何值都不行。
(No.3)Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).

6,为子元素加display:inline-block也可以。
(No.4)Margins of inline-block boxes do not collapse (not even with their in-flow children).

疑问:float:left和position:absolute时上部有很大的空隙

底部也会发生类似的合并。
(No.7)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.

-------------------- 两个兄弟元素之间的margin合并情况 -----------------------------
(No.5)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.(两个<div></div>其它内容<div></div>)

-------------------- 一个内部元素自身与in-flow子孙的margin合并的情况 -----------------------------
(No.8)
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.
结论:与测试事实不符。

In this specification, the expression collapsing margins 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. 所有毗邻的两个或更多盒元素的margin将会合并为一个margin共享之。毗邻的定义为:同级或者嵌套的盒元素,并且它们之间没有非空内容、Padding或Border分隔。
简单讲:根据规范,一个盒子如果没有上补白和上边框,那么它的上边距应该和其文档流中的第一个孩子元素的上边距重叠

*/

官方文章:

8.3.1 Collapsing margins

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.

Adjoining vertical margins collapse, except:

  • Margins of the root element‘s box do not collapse.
  • If the top and bottom margins of an element with clearance are adjoining, its margins collapse with the adjoining margins of following siblings but that resulting margin does not collapse with the bottom margin of the parent block.

Horizontal margins never collapse.

Two margins are adjoining if and only if:

  • both belong to in-flow block-level boxes that participate in the same block formatting context
  • no line boxes, no clearance, no padding and no border separate them (Note that certain zero-height line boxes (see 9.4.2) are ignored for this purpose.)
  • both belong to vertically-adjacent box edges, i.e. form one of the following pairs:
    • top margin of a box and top margin of its first in-flow child
    • bottom margin of box and top margin of its next in-flow following sibling
    • bottom margin of a last in-flow child and bottom margin of its parent if the parent has ‘auto‘ computed height
    • top and bottom margins of a box that does not establish a new block formatting context and that has zero computed ‘min-height‘, zero or ‘auto‘ computed ‘height‘, and no in-flow children

A collapsed margin is considered adjoining to another margin if any of its component margins is adjoining to that margin.

Note. Adjoining margins can be generated by elements that are not related as siblings or ancestors.

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.

When two or more margins collapse, the resulting margin width is the maximum of the collapsing margins‘ 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 maximum of the absolute values of the adjoining margins is deducted from zero.

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.

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.

时间: 2024-10-27 12:38:56

内层div/table的margin-top影响外层div的相关文章

[CSS][转载]内层div的margin-top影响外层div

参考 内层div的margin-top影响外层div——引出外边距合并 div嵌套导致子区域margin-top失效不起作用的解决方法 我使用的是在外层的div中添加 border: 1px solid transparent; 在上面参考方法中,给外层div 添加"overflow:hidden;"也实现了同样的效果.

div嵌套引起的内层margin-top对外层div起作用

嵌套div中margin-top转移问题的解决办法 在这两个浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-bottom的值会“转移”给外层div. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&

外层div随内层div高度自适应

首先说一下textarea的高度随文字的内容自适应,用div模拟textarea.直接看代码.其中 contenteditable="true"表示div可以编辑..主要是设置 overflow: auto; <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport"

外层div自适应内层div高度

最近在做项目的时候,写jsp页面,在显示一些数据时不知道数据的多少,这时候就需要外层div自适应内层div的高度. 在网上找了些方法,也是本人现在用的解决方法,这里只介绍一种,当然还有很多种解决方法. 如: 1 <div id="div_1"> 2 <div id="div_1_1"></div> 3 <div id="div_1_2"></div> 4 <div id="

div table 属性

div:1.基本属性:width, height, left, top, background-color(当设置为transparent时表示透明)2.定位属性:position: absolute/relativeabsolute时,位置固定:relative时,位置会随着内容的实际情况进行浮动3.显示属性:display: block/noneblock为默认状态,表示显示:none为隐藏4.优先属性:z-index:nn表示一个整数(正负均可),有多个Div时n越大,则越靠前显示:z-i

css - 防止float浮动效果影响下面div布局

转载:http://blog.csdn.net/u012246458/article/details/39025429 让页面的DIV布局向左或者向右有两种方法: 一:为了防止float,min-height等具有浮动效果的代码影响上下的div样式布局,可以要加上一个空白的div,其css属性为clear: <div class="clear"></div> 其 css要用 : .clear { clear: both; } 实例: html代码: <di

2017 League Table of the top 200 Universities in Asia

Asia ranking World Rank University Det. Country Presence Rank* Impact Rank* Openness Rank* Excellence Rank* ranking World Rank University Det. Country Presence Rank* Impact Rank* Openness Rank* Excellence Rank* 1 48 University of Tokyo / 東京大学 94 79 1

坑爹小问题-table改变frame动画影响cell

反正就是很坑爹.简单说就是这样吧,做一个简单的聊天界面,对话框左一个右一个那样子.(界面可以脑补微信)底下的键盘弹起来时候,展示对话的table的frame要相应的缩小,隐藏键盘又要恢复.就这么简单一个代码: 1 - (void)keyboardFrameChange:(NSNotification *)sender 2 { 3 NSDictionary *userInfo = sender.userInfo; 4 CGRect keyboardFrame; 5 [[userInfo value

子div块中设置margin-top时影响父div块位置的解决办法

在css中设置样式时,通常会遇到用子div块margin中设置margin-top时,父div块中就会随着子div的margin-top,也会和子div执行相同的margin-top的位置样式 解决办法1: 若子div块中使用margin-top,则在父div块中添加:overflow:hidden; 解决办法2: 若由于特殊情况不能在父div块中添加:overflow:hidden,那么在子div块中用padding-top代替margin-top;