问题描述

假设高度已知,写一个三栏布局,左右各200px,中间宽度自适应

解决方案or思路

  1. 浮动
  2. 绝对定位
  3. flex-box布局
  4. 网格grid布局
  5. 表格布局tab-cell的使用

    具体代码

    浮动定位
  1. <div class="container1">
  2. <div class="left">左边200</div>
  3. <div class="right">右边200</div>
  4. <div class="center">中间自适应</div>
  5. </div>
  1. .container1{
  2. div{
  3. height: 100px;
  4. }
  5. .left{
  6. background: red;
  7. width: 200px;
  8. float: left;;
  9. }
  10. .right{
  11. background: yellow;
  12. width: 200px;
  13. float: right;
  14. }
  15. .center{
  16. background: green;
  17. }
  18. }

绝对定位

  1. <div class="container2">
  2. <div class="left">左边200绝对定位</div>
  3. <div class="center">中间自适应-绝对定位</div>
  4. <div class="right">右边200绝对定位</div>
  5. </div>
  1. .container2{
  2. position: relative;
  3. div{
  4. height: 100px;
  5. position: absolute;
  6. }
  7. .left{
  8. background: yellow;
  9. left: 0;
  10. width: 200px;
  11. }
  12. .right{
  13. background: red;
  14. right: 0;
  15. width: 200px;
  16. }
  17. .center{
  18. background: cyan;
  19. left: 200px;
  20. right: 200px;
  21. }
  22. }

flex布局

  1. <div class="container3">
  2. <div class="left">左边200flex</div>
  3. <div class="center">中间自适应-flex</div>
  4. <div class="right">右边200flex</div>
  5. </div>
  1. .container3{
  2. margin-top: 200px;
  3. display: flex;
  4. div{
  5. height: 100px;
  6. }
  7. .left{
  8. background: yellow;
  9. width: 200px;
  10. }
  11. .right{
  12. background: red;
  13. width: 200px;
  14. }
  15. .center{
  16. background: cyan;
  17. flex:1;
  18. }
  19. }

网格布局

  1. <div class="container5">
  2. <div class="left">左边200grid</div>
  3. <div class="center">中间自适应-grid</div>
  4. <div class="right">右边200grid</div>
  5. </div>
  1. .container5{
  2. margin-top: 30px;
  3. width: 100%;
  4. display: grid;
  5. grid-template-rows:100px;
  6. grid-template-columns: 200px auto 200px;
  7. div{
  8. height: 100px;
  9. }
  10. .left{
  11. background: yellow;
  12. width: 200px;
  13. }
  14. .right{
  15. background: red;
  16. width: 200px;
  17. }
  18. .center{
  19. background: cyan;
  20. }
  21. }
  • 表格布局
  1. <div class="container4">
  2. <div class="left">左边200table</div>
  3. <div class="center">中间自适应-table</div>
  4. <div class="right">右边200table</div>
  5. </div>
  1. .container4{
  2. margin-top: 30px;
  3. width: 100%;
  4. display: table;
  5. div{
  6. height: 100px;
  7. display: table-cell;
  8. }
  9. .left{
  10. background: yellow;
  11. width: 200px;
  12. }
  13. .right{
  14. background: red;
  15. width: 200px;
  16. }
  17. .center{
  18. background: cyan;
  19. }
  20. }

各个方法的优缺点

方法 优势 劣势 去掉高度已
知后的适用性
浮动 兼容性较好 需要清除浮动以及处理好浮动元素周围的
元素之间的关系
绝对定位 快捷不易出问题 所有的子元素会脱离文档流,可使用性较差
flex布局 比较完美,尤其是在移动端更为方便 备注:css3属性
表格布局 兼容性好,可作为flex的补充(ie8) 当某个单元格高度超出的时候,其他单元格会
自动增高
网格布局(新) css开始支持,同任务代码更简单

拓展问题

  • 垂直三栏布局,两边定高,中间自适应(网格 flex 绝对定位目前可实现)
    1. <div class="container">
    2. <div class="top">上200</div>
    3. <div class="middle">中间自适应</div>
    4. <div class="bottom">下200</div>
    5. </div>
    flexbox布局:当高度不足400的时候,定高会同时缩小
    1. .container{
    2. height: 100%;
    3. display: flex;
    4. flex-direction: column;
    5. div{
    6. width: 100px;
    7. min-height:100px;
    8. }
    9. .top{
    10. background: red;
    11. height: 200px;
    12. }
    13. .middle{
    14. background: orange;
    15. flex: 1;
    16. }
    17. .bottom{
    18. background: green;
    19. height: 200px;
    20. }
    21. }

绝对定位

  1. .container{
  2. height: 100%;
  3. div{
  4. width: 100px;
  5. position:absolute;
  6. }
  7. .top{
  8. background: red;
  9. height: 200px;
  10. top:0;
  11. }
  12. .middle{
  13. background: orange;
  14. top:200px;
  15. bottom:200px;
  16. }
  17. .bottom{
  18. background: green;
  19. height: 200px;
  20. bottom:0
  21. }
  22. }

表格布局(在元素外部包裹一层显示为tab-row,真实元素显示tab-cell)

  1. .container{
  2. height: 100%;
  3. display: flex;
  4. flex-direction: column;
  5. div{
  6. width: 100px;
  7. }
  8. .top{
  9. background: red;
  10. height: 200px;
  11. }
  12. .middle{
  13. background: orange;
  14. flex: 1;
  15. }
  16. .bottom{
  17. background: green;
  18. height: 200px;
  19. }
  20. }

网格布局

  1. .container4{
  2. height: 100%;
  3. display: grid;
  4. grid-template-rows: 200px auto 200px;
  5. grid-template-columns: auto;
  6. div{
  7. width: 100px;
  8. }
  9. .top{
  10. background: red;
  11. }
  12. .middle{
  13. background: orange;
  14. }
  15. .bottom{
  16. background: green;
  17. }
  18. }
  • 两栏布局
  1. <div class="container">
  2. <div class="left">左邊300</div>
  3. <div class="right">右邊自適應</div>
  4. </div>

浮动

  1. .container{
  2. div{
  3. height: 200px;
  4. }
  5. .left{
  6. float: left;
  7. width: 300px;
  8. background: red;
  9. }
  10. .right{
  11. background: green;
  12. }
  13. }

flex布局

  1. .container{
  2. display: flex;
  3. div{
  4. height: 200px;
  5. }
  6. .left{
  7. width: 300px;
  8. background: red;
  9. }
  10. .right{
  11. background: green;
  12. flex:1;
  13. }
  14. }

网格布局

  1. .container{
  2. display: grid;
  3. grid-template-columns: 100px auto;
  4. grid-template-rows: 200px;
  5. div{
  6. // height: 200px;
  7. }
  8. .left{
  9. background: red;
  10. }
  11. .right{
  12. background: green;
  13. }
  14. }

绝对定位

  1. .container{
  2. width: 100%;
  3. position: relative;
  4. div{
  5. height: 200px;
  6. position: absolute;
  7. }
  8. .left{
  9. background: red;
  10. left: 0;
  11. width: 200px;
  12. }
  13. .right{
  14. background: green;
  15. left: 200px;
  16. width: 100%;//或者设置right为0
  17. }
  18. }

表格布局

  1. .container{
  2. width: 100%;//必须设置
  3. display: table;
  4. div{
  5. height: 100px;
  6. }
  7. .left{
  8. background: red;
  9. width: 200px;
  10. display: table-cell;
  11. }
  12. .right{
  13. background: green;
  14. display: table-cell;
  15. }
  16. }