CSS两边固定中间自适应布局

1:使用浮动

html:

<section class="layout float">
    <article class="left-center-right">
        <div class="left"></div>
        <div class="right"></div>
        <div class="center"></div>
    </article>
</section>

css:

.layout.float .left-center-right>div{
      min-height:100px;
   }
  .left-center-right .left{
      float:left;
      width:300px;
      background:red;
  }
  .left-center-right .right{
      float:right;
      width:300px;
      background:red;
  }
  .left-center-right .center{
      background:green;
  }

2:使用绝对定位

html:

<section class="layout absolute">
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </article>
</section>

css:

.layout.absolute.left-center-right>div{
      min-height:100px;
      position:absolute;
   }
  .left-center-right .left{
      left:0;
      width:300px;
      background:red;
  }
  .left-center-right .right{
      right:0;
      width:300px;
      background:red;
  }
  .left-center-right .center{
      left:300px;
      right:300px;
      background:green;
  }

3:使用flexbox方法

html:

  <section class="layout flex">
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </article>
  </section>

css:

.layout.flex. left-center-right>div{
      min-height:100px;
   }
.layout.flex .left-center-right{
      display:flex;
}
  .left-center-right .left{
      width:300px;
      background:red;
  }
  .left-center-right .right{
      width:300px;
      background:red;
  }
  .left-center-right .center{
      flex:1;
      background:green;
  }

4:使用table布局

html:

<section class="layout table">
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </article>
  </section>

css:

.layout.table .left-center-right{
      height:100px;
      width:100%;
      display:table;
   }
.layout.table .left-center-right>div{
      display:table-cell;
}
.left-center-right .left{
      width:300px;
      background:red;
  }
  .left-center-right .right{
      width:300px;
      background:red;
  }
  .left-center-right .center{
      background:green;
  }

5:使用grid:

html:

<section class="gridLayout">
    <article class="left-center-right">
        <div class="left"></div>
        <div class="center"></div>
        <div class="right"></div>
    </article>
  </section>

css:

.gridLayout .left-center-right{
      display:grid;
      width:100%;
      grid-template-rows:100px;
      grid-template-columns:300px auto 300px;
   }
.left-center-right .left{
      background:red;
  }
  .left-center-right .right{
      background:red;
  }
  .left-center-right .center{
      background:green;
  }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,868评论 1 92
  • 前言 温馨提示:本文较长,图片较多,本来是想写一篇 CSS 布局方式的,但是奈何 CSS 布局方式种类太多并且实现...
    sunshine小小倩阅读 3,205评论 0 59
  • 简介 CSS Grid布局 (又名"网格"),是一个基于二维网格布局的系统,旨在改变我们基于网格设计的用户界面方式...
    咕咚咚bells阅读 2,584评论 0 4
  • 一:在制作一个Web应用或Web站点的过程中,你是如何考虑他的UI、安全性、高性能、SEO、可维护性以及技术因素的...
    Arno_z阅读 1,277评论 0 1
  • 单例模式(Singleton Pattern) 前言 😊 按照 001 篇讲的,以后的每个模式都将按照:模式名称、...
    mliumeng阅读 506评论 1 3