css中心自适应布局的5种解法详解
前言
在做网页时,我们每每会遇到网页布局相干的内容,面试时也经常会被问到,那么今天我就来总结一下关于网页布局的内容。
题目:如何实现三栏布局(高度固定,左中右的结构)
假设高度已知,请写出三栏布局,其中左右宽度均为300px,中心自适应。
看了上面的标题,有经验的人大概会觉得很简单,细心想想,假如我们来写,能写出几种方案呢?一样平常都会想到两种吧,float和position定位,其实除了这两种外,还有3种可以写,下面我就来逐一介绍一下:
方案一(float浮动)
<section class='layout float'>
<style>
.layout.float .left-right-center{
height: 100px;
}
.layout.float .left{
float: left;
width: 300px;
background-color: red;
}
.layout.float .right{
float: right;
width: 300px;
background-color: blue;
}
.layout.float .center{
background-color: yellow;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">我是中心的自适应元素--浮动</div>
</article>
</section>原理:左右两个div因为浮动离开了文档流,center就会上移,造成三栏布局的结果(前提是高度雷同)
好处:兼容性高
瑕玷:必要消灭浮动来防止影响其他元素
假如高度不固定,中心的内容会被撑开,左右两边不会一路撑开
方案二(绝对定位)
<section class="layout absolute">
<style>
.layout.absolute .left-center-right div{
position: absolute;
height: 100px;
}
.layout.absolute .left{
left: 0;
width: 300px;
background-color: red;
}
.layout.absolute .center{
left: 300px;
right: 300px;
background-color: yellow;
}
.layout.absolute .right{
right: 0;
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中心的自适应元素--绝对定位
</div>
<div class="right"></div>
</article>
</section>原理:行使绝对定位以及宽度,将左右两边的div固定住,中心div的宽度就会有自适应的结果
好处:快捷
瑕玷:假如父元素离开了文档流,子元素肯定会离开文档流,运用的场景不多
假如中心元素的高度增长,两边元素的高度不会增长,所以只有中心的div会撑开
方案三(flex布局)
<section class="layout flex">
<style>
.layout.flex .left-center-right{
display: flex;
height: 100px;
}
.layout.flex .left{
width: 300px;
background-color: red;
}
.layout.flex .center{
flex: 1;
background-color: yellow;
}
.layout.flex .right{
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中心的自适应元素--flex布局
</div>
<div class="right"></div>
</article>
</section>原理:将父元素设置为flex布局,然后中心元素设置flex为1,达到自适应的结果
好处:在现实开发中常用
瑕玷:IE8及以下的欣赏器不支持
假如高度不固定,中心内容的高度撑开后,两边也会随之撑开
方案四(table布局)
<section class="layout table">
<style>
.layout.table .left-center-right{
display: table;
height: 100px;
width: 100%;
}
.layout.table .left{
display: table-cell;
width: 300px;
background-color: red;
}
.layout.table .center{
display: table-cell;
background-color: yellow;
}
.layout.table .right{
display: table-cell;
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中心的自适应元素--table
</div>
<div class="right"></div>
</article>
</section>原理:将父元素设置为table布局,然后每个子元素都是teble-cell,给左右两个格子设置固定的宽度,中心的格子就可以达到自适应的结果
好处:兼容性好,可做flex布局在ie8以下的代替
瑕玷:局限性
假如高度不固定,中心被撑开时,左右两边也会被撑开,和flex类似
方案五(网格布局)
<section class="layout grid">
<style>
.layout.grid .left-center-right{
display: grid;
width: 100%;
grid-template-rows: 100px;/*每一格都是100px高*/
grid-template-columns: 300px auto 300px;/*左右两格都是300px,中心是自适应*/
}
.layout.grid .left{
background-color: red;
}
.layout.grid .center{
background-color: yellow;
}
.layout.grid .right{
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中心的自适应元素--grid布局
</div>
<div class="right"></div>
</article>
</section>原理:将父元素设置为网格布局,然后规定每格的高度以及每格的宽度,只用分别给每格单独设置颜色即可
好处:技术比较新,方便
瑕玷:兼容性不是很好
假如高度不固定,中心元素添加文本,也不会撑开
以上就是本文的悉数内容,盼望对大家的学习有所帮助,也盼望大家多多支持图趣网。
本文地址:http://tuquu.com/tutorial/wd502.html
-
Macaron 马卡龙系色谱 ;附HC(16进制码)
Macaron 马卡龙系颜色表 色谱/色卡/色表 扩展阅读:马卡龙原指(Macaron
-
@media适配不同尺寸的手机
当下,不同分辨率,不同尺寸的移动设备越来越多,自适应也成了最热门的话题,下面就是图趣网整理到的
-
干货分享!推荐8个高清无水印无版权可商用图
在我们写文章的时候经常需要给文章配一个好图,或者是bannar等等,这就需要我们寻找一些好的无水印
-
国外黄色网站系列欣赏之一
黄色也是一个暖色。它有大自然、阳光、春天的涵义,而且通常被认为是一个快乐和有希望的色彩,黄色
-
CutterMan-一个好用的ps切图插件
cutterman--ps切图工具,方便快捷的用于网页开发/IOS/Android切图功能
-
ps像素图放大边缘模糊?一步搞定
ps像素图放大边缘模糊?我们将分享一个ps无损放大二维码的更方便快捷的实例...
-
庞门正道:好好玩耍的点线面(上)
庞门正道:好好玩耍的点线面(上)
-
经典网页配色方案之无色系
今天跟大家分享无色系配色---指除了彩色以外的其它颜色,常见的有金、银、黑、白、灰。明度从0变化

