博客列表 >浮动元素高度塌陷产生的原因与解决方案

浮动元素高度塌陷产生的原因与解决方案

零龙
零龙原创
2020年06月26日 02:14:55677浏览

浮动元素高度塌陷产生的原因与解决方案

  • 在使用CSS编写网页的过程中,我们经常会遇到一种情况,给元素设置浮动之后float:left/right,当父元素没定义高,高度由子元素撑开。所有子元素都浮动后,父元素失去高了,这就是高度塌陷。

  • 例:


  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>浮动元素高度塌陷产生的原因</title>
  7. </head>
  8. <body>
  9. <style>
  10. .container{
  11. border: 1px solid #000;
  12. }
  13. .item{
  14. width: 150px;
  15. height: 150px;
  16. float: left;
  17. }
  18. .item:nth-child(1)
  19. {
  20. background-color: indianred;
  21. }
  22. .item:nth-child(2)
  23. {
  24. background-color: indigo;
  25. }
  26. .item:nth-child(3)
  27. {
  28. background-color: lawngreen;
  29. }
  30. </style>
  31. </body>
  32. <div class="container">
  33. <div class="item"></div>
  34. <div class="item"></div>
  35. <div class="item"></div>
  36. </div>
  37. </html>
  • 例效果图

解决方案


  • overflow:hidden

  • 隐藏溢出,当内容超过其父元素时,可以使用该属性和值将溢出的部分裁剪掉,使页面更加美观

  • 清除浮动,当子元素浮动时,给父元素添加overflow:hidden,按照它的第一个特性,应该将子元素超出的部分截掉,但是因为子元素有浮动,无法裁剪,所有只能由父元素增加高度去包裹住子元素,使得父元素拥有了高度,而这个高度是跟随子元素自适应的高度,这样就把浮动的子元素包含在父元素内。
    -实例:
  1. .container{
  2. border: 1px solid #000;
  3. overflow: hidden;
  4. }
  • 效果图

  • ::after 伪类

  • 利用伪类来清楚浮动,优点是不会增加页面元素,缺点是IE浏览器只支持IE8及其以上。在父元素中添加伪类::after
  • 实例:
  1. .container::after{
  2. content: "";
  3. clear:both; /*清除浮动*/
  4. display:block;
  5. }
  • 效果图

使用定位与浮动完成一个三列经典布局

  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>定位与浮动三列经典布局</title>
  7. </head>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. /* ↑初始化↑ */
  15. a {
  16. text-decoration: none;
  17. color: deepskyblue;
  18. }
  19. a:hover {
  20. color: tomato;
  21. }
  22. /* 设置网页超链接的字体样式 */
  23. .header {
  24. width: 100%;
  25. height: 50px;
  26. border: 1px solid #000;
  27. background-color: #333;
  28. line-height: 50px;
  29. box-shadow: 1px 1px 5px #333333; /* 设置header阴影 */
  30. }
  31. .header > ul > li {
  32. list-style: none;
  33. float: left; /*导航栏浮动 */
  34. margin-left: 20px;
  35. width: 80px;
  36. text-align: center;
  37. }
  38. .header > ul > li a:hover {
  39. background-color: rgb(0, 136, 45);
  40. display: block;
  41. color: snow;
  42. }
  43. /* 设置网页头的样式 */
  44. .container {
  45. width: 960px;
  46. border: 1px solid #000;
  47. margin: 5px auto;
  48. position: relative;
  49. }
  50. .container > .left {
  51. width: 200px;
  52. min-height: 600px;
  53. background-color: darkgreen;
  54. position: absolute; /*定位元素为父级元素 */
  55. top: 0;
  56. left: 0;
  57. }
  58. .container > .main {
  59. width: 550px;
  60. min-height: 600px;
  61. background-color: forestgreen;
  62. position: absolute; /*绝对定位 */
  63. top: 0;
  64. left: 200px;
  65. margin-left: 5px;
  66. }
  67. .container > .right {
  68. width: 200px;
  69. min-height: 600px;
  70. background-color: darkgreen;
  71. margin-left: 10px;
  72. position: absolute; /*绝对定位 */
  73. top: 0;
  74. left: 750px;
  75. }
  76. .footer {
  77. width: 100%;
  78. height: 50;
  79. position: fixed; /*对网页底部绝对定位 */
  80. bottom: 0;
  81. left: 0;
  82. background-color: #333;
  83. line-height: 50px;
  84. box-shadow: 1px 1px 5px #333333; /* 设置header阴影 */
  85. text-align: center;
  86. color: blanchedalmond;
  87. }
  88. </style>
  89. <body>
  90. <div class="header">
  91. <ul>
  92. <li><a href="">首页</a></li>
  93. <li><a href="">视频教程</a></li>
  94. <li><a href="">资源下载</a></li>
  95. <li><a href="">社区问答</a></li>
  96. <li><a href="">技术文章</a></li>
  97. </ul>
  98. </div>
  99. <div class="container">
  100. <div class="left"></div>
  101. <div class="main"></div>
  102. <div class="right"></div>
  103. </div>
  104. <div class="footer">云南省德宏州瑞丽市</div>
  105. </body>
  106. </html>

实例效果图

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议