07.clearfix.html 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. .box1{
  10. width: 200px;
  11. height: 200px;
  12. background-color: #bfa;
  13. }
  14. /* .box1::before{
  15. content: '';
  16. display: table;
  17. } */
  18. .box2{
  19. width: 100px;
  20. height: 100px;
  21. background-color: orange;
  22. margin-top: 100px;
  23. }
  24. /*
  25. clearfix 这个样式可以同时解决高度塌陷和外边距重叠的问题,
  26. 当遇到这些问题时,直接使用clearfix这个类即可
  27. */
  28. .clearfix::before,
  29. .clearfix::after{
  30. content: '';
  31. display: table;
  32. clear:both;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="box1 clearfix">
  38. <div class="box2"></div>
  39. </div>
  40. </body>
  41. </html>