Previous This is a classic old question. Since a reader asked it before, I'll sort it out for you. Let’s start with a simple example Let's take a look at a simple example: <style> .slide1 div { margin:10px 0; } </style> <div class="slide1"> <h3>Margin collapse type 1: sibling elements</h3> <p>Text spacing 10px above and below</p> <p>Text spacing 10px above and below</p> </div> Look at the two Classification There are three basic cases of margin collapse:
Don't rush to memorize it. First of all, the example in the previous article is the first case - the margin collapse occurs between two adjacent elements. The second and third cases are as follows: <style> .father { background-color: green; } .child { margin-top: 50px; background-color: red; height: 300px; } .slide3 { margin: 10px 0; } </style> <h3>Second margin collapse: parent element and first child element</h3> <div class="slide2 father"> <!-- Parent element is green --> <div class="slide2 child"> <!-- Child elements are red --> </div> </div> <h3>Third margin collapse: empty block-level elements</h3> <div class="slide3"></div> Their images are also shown below: Case 2: The margins of the child element will be "transferred" outside the parent element Case 3: The top and bottom margins of the element will collapse Okay, now let's take a look at the common points of these situations (it is recommended to draw their box models, but I'm too lazy to draw them-_-), and we can find the common cause of margin collapse: the margins are directly in contact with each other without any obstruction. How to understand direct contact? It's very simple:
The results of folding in various cases How to calculate the margin after folding can be verified simply:
How to prevent margins from collapsing As mentioned above, the reason for margin collapse is that the margins are in direct contact. Therefore, the way to prevent the collapse is to block this direct contact. The combination of methods includes:
summary I have to add that what we discussed above are basic situations. In basic situations, combinations can also be made, such as between multiple adjacent elements; nesting of multiple layers of descendant elements, etc. Once you understand the basic principles, other situations will be easy to understand as long as you write a small demo to verify. Then there are the conventions: if there are mistakes in the content, please point them out (it’s totally fine if you feel uncomfortable and want to complain when reading it); The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. Original address: https://segmentfault.com/a/1190000016842993 |
<<: How to use Docker to limit container resources
>>: Nofollow makes the links in comments and messages really work
When nginx configures proxy_pass, the difference ...
Table of contents What happened? When to use Cont...
Index definition: It is a separate database struc...
Nowadays we often talk about Web2.0, so what is W...
Preface: The installation process will not be des...
Let's talk about some problems I have encounte...
After watching this, I guarantee that you have ha...
The META tag is an auxiliary tag in the head area...
In the migration of Oracle database to MySQL data...
About who Displays users logged into the system. ...
I use the simultaneous interpretation voice recog...
Correct answer Using useRouter : // router path: ...
Preface In this article, we will use Docker to bu...
In MySQL operation and maintenance, a R&D col...
Table of contents 1. Basic principles 2. Specific...