页面布局

1、页面布局

题目:假设高度已知,请写出三栏布局,其中左栏,右栏宽度各位300px,中间自适应。

方法1:浮动

 <!--浮动布局  -->
    <style media="screen">
      html *{
        padding: 0;
        margin: 0;
      }
      .layout article div{
        min-height: 100px;
      }
    </style>
    <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>

 

方法2:绝对定位

  <!-- 绝对布局 -->
    <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>

方法3:flex

  <!-- 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>

方法4:table

    <!-- 表格布局 -->
    <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>

方法5:grid布局

  <!-- 网格布局 -->
    <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>

 讨论:

以上五种方法的优缺点:

1、浮动布局 优点:兼容性好  缺点:要记得清除浮动

2、绝对定位布局 优点:配合js使用非常快捷  缺点:因为布局已经脱离文档流,所以你的子元素也脱离文档流,实用性较差。

3、flex布局 优点:解决了上述两个方法的不足  缺点:不兼容ie8

4、表格布局  优点:适合三栏布局,兼容较好  

5、网格布局  

以上五种方法让内容撑开高度时,哪个方法可以实现

只有flex布局和表格布局能够实现

posted @ 2018-02-08 18:39  未知小未来  阅读(156)  评论(0编辑  收藏  举报