08.外边距的折叠.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .box1,.box2{
  10. width: 200px;
  11. height: 200px;
  12. }
  13. /*
  14. 垂直外边距的重叠(折叠)
  15. -相邻的垂直方向外边距会发生重叠现象
  16. -兄弟元素
  17. -兄弟元素间的相邻垂直外边距会取两者之间的较大值(两者都是正值)
  18. -特殊情况:
  19. 如果相邻的外边距一正一负,则取两者的和
  20. 如果相邻的外边距都是负值,则取两者中绝对值较大的
  21. -兄弟元素之间的外边距重叠,对于开发时有利的,所以我们不需要进行处理
  22. -父子元素
  23. -父子元素间相邻外边距,子元素的会传递给父元素(上外边距)
  24. -父子外边距的折叠会影响到页面的布局,必须要进行处理
  25. */
  26. .box1{
  27. background-color: #bfa;
  28. /* 设置一个下外边距 */
  29. margin-bottom: 100px;
  30. }
  31. .box2{
  32. background-color: orange;
  33. /* 设置一个上外边距 */
  34. margin-top: 100px;
  35. }
  36. .box3{
  37. width: 200px;
  38. height: 200px;
  39. background-color: #bfa;
  40. /* padding-top: 100px; */
  41. /* border-top: 1px red solid; */
  42. }
  43. .box4{
  44. width: 100px;
  45. height: 100px;
  46. background-color: orange;
  47. margin-top: 100px;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <!-- <div class="box1"></div>
  53. <div class="box2"></div> -->
  54. <div class="box3">
  55. <div class="box4"></div>
  56. </div>
  57. </body>
  58. </html>