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>

 

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Div CSS 页面布局——左中右版式是一常见的网页布局方式。在这布局中,页面被分为左侧、中间和右侧三个区域,每个区域可以分别放置内容。 在HTML中,我们可以使用div元素来创建这样的布局。首先,我们需要设置一个父div元素,并为其设置宽度,以适应整个页面的大小。然后,我们可以创建三个div元素,分别用于左侧、中间和右侧区域。 对于左侧和右侧区域,我们可以通过设置宽度和浮动属性来控制其位置。例如,我们可以设置左侧div的宽度为25%,右侧div的宽度为25%,并将它们都向左浮动。这样,左侧和右侧的区域就会分别占据页面的左边和右边。 对于中间区域,我们可以简单地将其放在左侧和右侧区域之后。由于左侧和右侧区域都是浮动的,中间区域将会自动填充剩余的空间。 为了避免中间区域被覆盖,我们可以使用clear属性来清除浮动。我们可以在中间区域divCSS样式中添加clear:both来确保中间区域在内容的上方和下方都没有浮动元素。 除了设置区域的宽度和浮动属性外,我们还可以使用其他CSS属性来进一步调整页面布局。例如,我们可以通过设置边距、内边距和背景颜色来美化页面。 总的来说,Div CSS 页面布局——左中右版式是一简单而实用的布局方式,可以帮助我们将页面划分为多个区域,并在不同的区域放置不同的内容。通过灵活运用CSS属性,我们可以轻松地创建出各漂亮的页面布局
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

前端卡卡西呀

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

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

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

打赏作者

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

抵扣说明:

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

余额充值