10_浮动.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. #div1{
  10. width: 200px;
  11. height: 200px;
  12. background: #f00;
  13. float: left;
  14. }
  15. #div2{
  16. width: 200px;
  17. height: 200px;
  18. background: #0f0;
  19. float: left;
  20. }
  21. h1{
  22. height: 100px;
  23. background: orchid;
  24. }
  25. #div0{
  26. /* height: 200px; */
  27. /* overflow: hidden; */
  28. }
  29. /* 伪元素选择器 after在内部的后边生成元素 */
  30. .clearfix::after{
  31. content:''; /*after元素必须有*/
  32. display: block;
  33. clear: both;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <!-- 浮动 left左浮动 |right有浮动 |none不浮动
  39. 浮动的元素特点: 脱离文档流 空间释放;
  40. 子元素浮动导致父元素塌陷问题
  41. 解决方式:
  42. 1 给父元素添加一个高度 (已知父元素的高度)
  43. 2 给父元素添加一个overflow:hidden
  44. 3 在浮动的元素下边添加一个 块级元素,通过clear 清除浮动
  45. 4 通过clearfix::after{
  46. content:'';
  47. display:block;
  48. clear:both
  49. }
  50. -->
  51. <div id="div0" class="clearfix">
  52. <div id="div1"></div>
  53. <div id="div2"></div>
  54. <!-- clear:left |right |both -->
  55. <!-- <div style="clear:both ;"></div> -->
  56. <!-- 自动生成一个块元素 -->
  57. </div>
  58. <h1></h1>
  59. </body>
  60. </html>