How can we solve the problem of overlay margins ?, Outer margin overlay
First
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "Author" content = "Hu Chao">
<Title> super Hu </title>
<Style>
# A1 {
Width: 200px;
Height: 200px;
Border: 1px solid red;
Margin-bottom: 20px;
}
# A2 {
Margin-top: 20px;
Width: 100px;
Height: 100px;
Display: inline-block;/* or absolute position (position: absolute) or floating element */
Border: 1px solid black;
}
</Style>
</Head>
<Body>
<Div id = "a1">
</Div>
<Div id = "a2">
</Div>
</Body>
Second
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "Author" content = "Hu Chao">
<Title> super Hu </title>
<Style>
# Container {
Overflow: hidden;
Zoom: 1;
}
# A1 {
Border: 1px solid red;
Width: 200px;
Height: 200px;
Background: green;
Margin-bottom: 20px;
}
# A2 {
Border: 1px solid red;
Width: 200px;
Height: 200px;
Background: red;
Margin-top: 20px;
}
</Style>
</Head>
<Body>
<Div id = "container">
<Div id = "a1">
</Div>
</Div>
<Div id = "container">
<Div id = "a2">
</Div>
</Div>
</Body>
</Html>
How can we prevent the upper and lower margin elements from being folded?
1.The margin of floating elements, inline-block elements, and absolute positioning elements are not folded with the margin of other elements in the vertical direction.
(Note that this refers to the upper and lower adjacent elements)
2.A block-level formatting context element is created, instead of margin folding with its child elements. Method 2: Put the two <div> into two BFC (IE7-triggering hasLayout) in the parent container. yes
What we need to create BFC is not the two <div> but the parent-level container where they are located.
You can trigger block formatting context by using the following method:
1. float is not set to none.
2. overflow is not visible
3. Set display to 'table-cell ', 'table-caption', or 'inline-Block'
4. position is neither static nor relative
5. zoom: 1. The hasLayout feature of IE will create a new block formatting context