6.解决浮动问题.html 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #part1 {
  9. float: left;
  10. width: 200px;
  11. height: 200px;
  12. margin-top: 10px;
  13. background: red;
  14. }
  15. #part2 {
  16. float: left;
  17. width: 200px;
  18. height: 200px;
  19. margin-top: 10px;
  20. background: yellow;
  21. }
  22. #part3 {
  23. float: left;
  24. width: 200px;
  25. height: 200px;
  26. margin-top: 10px;
  27. background: blue;
  28. }
  29. #part4 {
  30. float: left;
  31. width: 200px;
  32. height: 200px;
  33. margin-top: 10px;
  34. background: pink;
  35. }
  36. #part5 {
  37. float: left;
  38. width: 200px;
  39. height: 200px;
  40. margin-top: 10px;
  41. background: purple;
  42. }
  43. /* .box {
  44. overflow: auto;
  45. } */
  46. /* .aa {
  47. clear: left;
  48. } */
  49. #clearfix::after {
  50. content: "";
  51. display: block;
  52. clear: both;
  53. }
  54. </style>
  55. </head>
  56. <body>
  57. <!--
  58. 1.溢出隐藏法:
  59. 在浮动的父级盒子上添加overflow:hidden|auto属性
  60. 2.额外标签法:
  61. 在浮动同级添加空盒子 给该盒子添加样式:clear:both |left |right |none
  62. 3.伪元素清浮动
  63. #clearfix::after {
  64. content: "";
  65. display: block;
  66. clear: both;
  67. }
  68. 将其添加到高度塌陷的父级盒子上
  69. -->
  70. <div class="box" id="clearfix">
  71. <div id="part1"></div>
  72. <div id="part2"></div>
  73. <div id="part3"></div>
  74. <div id="part4"></div>
  75. <div id="part5"></div>
  76. <!-- <div class="aa"></div> -->
  77. </div>
  78. </body>
  79. </html>