💪 Since ancient times, great achievements have not only been achieved with extraordinary talents, but also with great perseverance. – su shi
But first, let me Lao
Why write this blog post? Because of the overlapping problem of margin, the CSS attribute, we often encounter many problems in the development process, which will lead to chaos in the overall layout of our page. Today, through this blog post, we will study the problem of margin overlap.
Margin overlap problem
Margin overlap occurs only on block-level elements, such as
, and not on elements outside the document flow, such as when the float property is set, or when the value of position is absolute.
Margin overlap occurs
-
Margins of adjacent sibling elements overlap
The sample code looks like this:
<! DOCTYPEhtml> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="Width = device - width, initial - scale = 1.0" /> <title>Margins of adjacent sibling elements overlap</title> <style> .box { width: 600px; height: 100px; line-height: 50px; text-align: center; color: # 333; } .box1 { background-color: #f60; margin-bottom: 100px; } .box2 { background-color: #a90; margin-top: 100px; } </style> </head> <body> <div class="box box1">box1</div> <div class="box box2">box2</div> </body> </html> Copy the code
If we follow our normal logic, the margin between box1 and box2 should be 200px, but this is not the case, as shown below
In fact, there is already a margin overlap problem.
-
Parent and child margins overlap
The sample code looks like this:
<! DOCTYPEhtml> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="Width = device - width, initial - scale = 1.0" /> <title>Parent and child margins overlap</title> <style> .box { line-height: 50px; text-align: center; color: # 333; } .parent { width: 600px; height: 100px; background-color: #f60; margin-top: 100px; } .child { width: 600px; height: 50px; background-color: #a90; margin-top: 50px; } </style> </head> <body> <div class="box parent"> <div class="box child">child</div> </div> </body> </html> Copy the code
If we just look at our code, the parent’s margin-top is 100px, the children’s margin-top is 50px, and the total is 150px, but the actual result is as follows:
The parent and child margin overlap problem can be realized without any of the following terms
Margin – top overlap
- The parent element is not a BFC element.
- No parent element
border-top
attribute - No parent element
padding-top
attribute - There are no inline elements separating the parent element from the first child element.
Margin – bottom overlap
- The parent element is not a BFC element.
- No parent element
border-top
attribute - No parent element
padding-top
attribute - There are no inline elements separating the parent element from the first child element.
- The parent element has no height limitation
-
The margins of empty block elements overlap
The sample code looks like this:
<! DOCTYPEhtml> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="Width = device - width, initial - scale = 1.0" /> <title>The margins of empty block elements overlap</title> <style> .parent { width: 600px; height: 10px; background-color: #f60; } .child { margin: 50px 0; } </style> </head> <body> <div class="parent"> <div class="child"></div> </div> </body> </html> Copy the code
The code results are as follows:
The child element’s margin overlaps with the body’s 8px margin.
Margin overlap triggers for empty block elements must meet the following conditions
- Elements are not
border
attribute - Elements are not
padding
attribute - There is no
inline
The element - No height is set.
- Elements are not
Margin overlap calculation rules
The calculation rules for margin overlap are as follows:
- The margin of two positive numbers takes the largest margin as the margin.
- If one is positive and one is negative, the final margin is the sum of the two.
- If both values are negative, the final margin takes the largest absolute value.
Write in the last
Since margins overlap, we should make good use of this feature in practical development. For example, when writing a list, try not to write only the margins or the bottom margins, try to write both together, so that the layout of the bottom or the head is not what we want