Document directory
- Margin collapsing
- Margin collapsing is irrelevant to the Direction
- Margin collapsing only occurs in BFC
- Margin collapsing may span Elements
- Margin collapsing cannot be folded with padding (padding)
- Margin collapsing cannot match BFC
I have read the new book translated by Teacher Li songfeng, "CSS designer Guide (3rd edition" margin overlay part (http://www.ituring.com.cn/article/16969) is really a little anxious, this place is really some not written in place, there are also errors (for example, "Only vertical margin is superimposed, and horizontal margin is not superimposed ")
Margin collapsing
BFC (block formatting context) is adjacent to two block-level boxes. The bottom margin of the previous box is superimposed with the top margin of the next box, that is, the two are the largest, rather than the sum:
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px } #blue { margin:10px 10px 10px 10px } #red { margin:10px 10px 10px 10px }</style>Margin collapsing is irrelevant to the Direction
In the CSS designer Guide (version 3rd), it is wrong to say that "only vertical margins are superimposed, while horizontal margins are not superimposed, whether the two boxes are overlapped depends entirely on whether they are located in two adjacent boxes of the same BFC, regardless of the horizontal vertical. In the following example, the writing-mode of the body is changed, so the stack is in the horizontal direction (only visible to ie)
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px } #blue { margin:10px 10px 10px 10px } #red { margin:10px 10px 10px 10px }
body {
writing-mode:tb-rl;
}</style>Margin collapsing only occurs in BFC
Margin collapsing does not occur in IFC
When the display attribute of an element is set to inline-block, they are all in the IFC, so they will not overlap either horizontally or vertically:
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px;
display:inline-block; } #blue { margin:10px 10px 10px 10px;
display:inline-block; } #red { margin:10px 10px 10px 10px;
display:inline-block; }
</style>Margin collapsing does not occur in floats
When the float attribute of elements is set to left, they are not in the normal stream, and are not in the BFC. Similarly, they will not overlap in both the horizontal and vertical directions:
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px;
float:left; } #blue { margin:10px 10px 10px 10px;
float:left; } #red { margin:10px 10px 10px 10px;
clear:left;
float:left; }
</style><div id="red" style="background:pink;height:100px;width:100px;"></div></body>
Margin collapsing may span Elements
When a container has neither border nor padding, the margin of its first sub-box can also overlap with the margin of its previous container:
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px;
} #blue { margin:5px 10px 10px 10px;
} #red { margin:10px 10px 10px 10px;
}
</style> <div id="red" style="background:pink;height:100px;width:100px;"></div>
</div></body>Margin collapsing cannot be folded with padding (padding)
When padding (padding) exists, the previous section does not occur:
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px;
} #blue { margin:10px 10px 10px 10px;
padding:10px 10px 10px 10px;
} #red { margin:10px 10px 10px 10px;
}
</style> <div id="red" style="background:pink;height:100px;width:100px;"></div>
</div></body>
Margin collapsing cannot match BFC
When a container creates a new BFC inside it, cross-element folding does not occur.
Run
<!doctype HTML> #green { margin:10px 10px 10px 10px;
} #blue { margin:10px 10px 10px 10px;
overflow:hidden;
} #red { margin:10px 10px 10px 10px;
}
</style> <div id="red" style="background:pink;height:100px;width:100px;"></div>
</div></body>