As far as I know, overflow: hidden
and display:inline-block
can both establish a new blocking format context,and blocking format context can prevent margin collapse.
But overflow:hidden
can’t prevent margin collapsing between sibling divs as display:inline-block
.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.div1 {
background-color: red;
margin-bottom: 100px;
}
.div2 {
background-color: orange;
margin-top: 200px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="div1">div1</div>
<div class="div2">div2</div>
</body>
</html>
I wished the overflow:hidden
on .div2 can establish a block format context and the margin between .div1 and .div2 should be 300px. But it didn't work. Why?