css 上中下三行布局 4种方式

12 篇文章 1 订阅
2 篇文章 0 订阅

css 上中下三行布局,上下高度固定,中间自动填充满整个屏幕 的 4种方式

如下图

另,左中右三列布局点这里

1.absolute实现

 <section class="top-bottom absolute">
    <style>
      html,
      body,
      .absolute,
      article {
        height: 100%;
        overflow: hidden;
      }

      .absolute div {
        width: 100%;
      }

      .absolute .top {
        position: absolute;
        top: 0;
        height: 100px;
        background: red;
      }

      .absolute .middle {
        position: absolute;
        top: 100px;
        bottom: 100px;
        height: 100%;
        background: #0ff;
      }

      .absolute .bottom {
        position: absolute;
        bottom: 0;
        height: 100px;
        background: yellow;
      }
    </style>
    <article>
      <div class="top"></div>
      <div class="middle">上下固定,中间自适应 absolute实现</div>
      <div class="bottom"></div>
    </article>
  </section>

2. flex实现

<section class="top-bottom flex">
    <style>
      html,
      body,
      .flex,
      article {
        height: 100%;
      }

      .flex article {
        display: flex;
        flex-direction: column;
      }

      .flex .top {
        height: 100px;
        background: red;
      }

      .flex .middle {
        flex: 1;
        height: 100%;
        background: #0ff;
      }

      .flex .bottom {
        height: 100px;
        background: yellow;
      }
    </style>
    <article>
      <div class="top"></div>
      <div class="middle">上下固定,中间自适应 flex实现</div>
      <div class="bottom"></div>
    </article>
  </section>

3. display: table;实现

<section class="top-bottom table">
    <style>
      html,
      body,
      .table,
      article {
        height: 100%;
      }

      .table article {
        display: table;
        width: 100%;
      }

      .table article div {
        width: 100%;
        display: table-row;
      }

      .table .top {
        height: 100px;
        background: red;
      }

      .table .middle {
        background: #0ff;
      }

      .table .bottom {
        height: 100px;
        background: yellow;
      }
    </style>
    <article>
      <div class="top">top</div>
      <div class="middle">上下固定,中间自适应 table实现</div>
      <div class="bottom">bottom</div>
    </article>
  </section>

4. display:grid;实现

<section class="top-bottom grid">
    <style>
      html,
      body,
      .grid,
      article {
        height: 100%;
      }

      .grid article {
        display: grid;
        width: 100%;
        grid-template-rows: 100PX auto 100px;
      }

      .grid article div {
        width: 100%;
        display: grid-row;
      }

      .grid .top {
        background: red;
      }

      .grid .middle {
        height: 100%;
        background: #0ff;
      }

      .grid .bottom {
        background: yellow;
      }
    </style>
    <article>
      <div class="top">top</div>
      <div class="middle">上下固定,中间自适应 grid实现</div>
      <div class="bottom">bottom</div>
    </article>
  </section>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端卡卡西呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值