美文网首页
前端面试系列:页面布局之三栏布局

前端面试系列:页面布局之三栏布局

作者: 乌龟小姐v | 来源:发表于2020-01-02 18:18 被阅读0次
假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px;中间自适应。
  • 浮动
  <section class="layout float">
    <style media="screen">
      * {
        margin: 0;
        padding: 0;
      }
      .left-center-right div {
        min-height: 100px;
      }
      .left {
        float: left;
        background: red;
        width: 300px;
      }
      .right {
        float: right;
        background: blue;
        width: 300px;
      }
      .center {
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="right"></div>
      <div class="center">
        <h2>浮动布局</h2>
        1、浮动布局左右固定宽度,中间自适应
        2、浮动布局左右固定宽度,中间自适应
      </div>
    </article>
  </section>
  • 绝对定位
  <section class="layout position">
    <style media="screen">
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right div {
        position: absolute;
        min-height: 100px;
      }
      .left {
        left: 0;
        width: 300px;
        background: red;
      }
      .right {
        right: 0;
        width: 300px;
        background: blue;
      }
     .center {
        left: 300px;
        right: 300px;
        background: yellow;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>绝对定位</h2>
        1、定位布局左右固定宽度,中间自适应
        2、定位布局左右固定宽度,中间自适应
      </div>
      <div class="right"></div>
    </article>
  </section>
  • flex布局
<section class="layout flex">
    <style>
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right div{
        min-height: 100px;
      }
      .left-center-right {
        display: flex;
      }
      .left {
        width: 300px;
        background: red;
        order: -1;
        /* order属性定义项目的排列顺序 数值越小 排列越靠前 默认为0 */     
      }
      .center {
        flex: 1;
        background: yellow;
      }
      .right {
        width: 300px;
        background: blue;
      }
    </style>
    <article class="left-center-right">
      <div class="center">
        <h2>flex布局</h2>
        1、flex布局左右固定宽度,中间自适应
        2、flex布局左右固定宽度,中间自适应
      </div>
      <div class="left"></div>
      <div class="right"></div>
    </article>
  </section>
  • 表格布局
  <section class="layout table">
    <style>
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right {
        display: table;
        width: 100%;
        height: 100px;
      }
      .left-center-right div {
        display: table-cell;
        min-height: 100px;
      }
      .left {
        background: red;
        width: 300px;
      }
      .center {
        background: yellow;
      }
      .right {
        background: blue;
        width: 300px;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        <h2>tabale布局</h2>
        1、table布局左右固定宽度,中间自适应
        2、table布局左右固定宽度,中间自适应
      </div>
      <div class="right"></div>
    </article>
  </section>
  • 网格布局
 <section class="layout grid">
    <style media="screen">
      html * {
        margin: 0;
        padding: 0;
      }
      .left-center-right {
        display: grid;
        width: 100%;
        grid-template-rows: 100px; 
        grid-template-columns: 300px auto 300px;
      }
      .left-center-right div {
        min-height: 100px;
      }
      .left {
        background: red;
      }
      .center {
        background: yellow;
      }
      .right {
        background: blue;
      }
    </style>
    <article class="left-center-right">
      <div class="left"></div>
      <div class="center">
        1、网格布局左右固定宽度,中间自适应
        2、网格布局左右固定宽度,中间自适应
      </div>
      <div class="right"></div>
    </article>
  </section>
总结:

1、这五种解决方案各自的优缺点

  • 浮动:缺点是脱离文档流的,需要做清浮动处理;优点是兼容性很好。
  • 绝对定位:缺点是布局脱离文档流,子元素也必须脱离文档流,可使用性比较差。优点是快捷,不容易出问题。
  • flex布局:为解决以上两种布局方式的不足而出现的,比较完美,目前移动端基本都属于flex布局。
  • 表格布局:缺点是操作繁琐,对seo也不友好,当其中一个单元格的高度变大时,其他单元格的高度也会随之变大。优点是兼容性非常好。
  • 网格布局:缺点是新出的技术,低版本浏览器兼容性不是很好,优点是可以做许多复杂的事情,但是代码量要简化的多。

2、当高度不定时,两侧的高度也跟这中间的高度撑开,以上五种有哪几种可以继续用,哪几种不能用了?
通过看效果:浮动和绝对定位是不能用的,flex布局、表格布局和网格布局可以继续使用。

相关文章

  • 前端面试系列:页面布局之三栏布局

    假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px;中间自适应。 浮动 绝对定位 flex布局 表格布...

  • 前端

    前端页面布局前端页面布局——三栏布局 - magi的博客 - CSDN博客 页面高度,位置简述前端页面内的高度、位...

  • 入门任务11

    单栏布局三栏布局圣杯布局双飞翼布局页面范例

  • 实现三栏布局5种方式

    前言 前端开发中,布局是前端基础的内容,而其中三栏布局在实际开发和面试中最常见,三栏布局是指中间自适应两边固定宽,...

  • css入门11

    单栏布局三栏布局圣杯布局双飞翼布局实现如下页面

  • 回顾经典的css布局结构--圣杯和淘宝双飞翼

    前端页面的三栏布局为常见的展现结构,中间栏(main)的宽度不确定,左右两栏的宽度固定。本文总结回顾经典的圣杯布局...

  • CSS布局面试题

    页面布局 题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应 页面布局的变通 两栏布...

  • 经典页面布局学习

    总结几种常用的页面布局 上中下布局 页面运行效果: 左右两栏布局 页面运行效果: 左右两栏纯浮动实现宽度固定,不能...

  • css中常见的布局方式

    三栏布局 三栏布局是页面中比较常见的布局方式,也有好几种实现方式,分别是flex布局,grid布局,float布局...

  • 三栏布局——五种解决方案

    面试常见考题:三栏布局 浮动布局 绝对定位 flex布局 table-cell布局 grid布局

网友评论

      本文标题:前端面试系列:页面布局之三栏布局

      本文链接:https://www.haomeiwen.com/subject/rdkooctx.html