博客列表 >HTML布局 浮动元素高度塌陷原因及解决方案 定位 浮动实现web三列经典布局

HTML布局 浮动元素高度塌陷原因及解决方案 定位 浮动实现web三列经典布局

写代码的张先森
写代码的张先森原创
2020年06月19日 17:12:171409浏览

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


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>浮动元素的高度塌陷原因</title>
  7. <style>
  8. .container {
  9. border: 3px dashed red;
  10. }
  11. .item {
  12. width: 150px;
  13. height: 150px;
  14. }
  15. .item:first-of-type {
  16. background-color: lightgreen;
  17. }
  18. .item:nth-last-of-type(2) {
  19. background-color: lightcoral;
  20. }
  21. .item:last-of-type {
  22. background-color: lightblue;
  23. }
  24. /* 将三个子元素全部浮动 */
  25. .item {
  26. float: left;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div class="container">
  32. <div class="item">1</div>
  33. <div class="item">2</div>
  34. <div class="item">3</div>
  35. </div>
  36. </body>
  37. </html>

2.两种常用解决方案
好,我们知道了为什么会高度塌陷后,解决方案有四五种,接下来我们讲两种最常用且主流的解决方案:

(1)通过添加一个伪元素来解决

写法例:

  1. .container::after {
  2. content: "";
  3. display: block;
  4. clear: both;
  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>解决方案1</title>
  7. <style>
  8. .container {
  9. border: 3px dashed red;
  10. }
  11. .item {
  12. width: 150px;
  13. height: 150px;
  14. }
  15. .item:first-of-type {
  16. background-color: lightgreen;
  17. }
  18. .item:nth-last-of-type(2) {
  19. background-color: lightcoral;
  20. }
  21. .item:last-of-type {
  22. background-color: lightblue;
  23. }
  24. /* 将三个子元素全部浮动 */
  25. .item {
  26. float: left;
  27. }
  28. /* 解决方案1: 通过添加一个伪元素来解决 */
  29. .container::after {
  30. content: "";
  31. display: block;
  32. clear: both;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="container">
  38. <div class="item">1</div>
  39. <div class="item">2</div>
  40. <div class="item">3</div>
  41. </div>
  42. </body>
  43. </html>

(2)最简单的解决方案,用到BFC(块级格式化上下文) 通俗点用一个溢出隐藏的css方法

写法例:

  1. .container {
  2. overflow: hidden;
  3. }

我们可以通过给父元素添加一个overflow: hidden;溢出隐藏来解决高度塌陷的问题

  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>解决方案2</title>
  7. <style>
  8. .container {
  9. border: 3px dashed red;
  10. }
  11. .item {
  12. width: 150px;
  13. height: 150px;
  14. }
  15. .item:first-of-type {
  16. background-color: lightgreen;
  17. }
  18. .item:nth-last-of-type(2) {
  19. background-color: lightcoral;
  20. }
  21. .item:last-of-type {
  22. background-color: lightblue;
  23. }
  24. /* 将三个子元素全部浮动 */
  25. .item {
  26. float: left;
  27. }
  28. /* 解决方案2: 最简单的解决方案,用到BFC(块级格式化上下文) 溢出隐藏 */
  29. .container {
  30. overflow: hidden;
  31. }
  32. </style>
  33. </head>
  34. <body>
  35. <div class="container">
  36. <div class="item">1</div>
  37. <div class="item">2</div>
  38. <div class="item">3</div>
  39. </div>
  40. </body>
  41. </html>

讲完了浮动高度塌陷的问题接下来我们实战定位和浮动做web经典的三列布局

二、运用定位和浮动知识实战三列布局

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>使用绝对定位来完成一个通用三列布局</title>
  7. <style>
  8. /* 初始化 */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. li {
  15. list-style: none;
  16. }
  17. a {
  18. text-decoration: none;
  19. color: #666;
  20. }
  21. /* 页眉与页脚 */
  22. .header,
  23. .footer {
  24. height: 40px;
  25. background-color: black;
  26. }
  27. /* 页眉与页脚的内容区 */
  28. .content {
  29. width: 960px;
  30. margin: auto;
  31. }
  32. .content ul li {
  33. float: left;
  34. line-height: 40px;
  35. padding: 0 15px;
  36. }
  37. .content ul li a {
  38. display: block;
  39. width: 100%;
  40. height: 100%;
  41. color: white;
  42. box-sizing: content-box;
  43. }
  44. .content ul li:hover {
  45. background-color: white;
  46. color: black;
  47. }
  48. .content ul li a:hover {
  49. color: black;
  50. }
  51. /* 页脚样式 */
  52. .content p {
  53. text-align: center;
  54. line-height: 40px;
  55. color: white;
  56. }
  57. /* 主体用定位 */
  58. .container {
  59. width: 960px;
  60. margin: 10px auto;
  61. min-height: 600px;
  62. /* 转为定位元素,做为定位父级 */
  63. position: relative;
  64. }
  65. .container > .left {
  66. width: 200px;
  67. background-image: url("111.jpg");
  68. background-size: 100%;
  69. min-height: 600px;
  70. position: absolute;
  71. top: 0;
  72. left: 0;
  73. }
  74. .container > .right {
  75. width: 200px;
  76. background-image: url("222.jpg");
  77. background-size: 100%;
  78. min-height: 600px;
  79. position: absolute;
  80. top: 0;
  81. right: 0;
  82. }
  83. .container > .main {
  84. background-color: lightgreen;
  85. min-height: 600px;
  86. width: 540px;
  87. position: absolute;
  88. top: 0;
  89. left: 210px;
  90. }
  91. </style>
  92. </head>
  93. <body>
  94. <!-- 页眉 -->
  95. <div class="header">
  96. <!-- 内容区: 水平居中 -->
  97. <div class="content">
  98. <ul>
  99. <li><a href="">首页</a></li>
  100. <li><a href="">电脑办公</a></li>
  101. <li><a href="">家装</a></li>
  102. <li><a href="">食品</a></li>
  103. <li><a href="">生鲜</a></li>
  104. <li><a href="">手机</a></li>
  105. <li><a href="">数码</a></li>
  106. </ul>
  107. </div>
  108. </div>
  109. <!-- 主体 -->
  110. <div class="container">
  111. <div class="left">左侧</div>
  112. <div class="main">内容区</div>
  113. <div class="right">右侧</div>
  114. </div>
  115. <!-- 页脚 -->
  116. <div class="footer">
  117. <div class="content">
  118. <p>北京XXXX科技发展有限公司&copy; | 备案号: 京ICP *********</p>
  119. </div>
  120. </div>
  121. </body>
  122. </html>

效果图:

左右侧图片随便找的 将就着看☺

2.使用浮动方法来完成一个三列布局
代码:

  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. <style>
  8. /* 初始化 */
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. li {
  15. list-style: none;
  16. }
  17. a {
  18. text-decoration: none;
  19. color: #666;
  20. }
  21. /* 页眉与页脚 */
  22. .header,
  23. .footer {
  24. height: 40px;
  25. background-color: lightblue;
  26. }
  27. /* 页眉与页脚的内容区 */
  28. .content {
  29. width: 960px;
  30. margin: auto;
  31. }
  32. .content ul li {
  33. float: left;
  34. line-height: 40px;
  35. padding: 0 15px;
  36. }
  37. .content ul li:hover {
  38. background-color: coral;
  39. }
  40. /* 页脚样式 */
  41. .content p {
  42. text-align: center;
  43. line-height: 40px;
  44. }
  45. /* 主体用定位 */
  46. .container {
  47. width: 960px;
  48. margin: 10px auto;
  49. min-height: 600px;
  50. /* 防止浮动元素的高度塌陷 */
  51. overflow: hidden;
  52. }
  53. .container > .left {
  54. width: 200px;
  55. background-image: url("111.jpg");
  56. background-size: 100%;
  57. min-height: 600px;
  58. float: left;
  59. }
  60. .container > .right {
  61. width: 200px;
  62. background-image: url("222.jpg");
  63. background-size: 100%;
  64. min-height: 600px;
  65. float: right;
  66. }
  67. .container > .main {
  68. background-color: lightgreen;
  69. min-height: 600px;
  70. width: 540px;
  71. float: left;
  72. margin-left: 10px;
  73. }
  74. </style>
  75. </head>
  76. <body>
  77. <!-- 页眉 -->
  78. <div class="header">
  79. <!-- 内容区: 水平居中 -->
  80. <div class="content">
  81. <ul>
  82. <li><a href="">首页</a></li>
  83. <li><a href="">电脑办公</a></li>
  84. <li><a href="">家装</a></li>
  85. <li><a href="">食品</a></li>
  86. <li><a href="">生鲜</a></li>
  87. <li><a href="">手机</a></li>
  88. <li><a href="">数码</a></li>
  89. </ul>
  90. </div>
  91. </div>
  92. <!-- 主体 -->
  93. <div class="container">
  94. <div class="left">左侧</div>
  95. <div class="main">内容区</div>
  96. <div class="right">右侧</div>
  97. </div>
  98. <!-- 页脚 -->
  99. <div class="footer">
  100. <div class="content">
  101. <p>北京XXXX科技发展有限公司&copy; | 备案号: 京ICP *********</p>
  102. </div>
  103. </div>
  104. </body>
  105. </html>

效果图:

两种布局方法主要是css浮动和定位元素的变化,认真看css代码

小结


1.web页面布局技术常用到浮动和定位,一定要深刻理解浮动和定位知识
2.理解浮动的原理 会解决浮动后高度塌陷的问题
3.动手实践利用定位和浮动技术实现通用的三列布局方法

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