学习随笔 css布局 两固定中间自适应实现方法

> 在前端的面试中经常被问到 请写出一个两边固定中间自适应的方法实现

> 废话不多说直接上代码

> 首先写好默认样式

```javascript

  html *{

        padding: 0;

        margin: 0;

      }

      .layout article div{

        min-height: 100px;

      }

```

###### 利用float浮动实现 ######

```javascript

<!--浮动布局  -->

    <section class="layout float">

      <style media="screen">

      .layout.float .left{

        float:left;

        width:300px;

        background: red;

      }

      .layout.float .center{

        background: yellow;

      }

      .layout.float .right{

        float:right;

        width:300px;

        background: blue;

      }

      </style>

      <h1>三栏布局</h1>

      <article class="left-right-center">

        <div class="left"></div>

        <div class="right"></div>

        <div class="center">

          <h2>浮动解决方案</h2>

          1.这是三栏布局的浮动解决方案;

          2.这是三栏布局的浮动解决方案;

          3.这是三栏布局的浮动解决方案;

          4.这是三栏布局的浮动解决方案;

          5.这是三栏布局的浮动解决方案;

          6.这是三栏布局的浮动解决方案;

        </div>

      </article>

    </section>

```

###### 使用相对定位实现 ######

```javascript

<section class="layout absolute">

      <style>

        .layout.absolute .left-center-right>div{

          position: absolute;

        }

        .layout.absolute .left{

          left:0;

          width: 300px;

          background: red;

        }

        .layout.absolute .center{

          left: 300px;

          right: 300px;

          background: yellow;

        }

        .layout.absolute .right{

          right:0;

          width: 300px;

          background: blue;

        }

      </style>

      <h1>三栏布局</h1>

      <article class="left-center-right">

        <div class="left"></div>

        <div class="center">

          <h2>绝对定位解决方案</h2>

          1.这是三栏布局的浮动解决方案;

          2.这是三栏布局的浮动解决方案;

          3.这是三栏布局的浮动解决方案;

          4.这是三栏布局的浮动解决方案;

          5.这是三栏布局的浮动解决方案;

          6.这是三栏布局的浮动解决方案;

        </div>

        <div class="right"></div>

      </article>

    </section>

```

###### 利用flexbox实现 ######

```javascript

<!-- flexbox布局 -->

    <section class="layout flexbox">

      <style>

        .layout.flexbox{

          margin-top: 110px;

        }

        .layout.flexbox .left-center-right{

          display: flex;

        }

        .layout.flexbox .left{

          width: 300px;

          background: red;

        }

        .layout.flexbox .center{

          flex:1;

          background: yellow;

        }

        .layout.flexbox .right{

          width: 300px;

          background: blue;

        }

      </style>

      <h1>三栏布局</h1>

      <article class="left-center-right">

        <div class="left"></div>

        <div class="center">

          <h2>flexbox解决方案</h2>

          1.这是三栏布局的浮动解决方案;

          2.这是三栏布局的浮动解决方案;

          3.这是三栏布局的浮动解决方案;

          4.这是三栏布局的浮动解决方案;

          5.这是三栏布局的浮动解决方案;

          6.这是三栏布局的浮动解决方案;

        </div>

        <div class="right"></div>

      </article>

    </section>

```

###### 利用display:table实现 #####

```javascript

<!-- 表格布局 -->

    <section class="layout table">

      <style>

        .layout.table .left-center-right{

          width:100%;

          height: 100px;

          display: table;

        }

        .layout.table .left-center-right>div{

          display: table-cell;

        }

        .layout.table .left{

          width: 300px;

          background: red;

        }

        .layout.table .center{

          background: yellow;

        }

        .layout.table .right{

          width: 300px;

          background: blue;

        }

      </style>

      <h1>三栏布局</h1>

      <article class="left-center-right">

        <div class="left"></div>

        <div class="center">

          <h2>表格布局解决方案</h2>

          1.这是三栏布局的浮动解决方案;

          2.这是三栏布局的浮动解决方案;

          3.这是三栏布局的浮动解决方案;

          4.这是三栏布局的浮动解决方案;

          5.这是三栏布局的浮动解决方案;

          6.这是三栏布局的浮动解决方案;

        </div>

        <div class="right"></div>

      </article>

    </section>

```

###### 利用css3 display:grid实现(网格布局) #####

```javascript

<!-- 网格布局 -->

    <section class="layout grid">

      <style>

        .layout.grid .left-center-right{

          width:100%;

          display: grid;

          grid-template-rows: 100px;

          grid-template-columns: 300px auto 300px;

        }

        .layout.grid .left-center-right>div{

        }

        .layout.grid .left{

          width: 300px;

          background: red;

        }

        .layout.grid .center{

          background: yellow;

        }

        .layout.grid .right{

          background: blue;

        }

      </style>

      <h1>三栏布局</h1>

      <article class="left-center-right">

        <div class="left"></div>

        <div class="center">

          <h2>网格布局解决方案</h2>

          1.这是三栏布局的浮动解决方案;

          2.这是三栏布局的浮动解决方案;

          3.这是三栏布局的浮动解决方案;

          4.这是三栏布局的浮动解决方案;

          5.这是三栏布局的浮动解决方案;

          6.这是三栏布局的浮动解决方案;

        </div>

        <div class="right"></div>

      </article>

    </section>

```

* 以上是我所了解接触到的最常见的方法及实现有其他方案可以在评论区提供哦

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HTML 和 CSS 页面布局 css盒模型 BFC 页面布局 有5中可以解决: ​ 第一种:浮动 ....
    涅槃no重生阅读 318评论 0 0
  • 前面学习了使用HTML为网页添加内容,要对所添加的内容进行布局,就需要使用到CSS,JS等,这里就记录一下自己关于...
    wxyzcctn阅读 568评论 0 3
  • 1张I便签,关于如何快速获得信任感? 要想快速获得对方的信任感,需要了解对方的情况,做到对症下药。下文中是给出通过...
    0大梦0阅读 174评论 0 0
  • 像有一双无形的大手,推着我们向前进。——题记 这几天我一直处于闷声不语状态,我天真地以为,时间可以如空气,只要你不...
    杨榆阅读 317评论 0 1
  • 儿子,首先,对你得到24点擂主表示祝贺。 回想起来,应该差不多从寒假开始,我们一家三口陆陆续续地在玩24点,用纸牌...
    何豆子阅读 792评论 0 0