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

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>

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>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章