博主信息
博文 12
粉丝 0
评论 0
访问量 14360
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
盒模型之浮动和定位
amin
原创
1391人浏览过

浮动元素高度塌陷产生的原因

  • 元素浮动之后从文档流中脱离出来,它原来在文档流中占据的空间就会被释放出来,它后面的元素会自动填充它出让出来的空间大小

浮动元素高度塌陷解决方案

  • html代码
  1. <div class="box">
  2. <div class="box1">box1</div>
  3. <div class="box2">box2</div>
  4. <div class="box3">box3</div>
  5. </div>
  • css代码
  1. .box{
  2. border: 2px solid #000;
  3. }
  4. .box1,.box2,.box3 {
  5. width: 200px;
  6. height: 200px;
  7. float: left;
  8. }
  9. .box1 {
  10. background-color: lightblue;
  11. }
  12. .box2 {
  13. background-color: lightcoral;
  14. }
  15. .box3 {
  16. background-color: lightgreen;
  17. }
  • 解决方案 (前三个方案基本不用,主要用到的是方案4和放啊安5)

    1、 给父元素也添加一个高度
    .box { height: 200px; }

    2、把父元素也浮动起来
    .box{float: left;}

    3、添加一个元素专用于清除浮动(置于父级元素的最后一个子元素位置)
    .clear {clear: both;}

    4、通过添加一个伪元素来解决
    .box:after { content: ""; display: block; clear: both; }

    5、使用overflow
    .box { overflow:hidden; }

  • 效果图

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

  • html
    1. <div class="header">
    2. header
    3. </div>
    4. <div class="container">
    5. <div class="left">左侧</div>
    6. <div class="main">内容区</div>
    7. <div class="right">右侧</div>
    8. </div>
    9. <div class="footer">
    10. footer
    11. </div>
  • css 定位实现
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. .header,
  7. .footer {
  8. width: 960px;
  9. height: 40px;
  10. line-height: 40px;
  11. margin: 0 auto;
  12. text-align: center;
  13. background-color: lightblue;
  14. }
  15. .container {
  16. width: 960px;
  17. margin: 10px auto;
  18. min-height: 100px;
  19. position: relative;
  20. }
  21. .container > .left {
  22. width: 200px;
  23. background-color: wheat;
  24. min-height: 100px;
  25. position: absolute;
  26. top: 0;
  27. left: 0;
  28. }
  29. .container > .right {
  30. width: 200px;
  31. background-color: wheat;
  32. min-height: 100px;
  33. position: absolute;
  34. top: 0;
  35. right: 0;
  36. }
  37. .container > .main {
  38. background-color: lightgreen;
  39. min-height: 100px;
  40. width: 540px;
  41. position: absolute;
  42. top: 0;
  43. left: 210px;
  44. }
  • css 浮动实现
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. box-sizing: border-box;
  5. }
  6. .header,
  7. .footer {
  8. width: 960px;
  9. height: 40px;
  10. line-height: 40px;
  11. margin: 0 auto;
  12. text-align: center;
  13. background-color: lightblue;
  14. }
  15. .container {
  16. width: 960px;
  17. margin: 10px auto;
  18. /* 清浮动 */
  19. overflow: hidden;
  20. }
  21. .container > .left {
  22. width: 200px;
  23. background-color: wheat;
  24. min-height: 100px;
  25. float: left;
  26. }
  27. .container > .right {
  28. width: 200px;
  29. background-color: wheat;
  30. min-height: 100px;
  31. float: left;
  32. }
  33. .container > .main {
  34. background-color: lightgreen;
  35. min-height: 100px;
  36. width: 540px;
  37. float: left;
  38. margin: 0 10px;
  39. }
  • 效果图

批改老师: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+教程免费学