Here's a solution to the problem where margin doesn't work when divs are nested. By the way, let’s learn the definition and usage of margin. Div nested HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } body{ width: 100%; height: 100%; background-color: dodgerblue; } .box{ width: 300px; height: 300px; margin: 30px; background-color: gray; } .box .child{ width: 200px; height: 200px; margin: 50px; background-color:darkturquoise; } </style> </head> <body> <div class="box"> <div class="child"> </div> </div> </body> </html> Actual effect: This is actually not the effect we want. Let's first talk about why this problem occurs: There are two nested divs. If the parent element padding value of the outer div is 0, the margin-top or margin-bottom value of the inner div will be "transferred" to the outer div. To solve this problem, we need to first understand a property unique to IE: haslayout. The effect we want to achieve is this: Finally, let’s talk about the solution: 1. Let the parent element generate a block formatting context (block-level formatting context, you can learn about it on Baidu). The following properties can be achieved float: left/right position: absolute display: inline-block overflow: hidden/auto 2. Add border or padding to the parent element Take overflow:hidden of method 1 as an example: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style type="text/css"> *{ margin: 0; padding: 0; } body{ width: 100%; height: 100%; background-color: dodgerblue; } .box{ width: 300px; height: 300px; margin: 30px; background-color: gray; overflow: hidden; } .box .child{ width: 200px; height: 200px; margin: 50px; background-color:darkturquoise; } </style> </head> <body> <div class="box"> <div class="child"> </div> </div> </body> </html> Final result: This is the end of this article about how to solve the problem of margin not working when divs are nested in HTML. For more related content about margin not working when divs are nested in HTML, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: Detailed explanation of the payment function code of the Vue project
>>: Three ways to achieve text flashing effect in CSS3 Example code
Effect Fading from top to bottom Source code html...
Summary of common operators and operators in java...
The general way of writing is as follows: XML/HTM...
Table of contents Preface 1. Recursive components...
Table of contents 1. this points to 2. Modify thi...
CSS attribute selectors are amazing. They can hel...
I recently wanted to convert a website to https a...
Preface In MySQL, cross-database queries are main...
Notice! ! ! select * from user where uid not in (...
Table of contents MySQL multi-version concurrency...
React is an open-source JavaScript library used b...
This article uses examples to illustrate the MySQ...
MySQL replication detailed explanation and simple...
Table of contents 1. Basic configuration of Nginx...
Questions about select elements in HTML have been...