两栏布局 左侧固定右侧自适应

10 篇文章 0 订阅

两栏布局 左侧固定右侧自适应

 <div class="out">
        <div class="left">左</div>
        <div class="right">右</div>
    </div>

1.浮动

  • 左侧固定,设置向左浮动
  • 右边margin-left: 200px;,右边元素宽度默认为auto 
 .out{
            height: 100px;
        }
        .left{
            height: 100px;
            width: 200px;
            float: left;
            background-color: yellow;
        }
        .right{
        height: 100px;
            margin-left: 200px;
            background-color: aqua;
        }

2.浮动加BFC

  • 左侧宽度固定,设置浮动
  • 右边overflow:hidden,触发bfc盒子 ,bfc不会与浮动元素发生重叠
 .out{
            height: 100px;
        }
        .left{
            height: 100%;
            width: 200px;
            float: left;
            background-color: yellow;
        }
        .right{
        height: 100px;
          overflow: hidden;
            background-color: aqua;
        } 

 3.flex布局

  • 外围盒子设置 display: flex;
  • 左边固定高度,右边flex设置为1
.out{
    display: flex;
            height: 100px;
        }
        .left{
            height: 100%;
            width: 200px;
            background-color: yellow;
        }
        .right{
        height: 100px;
        flex: 1;
            background-color: aqua;
        }

 4.绝对定位

  • 父元素相对定位,
  • 左边元素设置为absolute定位,并且固定宽度设置为200px,left值设置为0。
  • 右盒子 margin-left: 200px;
 .out{
           position: relative;
            height: 100px;
        }
        .left{
            position: absolute;
            height: 100%;
            width: 200px;
            background-color: yellow;
        }
        .right{
        height: 100px;
        margin-left: 200px;
            background-color: aqua;
        } 

5.gird布局 

  • 盒子添加"display: grid"属性,再添加"grid-template-columns: 100px 1fr"属性,表示第一列宽度始终为100px,第二列的宽度为剩余的所有空间。
  .out{
             height: 100px;
             display: grid;
             grid-template-columns: 100px 1fr;
        }
        .left {
            width: 100px;
            height: 100px;
         background-color: yellow;
     }
       .right {
        height: 100px;
      background-color: aqua;
      }


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值