首页 > 其他 > 详细

两栏布局

时间:2020-02-29 09:37:27      阅读:46      评论:0      收藏:0      [点我收藏+]

常用的后台布局方式。左侧固定,右侧宽度自适应屏幕。

实现方式

  1. BFC。运用 BFC 不和 float 块重叠的特点
  2. position 布局。right 块 absolute,让 left 为right的宽度
  3. 自适应布局。right 不设置宽度。用 margin 撑开左边边距。
  4. flex 布局。right 设置 flex=1.left 设置 flex-shrink:0;
  5. 运用 css 函数 calc。自动计算右侧的宽度。设置 right 的 width:calc(100% - left.width)。

代码实现

  <div class="float-box">
    <div class="aside"></div>
    <div class="main"></div>
  </div>
  • BFC
  .float-box {
    position: relative;
  }

  .aside {
    width: 100px;
    height: 150px;
    float: left;
    background: #f66;
  }

  .main {
    height: 200px;
    background: #fcc;
    overflow: hidden;
  }
  • postion 布局
  .float-box {
    position: relative;
  }

  .aside {
    width: 100px;
    height: 150px;
    float: left;
    background: #f66;
  }

  .main {
    height: 200px;
    background: #fcc;
    position: absolute;
    left: 100px;
    right: 0;
  }
  • 自适应布局
      .float-box {
        position: relative;
      }
    
      .aside {
        width: 100px;
        height: 150px;
        float: left;
        background: #f66;
      }
    
      .main {
        height: 200px;
        margin-left: 100px;
        background: #fcc;
      }
  • flex 布局
  .float-box {
    display: flex;
    justify-content: flex-start;
  }

  .aside {
    width: 100px;
    height: 150px;
    background: #f66;
    flex-shrink: 0;
  }

  .main {
    height: 200px;
    background: #fcc;
    flex-grow: 1;
    flex-shrink: 0;
    flex-basis: auto;
  }
  • cacl 计算
  .float-box {
    position: relative;
  }

  .aside {
    width: 100px;
    height: 150px;
    float: left;
    background: #f66;
  }

  .main {
    float: left;
    height: 200px;
    background: #fcc;
    width: calc(100% - 100px)
  }

两栏布局

原文:https://www.cnblogs.com/shenggao/p/12381328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!