Margin overlap between parent and child div

code show as below:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <style>
  #father{
  width:500px;
  height:500px;
  background:pink;
  margin:10px;
  


  }
  #son{
   width:200px;
   height:200px;
   background:gray;
   
   margin:10px;
  }
  </style>
 </head>
 <body>
  <div id="father">
  <div id="son"></div>
  </div>
 </body>
</html>

The effect of starting the browser is as follows, and it is found that the margins set by the parent and child divs overlap.

Reason: All sibling or nested box elements have overlapping margins

Solution:

1. Modify the height of the parent element and increase the padding-top style simulation (padding-top: 1px; commonly used) 
2. Add overflow: hidden to the parent element; the style is fine (perfect) 
3. Declare a float for the parent element or child element ( float: left; available) 
4. Add a border to the parent element (border: 1px solid transparent is available) 
5. Declare absolute positioning for the parent or child element

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686830&siteId=291194637