美文网首页
前端面试题(1)——页面布局

前端面试题(1)——页面布局

作者: 言歌不言歌 | 来源:发表于2018-03-25 11:58 被阅读0次

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

屏幕快照 2018-03-25 上午11.28.45.png

我这边总结了五种方法,如有不正确,欢迎批评指正~

  • 浮动
  • 绝对定位
  • flex布局
  • 表格布局
  • 网格布局

一、浮动

    <section class="layout float">
        <style media="screen">
            html * {
                margin: 0;
                padding: 0;
            }

            .layout article div {
                min-height: 100px;
            }

            .layout.float .left {
                float: left;
                background: red;
                width: 300px;
            }
            
            .layout.float .right {
                float: right;
                background: blue;
                width: 300px;
            }
            
            .layout.float .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;
            }

            .layout article div {
                min-height: 100px;
            }

            .layout.position .left-center-right>div {
                position: absolute;
            }
            
            .layout.position .left {
                left: 0;
                width: 300px;
                background: red;
            }
            
            .layout.position .right {
                right: 0;
                width: 300px;
                background: blue;
            }
            
            .layout.position .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;
            }

            .layout article div {
                min-height: 100px;
            }

            .layout.flex .left-center-right {
                display: flex;
            }
            
            .layout.flex .left {
                width: 300px;
                background: red;
            }
            
            .layout.flex .center {
                flex: 1;
                background: yellow;
            }
            
            .layout.flex .right {
                width: 300px;
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>flex布局</h2>
                1、flex布局左右固定宽度,中间自适应 
                2、flex布局左右固定宽度,中间自适应
            </div>
            <div class="right"></div>
        </article>
    </section>

四、表格布局

    <section class="layout table">
        <style>
            html * {
                margin: 0;
                padding: 0;
            }

            .layout article div {
                min-height: 100px;
            }

            .layout.table .left-center-right {
                display: table;
                height: 100px;
            }
            
            .layout.table .left-center-right>div {
                display: table-cell;
            }
            
            .layout.table .left {
                background: red;
                width: 300px;
            }
            
            .layout.table .center {
                background: yellow;
            }
            
            .layout.table .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;
            }

            .layout article div {
                min-height: 100px;
            }

            .layout.grid .left-center-right {
                display: grid;
                width: 100%;
                grid-template-rows: 100px;
                grid-template-columns: 300px auto 300px;
            }
            
            .layout.grid .left {
                background: red;
            }
            
            .layout.grid .center {
                background: yellow;
            }
            
            .layout.grid .right {
                background: blue;
            }
        </style>
        <article class="left-center-right">
            <div class="left"></div>
            <div class="center">
                <h2>网格布局</h2>
                1、网格布局左右固定宽度,中间自适应 
                2、网格布局左右固定宽度,中间自适应
            </div>
            <div class="right"></div>
        </article>
    </section>

总结

  1. 这五种解决方案各自的优缺点;
  • 浮动:缺点浮动是脱离文档流的,需要做清浮动处理;优点是兼容性很好。
  • 绝对定位:缺点是布局脱离文档流,子元素也必须脱离文档流,可使用用性比较差;优点是快捷,不容易出问题。
  • flex布局:为解决以上两种布局方式的不足而出现的,比较完美,现在移动端基本都属于flex布局。
  • 表格布局:缺点是操作繁琐,对seo也不友好,当其中一个单元格的高度变大时,其他单元格的高度也会随之变大,优点是兼容性非常好。
  • 网格布局:缺点是新出的技术,低版本浏览器兼容性不是很好,优点是可以做许多复杂的事情,但是代码量要简化的多。
  1. 当高度不定时,两侧的高度也跟这中间的高度撑开,以上五种有哪几种可以继续用,哪几种不能用了;
     <article class="left-center-right">
         <div class="left"></div>
         <div class="center">
             1、网格布局左右固定宽度,中间自适应 
             2、网格布局左右固定宽度,中间自适应
             <p>增加高度</p>
             <p>增加高度</p>
             <p>增加高度</p>
             <p>增加高度</p>
             <p>增加高度</p>
             <p>增加高度</p>
         </div>
         <div class="right"></div>
     </article>
  • 通过看效果:浮动和绝对定位以及网格布局是不能用的,flex布局和表格布局可以继续使用。
  • 在这个面试过程中一定要说出哪个能用,哪个不能用,以及不能用的原因。
  • 浮动可以延伸到BFC,可以去了解BFC相关的知识;
  1. 兼容性;如果现在要在业务中实现,你觉得最优选择时哪种;

页面布局小结

  1. 语义化掌握要到位
  2. 页面布局理解深刻
  3. CSS基础知识扎实
  4. 思维灵活且积极上进(对新技术的了解,如网格布局)
  5. 代码书写规范

相关文章

网友评论

      本文标题:前端面试题(1)——页面布局

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