Blogger Information
Blog 12
fans 0
comment 0
visits 11008
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
盒模型之浮动和定位
amin
Original
723 people have browsed it

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

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

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

  • 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. }
  • 效果图

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