21_BFC.html 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box1{
  9. width: 400px;
  10. height: 400px;
  11. background-color: blue;
  12. /* float: left; */
  13. overflow: hidden;
  14. /* display: inline-block */
  15. /* border:1px solid black; */
  16. }
  17. .box2{
  18. width: 200px;
  19. height: 200px;
  20. background-color: red;
  21. margin-top: 100px;
  22. margin-left: 100px;
  23. }
  24. .box3,.box4{
  25. width: 200px;
  26. height: 200px;
  27. }
  28. .box3{
  29. background-color: green;
  30. margin-bottom: 20px;
  31. }
  32. .box4{
  33. margin-top: 30px;
  34. background-color: yellow;
  35. }
  36. .box5{
  37. overflow: hidden;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <!-- 外边距溢出 仅限于垂直方向 margin-top-->
  43. <!-- BFC 块格式化上下文-->
  44. <!-- BFC 限制内部元素与外部元素接触 内部元素所有效果仅限于BFC区域内 -->
  45. <div class="box1">
  46. <div class="box2"></div>
  47. </div>
  48. <!-- 外边距合并 上下两个相邻元素 垂直方向 margin-top margin-bottom 合并成一个 -->
  49. <div class="box5">
  50. <div class="box3"></div>
  51. </div>
  52. <div class="box4"></div>
  53. </body>
  54. </html>