Blogger Information
Blog 10
fans 0
comment 0
visits 10285
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
div布局及浮动元素高度塌陷的解决措施
xsrsblog
Original
977 people have browsed it

浮动元素高度塌陷

子元素设置了float样式之后,脱离了正常的文档流,父元素的高度没有被相应撑开,导致了父元素的高度塌陷。

有若干方法可以消除子元素浮动造成的父元素高度塌陷:
如图示例:

  1. 设置父元素的高度

  1. 父元素也浮动

  1. 插入清除浮动元素

  1. 插入伪元素

  1. 父元素设置溢出隐藏样式

三列经典布局

  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. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. text-decoration: none;
  14. }
  15. /* 设置链接 */
  16. a{
  17. color: grey;
  18. font-size: 20px;
  19. height: 30px;
  20. line-height: 30px;
  21. }
  22. /* 头部设置,清除Li的浮动影响 */
  23. .head{
  24. background-color: lightskyblue;
  25. /* border: 1px solid red; */
  26. overflow: hidden;
  27. }
  28. /* li设置浮动 */
  29. li{
  30. float: left;
  31. padding: 10px 40px;
  32. /* border: 1px solid blue; */
  33. }
  34. .foot{
  35. background-color: lightskyblue;
  36. }
  37. /* 底部设置 */
  38. .foot span{
  39. text-align: center;
  40. display: block;
  41. margin: 0 auto;
  42. height: 50px;
  43. font-size: 20px;
  44. line-height: 50px;
  45. color: lightslategrey;
  46. }
  47. /* min-height一定要设置高度,否则会因为子div定位或浮动造成高度塌陷 */
  48. .body1{
  49. width: 980px;
  50. min-height: 620px;
  51. background-color: wheat;
  52. margin: 0 auto;
  53. position: relative;
  54. }
  55. .left{
  56. width: 200px;
  57. min-height: 600px;
  58. /* float: left; */
  59. background-color: magenta;
  60. position: absolute;
  61. top: 10px;
  62. left: 0;
  63. }
  64. /* 居中设置 */
  65. .middle{
  66. width: 560px;
  67. min-height: 600px;
  68. background-color: lightseagreen;
  69. /* float: left; */
  70. position: absolute;
  71. top: 10px;
  72. left: 210px;
  73. }
  74. .right{
  75. width: 200px;
  76. min-height: 600px;
  77. /* float: left; */
  78. background-color: mediumslateblue;
  79. position: absolute;
  80. top: 10px;
  81. right: 0;
  82. }
  83. </style>
  84. </head>
  85. <body>
  86. <div class="head">
  87. <ul>
  88. <li><a href="">首页</a></li>
  89. <li><a href="">618会场</a></li>
  90. <li><a href="">在线客服</a></li>
  91. </ul>
  92. </div>
  93. <div class="body1">
  94. <div class="left">left</div>
  95. <div class="middle">middle</div>
  96. <div class="right">right</div>
  97. </div>
  98. <div class="foot">
  99. <span>xxxxxx有限公司© | 备案号: 皖ICP *********</span>
  100. </div>
  101. </body>
  102. </html>

效果:

Correcting teacher:WJWJ

Correction status:qualified

Teacher's comments:总的来説写的很不错很认真,继续加油!
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post