How can we solve the problem of overlay margins ?, Outer margin overlay

Source: Internet
Author: User

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

Related Article

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.