9.清浮动.html 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #box {
  9. /* height: 800px; */
  10. margin: 0 auto;
  11. /* overflow: hidden; */
  12. /* overflow: auto; */
  13. /* border: 3px solid #000; */
  14. }
  15. #part1 {
  16. width: 200px;
  17. height: 200px;
  18. float: left;
  19. background-color: #f00;
  20. }
  21. #part2 {
  22. width: 230px;
  23. height: 230px;
  24. float: left;
  25. background-color: #ff0;
  26. }
  27. #part3 {
  28. width: 200px;
  29. height: 200px;
  30. float: left;
  31. background-color: #00f;
  32. }
  33. /*
  34. 浮动会导致父元素高度塌陷
  35. 清浮动:
  36. 1.溢出隐藏法
  37. 在高度塌陷的父元素上 添加overflow:hidden/auto;
  38. 2.额外标签法
  39. 在浮动的盒子同级添加一个空盒子
  40. 并给当前盒子 添加属性 clear:both/left/right/none
  41. 3.伪元素清浮动
  42. .clearfix::after {
  43. content: "";
  44. display: block;
  45. clear: both;
  46. }
  47. 将属性名添加到 高度塌陷的父元素中
  48. */
  49. #aa {
  50. clear: both;
  51. }
  52. .clearfix::after {
  53. content: "";
  54. display: block;
  55. clear: both;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div id="box" class="clearfix">
  61. <div id="part1"></div>
  62. <div id="part2"></div>
  63. <div id="part3"></div>
  64. <!-- <div id="aa"></div> -->
  65. </div>
  66. </body>
  67. </html>