博主信息
博文 26
粉丝 2
评论 0
访问量 30892
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
清除元素浮动的几种方法及经典布局实现
leverWang
原创
733人浏览过

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

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

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

解决方案:

  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>
批改老师:WJWJ

批改状态:合格

老师批语:写的很认真仔细,继续加油!
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学