经典的流式布局实现方法总结(二)

前言:

昨天总结了经典的流式布局中的左侧固定,右侧自适应。今天总结一下右侧固定左侧自适应的三种实现方法。其实,左侧固定右侧自适应和右侧固定左侧自适应基本差不多,就是左右换一下。但是,需要强调和注意的是利用bfc块级格式化上下文, 实现右侧固定左侧自适应的时候,需要在html结构上让右盒子在前。

右侧固定左侧自适应的三种实现方法:

1.利用bfc块级格式化上下文, 实现右侧固定左侧自适应
(1) 先从结构上就要让 右盒子 先右浮
(2)再给 left 左盒子, 添加 overflow: hidden;

具体css代码

<style>
    .father {
      height: 400px;
      background-color: pink;
    }
    .right {
      float: right;
      width: 200px;
      height: 300px;
      background-color: blue;
    }
    .left {
      height: 350px;
      background-color: green;
      overflow: hidden;
    }
    /* 注意点:
            1. 先从结构上就要让 右盒子 先右浮
            2. 再给 left 左盒子, 添加 overflow: hidden;
     */
  </style>

html结构

<div class="father">
    <div class="right"></div>   //注意: 一定要right 在前
    <div class="left"></div>
</div>

2.利用定位实现右侧固定左侧自适应

(1) 给父盒子设置padding-right 值

(2) 给右侧子盒子设置width = padding-right ,并定位到父盒子的padding-right上.

(3) 左侧盒子自适应

具体css代码

<style>
    .father {
      height: 400px;
      background-color: pink;
      padding-right: 200px;
      position: relative;
    }
    .right {
      width: 200px;
      height: 300px;
      background-color: blue;
      position: absolute;
      top: 0;
      right: 0;
    }
    .left {
      height: 350px;
      background-color: green;
    }
    
  </style>

html结构

<div class="father">
    <div class="left"></div>
    <div class="right"></div>
</div>

3.利用flex布局 实现右侧固定左侧自适应

(1) 给父盒子设置display:flex ;

(2) 右侧盒子设置固定宽高 ,左侧盒子设置flex:1 ;

具体css代码

<style>
    .father {
      height: 400px;
      background-color: pink;
      display: flex;
    }
    .right {
      float: right;
      width: 200px;
      height: 300px;
      background-color: blue;
    }
    .left {
      height: 350px;
      background-color: green;
      flex: 1 ;
    }
    
  </style>

html结构

<div class="father">
    <div class="left"></div>
    <div class="right"></div>
</div>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值