16.清浮动.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. #box {
  14. /* height: 400px; */
  15. /* 溢出隐藏 */
  16. /* overflow: auto; */
  17. }
  18. #box div {
  19. /* margin-top: 10px; */
  20. /* float: left; */
  21. /* margin-left: 10px; */
  22. }
  23. #box1 {
  24. width: 200px;
  25. height: 200px;
  26. float: left;
  27. background: red;
  28. }
  29. #box2 {
  30. width: 200px;
  31. height: 200px;
  32. float: left;
  33. background: rgb(0, 255, 217);
  34. }
  35. #box3 {
  36. width: 200px;
  37. height: 200px;
  38. float: left;
  39. background: rgb(225, 255, 0);
  40. }
  41. #box4 {
  42. width: 200px;
  43. height: 200px;
  44. float: left;
  45. background: #00f;
  46. }
  47. #box5 {
  48. width: 200px;
  49. height: 200px;
  50. float: left;
  51. background: #0f0;
  52. }
  53. #box6 {
  54. width: 200px;
  55. height: 200px;
  56. float: left;
  57. background: rgb(255, 0, 212);
  58. }
  59. #empty {
  60. clear: both;
  61. }
  62. .clearfix::after {
  63. content: "";
  64. display: block;
  65. clear: both;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <!--
  71. 浮动导致的问题?
  72. 使用浮动标签的父元素的高度塌陷
  73. 清浮动:
  74. 1.溢出隐藏法
  75. 在塌陷的父元素上添加overflow:hidden/auto属性
  76. 2.伪元素清浮动
  77. .clearfix::after {
  78. content: "";
  79. display: block;
  80. clear: both;
  81. }
  82. 将 属性名添加到高度塌陷的父级盒子上
  83. 3.额外标签法
  84. 在浮动的同级添加一个空盒子 然后给这个盒子一个样式clear:both/left/right/none
  85. -->
  86. <div>
  87. <div id="box" class="clearfix">
  88. <div id="box1"></div>
  89. <div id="box2"></div>
  90. <div id="box3"></div>
  91. <div id="box4"></div>
  92. <div id="box5"></div>
  93. <div id="box6"></div>
  94. <!-- <div id="empty"></div> -->
  95. </div>
  96. <h1>你好</h1>
  97. </div>
  98. </body>
  99. </html>