CSS 左中右三列布局5种方式

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

CSS 左中右三列布局左右固定宽度,固定位置,中间自适应的5种方式

如图:

另:上中下三行布局点这里

1.float布局

左右分别float:left/right;中间撑开

<section class="layout float">
    <style>
    .layout {
      margin-top: 10px;
    }

    .layout article div {
      height: 100px;
    }
      .layout.float .left {
        float: left;
        width: 300px;
        background: red;
      }

      .layout.float .center {
        background: #0fff;
      }

      .layout.float .right {
        float: right;
        width: 300px;
        background: yellow;
      }
    </style>
    <article>
      <div class="left"></div>
      <div class="right"></div>
      <div class="center">z这个是一个三部分左-中-右的布局根据float设置 第三方哈咖啡这里是最近刚好中间内容</div>
    </article>
  </section>

⚠️注意:这里要注意 center应该写在right后边,否则会把right挤到下一行,具体效果及原因参见 float元素先后顺序

2.position: absolute固定定位

左中右全部设置position:absolute;左边left:0;右边right:0;中间:left:300px;right:300px;

<section class="layout absolute">
    <style>
      .layout {
        margin-top: 10px;
      }

      .layout article div {
        height: 100px;
      }

      .absolute div {
        position: absolute;
      }

      .absolute .left {
        left: 0;
        width: 300px;
        background: red;
      }

      .absolute .center {
        left: 300px;
        right: 300px;
        background: #00ffff;
      }

      .absolute .right {
        right: 0;
        width: 300px;
        background: yellow;
      }
    </style>
    <article>
      <div class="left"></div>
      <div class="center">z这个是一个三部分左-中-右的布局根据absolute设置 第三方哈咖啡这里是最近刚好中间内容</div>
      <div class="right"></div>
    </article>
  </section>

3.flex弹性盒子布局

中间flex:1;自动填充剩余空间

<section class="layout flex">
    <style>
      .layout {
        margin-top: 10px;
      }

      .layout article div {
        height: 100px;
      }

      .flex article {
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .flex .left {
        width: 300px;
        background: red;
      }

      .flex .center {
        flex: 1;
        background: #0fff;
      }

      .flex .right {
        width: 300px;
        background: yellow;
      }
    </style>
    <article>
      <div class="left"></div>
      <div class="center">z这是flex布局</div>
      <div class="right"></div>
    </article>
  </section>

4.display:table;布局

<section class="layout table">
    <style>
       .layout {
        margin-top: 10px;
      }

      .layout article div {
        height: 100px;
      }
      .table article {
        display: table;
        width: 100%;
      }

      .table article>div {
        display: table-cell;
      }

      .table .left {
        width: 300px;
        background: red;
      }

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

      .table .right {
        width: 300px;
        background: yellow;
      }
    </style>
    <article>
      <div class="left"></div>
      <div class="center">这是table设置</div>
      <div class="right">1</div>
    </article>
  </section>

5.display:grid; 网格布局

<section class="layout grid">
    <style>
       .layout {
        margin-top: 10px;
      }

      .layout article div {
        height: 100px;
      }
      .grid article {
        display: grid;
        width: 100%;
        grid-template-rows: 100%;
        grid-template-columns: 300px auto 300px;
      }

      .grid .left {
        background: red;
      }

      .grid .center {
        background: #0ff;
      }

      .grid .right {
        background: yellow;
      }
    </style>
    <article>
      <div class="left"></div>
      <div class="center">这是grid布局</div>
      <div class="right"></div>
    </article>
  </section>

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前端卡卡西呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值