网页效果 - 填充布局

一、使用定位属性填充空间

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      html, body {
        height: 100%;
      }
      .box {
        height: 100%;
        position: relative;
        background-color: yellow;
      }
      .box1 {
        height: 50px;
        background-color: red;
      }
      .box2 {
        position: absolute;
        // top 要跟height的数值一致
        top: 50px;
        bottom: 0;
        left: 0;
        right: 0;
        background-color: blue;
        overflow: hidden;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="box1"></div>
      <div class="box2">
      </div>
    </div>
  </body>
</html>

缺点:需要根据box1计算或者设置box2的height

二、使用flex布局

<!DOCTYPE html>
<html lang="en">
  <head>
    <style>
     * {
        margin:0;
        padding:0;
      }
      html{
        height:100%;
        background-color: green;
      }
      body{
        height:100%;
        background-color: red;
      }
      .box{
        height:100%;
        display: flex;
        flex-direction: column;
      }
      .box1{
        width: 100%;
        height: 50px;
        background-color: yellow;
      }
      .box2{
        width: 100%;
        height: 100px;
        background-color: grey;
        flex: 1;
        overflow: scroll;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="box1"></div>
      <div class="box2">
      </div>
    </div>
  </body>
</html>

解决了需要知道box1的高度问题

flex: 1 的作用

通过元素审查我们容易得知

flex: 1是一个简写属性,它相当于 flex: 1 1 0%

详细属性是flex-grow: 1;flex-shrink: 1;flex-basis: 0%;

flex-grow 属性用于设置或检索弹性盒子的扩展比率。
注意:如果元素不是弹性盒对象的元素,则 flex-grow 属性不起作用。

flex-shrink 属性指定了 flex 元素的收缩规则。flex 元素仅在默认宽度之和大于容器的时候才会发生收缩,其收缩的大小是依据 flex-shrink 的值。
注意:如果元素不是弹性盒对象的元素,则 flex-shrink 属性不起作用。

flex-basis 属性用于设置或检索弹性盒伸缩基准值。
注意:如果元素不是弹性盒对象的元素,则 flex-basis 属性不起作用。

通俗来讲,如果在flex布局中,容器有剩余空间,flex-grow设置元素利用剩余空间的比率;如果元素空间之和大于容器空间,flex-shrink设置元素的缩小比率;而flex-basis,则是给可伸缩项在容器中分配初始大小.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值