页面布局

多种方式实现三栏布局

一、float布局

<section class="layout float">
    <style type="text/css" media="screen">
      .layout.float .wrapper>div {
        min-height: 100px;
      }
      .layout.float .left {
        float: left;
        width: 300px;
        background: red;
      }
      .layout.float .center {
        background: yellow;
      }
      .layout.float .right {
        float: right;
        width: 300px;
        background: blue;
      }
    </style>
    <article class="wrapper">
      <div class="left"></div>
      <div class="right"></div>
      <div class="center">
        <h1>float布局</h1>
        1.我是float布局的中间部分
        2.我是float布局的中间部分
      </div>
    </article>
  </section>

优点:设置简单,兼容性比较好。注意需要清除浮动。
缺点:浮动元素是脱离文档流,清除浮动处理不好会出现高度塌陷等问题。

二、绝对布局

<section class="layout absolute">
    <style type="text/css" media="screen">
      .layout.absolute .wrapper {
        width: 100%;
        margin-top: 20px;
      }

      .layout.absolute .wrapper>div {
        min-height: 100px;
      }

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

      .layout.absolute .center {
        position: absolute;
        left: 300px;
        right: 300px;
        background: yellow;
      }

      .layout.absolute .right {
        position: absolute;
        right: 0;
        width: 300px;
        background: blue;
      }
    </style>
    <article class="wrapper">
      <div class="left"></div>
      <div class="center">
        <h1>absolute布局</h1>
        1.我是absolute布局的中间部分
        2.我是absolute布局的中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

优点:设置简单,兼容性比较好。
缺点:绝对定位是脱离文档流的,下面的所有子元素也会脱离文档流,导致这种方法的有效性和可使用性是比较差的。

三、flex布局

<section class="layout flex">
    <style type="text/css" media="screen">
      .layout.flex .wrapper {
        width: 100%;
        min-height: 100px;
        display: flex;
        margin-top: 140px;
      }

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

      .layout.flex .center {
        flex: 1;
        background: yellow;
      }

      .layout.flex .right {
        width: 300px;
        background: blue;
      }
    </style>
    <article class="wrapper">
      <div class="left"></div>
      <div class="center">
        <h1>flex布局</h1>
        1.我是flex布局的中间部分
        2.我是flex布局的中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

优点:设置简单。
缺点:不支持 IE8 及以下。

四、table布局

<section class="layout table">
    <style type="text/css" media="screen">
      .layout.table .wrapper {
        display: table;
        width: 100%;
        min-height: 100px;
        margin-top: 20px;
      }

      .layout.table .left {
        display: table-cell;
        width: 300px;
        background: red;
      }

      .layout.table .center {
        display: table-cell;
        background: yellow;
      }

      .layout.table .right {
        display: table-cell;
        width: 300px;
        background: blue;
      }
    </style>
    <article class="wrapper">
      <div class="left"></div>
      <div class="center">
        <h1>table布局</h1>
        1.我是table布局的中间部分
        2.我是table布局的中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

优点:设置简单。
缺点:当其中一个单元格高度超出的时候,两侧的单元格也是会跟着一起变高的,而有时候这种效果不是我们想要的。

五、grid布局

<section class="layout grid">
    <style type="text/css" media="screen">
      .layout.grid .wrapper {
        display: grid;
        grid-template-columns: 300px auto 300px;
        grid-template-rows: 100px;
        width: 100%;
        margin-top: 20px;
      }

      .layout.grid .left {
        background: red;
      }

      .layout.grid .center {
        background: yellow;
      }

      .layout.grid .right {
        background: blue;
      }
    </style>
    <article class="wrapper">
      <div class="left"></div>
      <div class="center">
        <h1>grid布局</h1>
        1.我是grid布局的中间部分
        2.我是grid布局的中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

优点:设置简单。
缺点:不支持 IE8 及以下。

六、参考

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

http://www.ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

水平垂直居中的方法 Previous
设计模式(一) Next