Inner layer div/table margin-top influence outer div

Source: Internet
Author: User

. 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; You can avoid the margin merge but affect the arrow display*/position:relative;Padding-top:0.5px;/** Avoid margin consolidation **/&:after{bottom:100%; Left:-20px;Border:Solid Transparent;content: " ";Height:0;width:0;position:Absolute;Border-bottom-color:#bf1515;Border-width:10px; }}

Originally is to give the inner layer table plus margin, margin Div does not have the effect, but the outer div margin problems.

The margin-top of the inner Div affects the outer div--collapsing margins

Zccst

It's only today that we know about margin merging. As one of my predecessors put it, every CSS pit makes you learn a lot of knowledge.

<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>


/*
The cause is a condition that conforms to the vertical margin merge.
(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 Have no top border, no top padding, and the child have no clearance.
The top of the merge, which is why this article. Note that the external div has no border, no padding, and there are no gaps between the first sub-div

Elimination Method:

First, the literal visual solution
1, to the outside Div plus padding-top:1px can solve.
For margin-top in three different situations:
(1) Two margin-top are positive. MARGIN-TOP:1PX is not good, the reason is that the merger to take its large.
(2) One positive and one negative. As margin-top:-30px, the two cancel each other out. The distance from the top is equal to 30-(40/30/20), and if it is-40, the top part is hidden.
(3) two are negative.
2, give the outside Div plus border:1px solied transparent;

Also, if there is content on the inside Div, it is normal. Since this is not possible, there is no single line.

Ii. Non-literal solutions

3, add float:left to the parent element. But setting only child elements float:left not.
(The most) Margins between a floated box and any other box does not collapse (not even between a float and its in-flow children).

[4], add Overflow:hidden for the parent element, style (perfect). Overflow can be caused by the creation of new block elements.
(No.2) Margins of elements that establish new block formatting contexts (such as floats and elements with ' overflow ' other than ' Visible ') does not collapse with their in-flow children.

5, add Position:abosulte to the parent element, relative not. The child element is not set position any value.
(No.3) Margins of absolutely positioned boxes do not collapse (not even with their in-flow children).

6, for child elements plus display:inline-block can also.
(No.4) Margins of Inline-block boxes do not collapse (not even with their in-flow children).

Question: There is a big gap in the upper part of Float:left and Position:absolute


Similar merges occur at the bottom.
(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 a no bottom padding and no bottom border and the child's bottom margin Does not collapse with a top margin, which has clearance.


--------------------a margin merger between the two sibling elements-----------------------------
(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 s Ibling, unless that sibling have clearance. (Two <div></div> other content <div></div>)

--------------------a situation in which the inner element itself is merged with the In-flow descendants ' margin-----------------------------
(No.8)
A box ' s own margins collapse if the ' Min-height ' property was zero, and it has neither top or bottom borders nor top or bot Tom 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 child Ren ' s margins (if any) collapse.
Conclusion: Inconsistent with the test facts.


In this specification, the expression collapsing margins means that adjoining margins (no non-empty content, padding or bo Rder areas or clearance separate them) of one or more boxes (which May is next to one another or nested) combine to form a Single margin. The margin of all adjacent two or more box elements will be merged into a margin share. Adjacent definitions are: sibling or nested box elements, and there is no non-empty content, padding, or border separation between them.
Simple: According to the specification, if a box does not have a filler and a top border, then its top margin should overlap with the top margin of the first child element in its document flow


*/

Official articles:

8.3.1 Collapsing margins

In CSS, the adjoining margins of both or more boxes (which might or might is siblings) can combine to form a single mar Gin. Margins that combine this-a-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 does not collapse.
    • If the top and bottom margins of an element with clearance is adjoining, its margins collapse with the adjoining margins Of following siblings but this resulting margin does not collapse with the bottom margin of the parent block.

Horizontal margins never collapse.

Margins is adjoining if and only if:

  • Both belong to In-flow block-level boxes this participate in the same block formatting context
  • No Line boxes, no clearance, no padding and no border separate them (Note this certain Zero-height line boxes (see 9.4 .2) is 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 their parent if the parent has ' auto ' computed height
    • Top and bottom margins of a box that does no establish a new block formatting context and that have 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 are adjoining to that margin.

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

note The above rules imply that:

  • Margins between a floated box and any other box does 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 th An ' visible ') does 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 s Ibling, unless that sibling had 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 Have no top border, no top padding, and the child have 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 a no bottom padding and no bottom border and the child ' s Botto M margin does not collapse with a top margin , which has clearance.
  • A box ' s own margins collapse if the ' Min-height ' property was 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-FL ow children ' s margins (if any) collapse.

When both 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 are deducted from the Maximum of the positive adjoining margins. If There is no positive margins, the maximum of the absolute values of the adjoining margins was deducted from zero.

If the top and bottom margins of a box are adjoining, then it's 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 is being coll Apsed.

    • If the element ' s margins is collapsed with its parent's top margin, the top border edge of the box is defined to be the S Ame as the parent ' s.
    • Otherwise, either the element ' s parent is not a 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 has been if the element had a Non-zero bottom Borde R.

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

Inner layer div/table margin-top influence outer div

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.