4.解决浮动问题.html 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. .main {
  9. border: 2px solid #000;
  10. /* overflow: auto; */
  11. }
  12. .one {
  13. background: #f00;
  14. float: left;
  15. width: 200px;
  16. height: 200px;
  17. font-size: 30px;
  18. color: #fff;
  19. }
  20. .two {
  21. background: #0f0;
  22. float: left;
  23. width: 200px;
  24. height: 200px;
  25. font-size: 30px;
  26. color: #fff;
  27. }
  28. .three {
  29. background: #00f;
  30. float: left;
  31. width: 200px;
  32. height: 200px;
  33. font-size: 30px;
  34. color: #fff;
  35. }
  36. /* .empty {
  37. clear: both;
  38. } */
  39. .clearfix::after {
  40. content: "";
  41. display: block;
  42. clear: both;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <!--
  48. 1.溢出隐藏法:在高度塌陷的父级元素上 添加一个属性 overflow:hidden/auto;
  49. 2.额外标签法: 在子集添加一个空标签 并添加属性clear:both/left/right; clear:none;
  50. 3.伪元素清浮动:
  51. .clearfix::after {
  52. content: "";
  53. display: block;
  54. clear: both;
  55. }
  56. 把样式名clearfix添加到对应高度塌陷的父元素上
  57. -->
  58. <div class="main clearfix">
  59. <div class="one">1</div>
  60. <div class="two">2</div>
  61. <div class="three">3</div>
  62. <!-- <div class="empty"></div> -->
  63. 我是一段话
  64. </div>
  65. </body>
  66. </html>