页面布局--三栏布局

页面布局

题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应
延伸而出的问题
1、每个方案的优缺点

  • 浮动问题
    优点:兼容性比较好
    缺点:浮动后是脱离文档流的,要清除浮动
  • 绝对定位
    优点:快捷,也不容易出问题
    缺点:因为已经脱离文档流,意味着子元素也脱离了文档流,导致这个方案的可使用性比较差
  • flex布局
    css3新出现的布局,就是为了解决上面两个的不足。尤其是在移动端,所以是比较完美的一个
  • 表格布局
    优点:兼容性好,当要支持ie8的时候,flex不兼容可以考虑表格布局
    缺点:除了历史上诟病的以外,还有一个就是三栏布局,其中一栏增高的时候,其它两个也会增高,有的场景是不需要联动增高的。
  • 网格布局
    因为之前栅格布局的出现,css专门出的网格布局技术,用少量简单的代码实现以前复杂的场景
    2、把已知高度去掉,哪个不在适用
    通过调试,缩小浏览器宽度或或添加center内容,证明只有flex和table布局是完好的。
    3、它们的兼容性,业务中哪个最实用
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>layout</title>
  <style>
    html * {
      padding: 0;
      margin: 0;
    }

    .layout {
      margin-top: 20px;
    }

    .layout article div {
      min-height: 100px;
    }
  </style>
</head>

<body>
  <!-- 浮动布局 -->
  <section class="layout float">
    <style>
      .layout.float .left {
        float: left;
        width: 300px;
        background-color: red;
      }

      .layout.float .right {
        float: right;
        width: 300px;
        background-color: blue;
      }

      .layout.float .center {
        background: yellow;
      }
    </style>
    <article class="left-right-center">
      <div class="left"></div>
      <div class="right"></div>
      <div class="center">
        <h1>浮动解决方案</h1>
        1、这是三栏布局中间部分
        1、这是三栏布局中间部分
      </div>
    </article>
  </section>

  <!-- 绝对定位布局 -->
  <section class="layout absolute">
    <style>
      .layout.absolute .left-center-right>div {
        position: absolute;
      }

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

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

      .layout.absolute .right {
        width: 300px;
        right: 0;
        background: blue;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>绝对定位解决方案</h2>
        1、这是三栏布局中间部分
        1、这是三栏布局中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

  <!-- flexbox -->
  <section class="layout flexbox">
    <style>
      .layout.flexbox {
        margin-top: 140px;
      }

      .layout.flexbox .left-center-right {
        display: flex;
      }

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

      .layout.flexbox .right {
        width: 300px;
        background: blue;
      }

      .layout.flexbox .center {
        flex: 1;
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>flexbox解决方案</h2>
        1、这是三栏布局中间部分
        1、这是三栏布局中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

  <!-- 表格布局 -->
  <section class="layout table">
    <style>
      .layout.table .left-center-right {
        width: 100%;
        display: table;
        height: 100px;
      }

      .layout.table .left-center-right>div {
        display: table-cell;
      }

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

      .layout.table .right {
        width: 300px;
        background: blue;
      }

      .layout.table .center {
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>table解决方案</h2>
        1、这是三栏布局中间部分
        1、这是三栏布局中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>

  <!-- 网格布局 -->
  <section class="layout grid">
    <style>
      .layout.grid .left-center-right {
        display: grid;
        width: 100%;
        grid-template-rows: 100px;
        grid-template-columns: 300px auto 300px;
      }

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

      .layout.grid .right {
        width: 300px;
        background: blue;
      }

      .layout.grid .center {
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>grid解决方案</h2>
        1、这是三栏布局中间部分
        1、这是三栏布局中间部分
      </div>
      <div class="right"></div>
    </article>
  </section>
</body>

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

推荐阅读更多精彩内容