练习5_浮动练习.html 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. /* 块元素如果不设置宽度 会默认占满父元素的宽度 */
  9. /* 块元素如果不设置高度 会根据内容撑开 */
  10. .box{
  11. border:1px solid black;
  12. margin-bottom: 20px;
  13. }
  14. .item1{
  15. background-color: blue;
  16. float: left;
  17. }
  18. /* 清除浮动 工具类 */
  19. .clearfix::after{
  20. content: "";
  21. display: block;
  22. clear: both;
  23. }
  24. .item2{
  25. background-color: red;
  26. float: right;
  27. }
  28. .item{
  29. width: 100px;
  30. height: 100px;
  31. color: white;
  32. font-size: 50px;
  33. font-weight: bold;
  34. margin:10px;
  35. text-align: center;
  36. line-height: 100px;
  37. }
  38. .item3{
  39. background-color: green;
  40. float: left;
  41. }
  42. .item4{
  43. background-color: orange;
  44. margin:10px auto;
  45. }
  46. .item5{
  47. background-color: green;
  48. float: right;
  49. }
  50. </style>
  51. </head>
  52. <body>
  53. <!-- 一个标签中可以写多个类名 -->
  54. <div class="box clearfix">
  55. <div class="item1 item">1</div>
  56. <div class="item1 item">2</div>
  57. <div class="item1 item">3</div>
  58. </div>
  59. <div class="box clearfix">
  60. <div class="item2 item">1</div>
  61. <div class="item2 item">2</div>
  62. <div class="item2 item">3</div>
  63. </div>
  64. <div class="box">
  65. <div class="item3 item">左</div>
  66. <div class="item5 item">右</div>
  67. <div class="item4 item">中</div>
  68. </div>
  69. </body>
  70. </html>