Blogger Information
Blog 26
fans 2
comment 0
visits 24277
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
清除元素浮动的几种方法及经典布局实现
leverWang
Original
547 people have browsed it

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

设置浮动后父级元素高度塌陷,高度塌陷产生的原因:元素浮动后脱离了文档流,无法撑开父级元素

清除浮动后父级元素高度被子元素重新撑开

解决方案:

  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. padding: 0;
  10. margin: 0;
  11. }
  12. .container {
  13. width: 1200px;
  14. border: 3px solid rgb(187, 0, 0);
  15. }
  16. /* 方案1 ,为父级元素添加高度 */
  17. /* .container {
  18. height: 200px;
  19. } */
  20. /* 方案2 为父级元素添加浮动 */
  21. /* .container {
  22. float: left;
  23. } */
  24. /* 方案3,为父级元素添加伪类 */
  25. /* .container:after {
  26. content: '.';
  27. display: block;
  28. clear: both;
  29. } */
  30. /* 方案4,为父级元素 overflow: hidden */
  31. .container {
  32. overflow: hidden;
  33. }
  34. .item {
  35. float: left;
  36. width: 200px;
  37. height: 200px;
  38. margin-left: 20px;
  39. }
  40. .item:first-of-type {
  41. background: lightpink;
  42. }
  43. .item:nth-of-type(2) {
  44. background: lightcyan;
  45. }
  46. .item:last-of-type {
  47. background: lightgreen;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="container">
  53. <div class="item"></div>
  54. <div class="item"></div>
  55. <div class="item"></div>
  56. </div>
  57. </body>
  58. </html>

利用浮动和定位实现经典圣杯布局

  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. padding: 0;
  10. margin: 0;
  11. }
  12. .header,
  13. .footer {
  14. width: 100%;
  15. height: 100px;
  16. background: lightslategray;
  17. }
  18. /* container设置 内边距 给left和right空出位置 */
  19. .container {
  20. overflow: hidden;
  21. padding: 0 200px;
  22. min-width: 500px;
  23. }
  24. /* 为元素设置浮动和相对定位 */
  25. .container>div {
  26. float: left;
  27. position: relative;
  28. }
  29. .container>.center {
  30. background: lightpink;
  31. height: 500px;
  32. width: 100%;
  33. }
  34. .left,
  35. .right {
  36. width: 200px;
  37. height: 500px;
  38. background: lightseagreen;
  39. }
  40. /* 设置left的 margin-left: -100%;,让left回到上一行最左侧 */
  41. .left {
  42. margin-left: -100%;
  43. left: -200px;
  44. }
  45. /* right的 margin-的: -200px;,让right的回到上一行最右侧 */
  46. .right {
  47. margin-right: -200px;
  48. }
  49. </style>
  50. </head>
  51. <body>
  52. <div class="header">头部</div>
  53. <div class="container">
  54. <div class="center">内容</div>
  55. <div class="left">左边</div>
  56. <div class="right">右边</div>
  57. </div>
  58. <div class="footer">底部</div>
  59. </body>
  60. </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