前端面试—CSS三栏布局实现方式

100人浏览   2024-08-21 11:00:10

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

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Layout</title>

<style media="screen">

html *{

padding: 0;

margin: 0;

}

.layout article 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>

</head>

<body>

<!--浮动布局 -->

<section class="layout float">

<h1>三栏布局</h1>

<article class="left-right-center">

<div class="left"></div>

<div class="right"></div>

<div class="center">

<h2>浮动解决方案</h2>

</div>

</article>

</section>

<!-- 绝对布局 -->

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

</div>

<div class="right"></div>

</article>

</section>

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

</div>

<div class="right"></div>

</article>

</section>

<!-- 表格布局 -->

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

</div>

<div class="right"></div>

</article>

</section>

<!-- 网格布局 -->

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

</div>

<div class="right"></div>

</article>

</section>

</body>

</html>

能写出以上五种来,已经很优秀了,但是面试官可能会继续追问以下问题:

1.这5种方案的优缺点是什么?

(1)浮动方案

优点:兼容性好

缺点:在于需要清除浮动,浮动以后是脱离文档流,如果处理不好会带来很多问题,这是它本身的局限性。

(2)绝对定位

优点:快捷

缺点:布局已经脱离文档流,意味着里面所有的子元素也必须脱离文档流,就导致了有效性/可使用性比较差

(3)flex布局

优点:CSS3中用于解决上面两个方案的不足出现的,比较完美的一个方案,移动端普遍使用

缺点:兼容性问题(IE8以上可用)

(4)table布局

兼容性好(比flex好)例子中,中间内容多撑开后两侧容器也会自动增高(同时也是自身的不足,选择哪个方案看具体的业务,没有绝对的优缺点)

(5)网格grid布局

一些CSS框架在做网格的事情,例如栅格系统,960px宽度设计12列,模拟网格控制每一部分的位置达到布局的方式,在新出的网格布局中,通过把它标准化也就是CSS开始支持,可以做很多复杂事情,同时代码量简化的多

2.如果把假设高度已知去掉,比如,中间部分的内容太多把容器撑高了,两侧的容器也要相应增高,这样哪种方案就不适用了?

flex布局和table布局可以继续使用,其它的不适用(两侧没有自动增高)。

3.5种方案的兼容性如何?如果让你真正去写业务中的页面布局,那最优的方案是哪个?

答案可以参考第1题

-

相关推荐