Css margin overlay

Source: Internet
Author: User
Tags xmlns

W3c believes that the margin stack is reasonable. Whether it is a bug or not, but this problem does exist. Let's look at the example below:

 
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  5. </Head>
  6. <Style type = "text/css">
  7. * {Padding: 0; margin: 0 ;}
  8. . Box {background: # ccc ;}
  9. . Content {background: # eee; margin: 20px auto ;}
  10. . An-box {margin: 50px auto; background: #999999 ;}
  11. </Style>
  12. </Head>
  13. <Body>
  14. <Div class = "box">
  15. <Div class = "content"> margin: 20px auto </div>
  16. <Div class = "content"> margin: 20px auto </div>
  17. </Div>
  18. <Div class = "box">
  19. <Div class = "content"> margin: 20px auto </div>
  20. </Div>
  21. <Div class = "box">
  22. <Div class = "content"> margin: 20px auto </div>
  23. </Div>
  24. <Div class = "an-box"> margin: 50px auto </div>
  25. </Body>
  26. </Html>

There are many ways to solve this problem. Let's take a look at this. In the outer container, add overflow: hidden; zoom: 1;

 

 
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  5. </Head>
  6. <Style type = "text/css">
  7. * {Padding: 0; margin: 0 ;}
  8. . Box {background: # ccc; overflow: hidden; zoom: 1}
  9. . Content {background: # eee; margin: 20px auto ;}
  10. . An-box {margin: 50px auto; background: #999999 ;}
  11. </Style>
  12. </Head>
  13. <Body>
  14. <Div class = "box">
  15. <Div class = "content"> here there are two overlapping margin: 20px auto </div>
  16. <Div class = "content"> here there are two overlapping margin: 20px auto </div>
  17. </Div>
  18. <Div class = "box">
  19. <Div class = "content"> margin: 20px auto </div>
  20. </Div>
  21. <Div class = "box">
  22. <Div class = "content"> margin: 20px auto </div>
  23. </Div>
  24. <Div class = "an-box"> margin: 50px auto </div>
  25. </Body>
  26. </Html>

In addition, the border attribute is added to the outer container, and the empty block element is added to the overlay element .. Also, we use padding to replace margin.

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.