1.前言

用css实现“两边定宽,中间自适应的三栏布局”这个问题应该是在前端面试中被面试官提问到的高频问题了,一般当面试者写出一种实现方法之后,面试官还会问你还有没有别的方法,尽量多的写出几种实现方法。

2.实现原理

要想实现这种“两边定宽,中间自适应的三栏布局”其实也不难,无外乎把握住以下几点:

  • 要想中间自适应,那么中间的div不能设置宽度width,这样宽度就会随着浏览器窗口自适应。
  • 由于div是块级元素,要想三个div处于同一行,那么两边的div就要脱离文档流。
  • CSS3的flex伸缩布局。
  • 表格table布局。
  • 网格布局。

3.具体实现

下面就按照上面所说的实现原理,列举以下几种实现方式:

3.1 浮动解决方案

<style>
.layout-float .right{
float: right;
width: 300px;
height: 100px;
background-color: red;
}
.layout-float .left{
float: left;
width: 300px;
height: 100px;
background-color: blue;
}
.layout-float .center{
height: 100px;
background-color: yellow;
}
</style>
<div class="right"></div>
<div class="left"></div>
<div class="center"><h1>浮动解决方案</h1></div>

3.2 绝对定位解决方案

<style>
.layout-absolute .right{
width: 300px;
height: 100px;
right: 0px;
position: absolute;
background-color: red;
}
.layout-absolute .left{
width: 300px;
height: 100px;
left: 0px;
position: absolute;
background-color: blue;
}
.layout-absolute .center{
left: 300px;
right: 300px;
height: 100px;
position: absolute;
background-color: yellow;
}
</style>
<div class="left"></div>
<div class="center"><h1>绝对定位解决方案</h1></div>
<div class="right"></div>

3.3 Flex伸缩布局解决方案

<style>
.layout-flex .left-center-right{
display: flex;
}
.layout-flex .left-center-right .left{
width: 300px;
height: 100px;
background-color: blue;
}
.layout-flex .left-center-right .center{
flex:;
background-color: yellow;
}
.layout-flex .left-center-right .right{
width: 300px;
height: 100px;
background-color: red;
}
</style>
<div class="left-center-right">
<div class="left"></div>
<div class="center"><h1>Flex伸缩布局解决方案</h1></div>
<div class="right"></div>
</div>

3.4 表格布局解决方案

<style>
.layout-table .left-center-right{
width: 100%;
display: table;
height: 100px;
}
.layout-table .left-center-right>div{
display: table-cell;
}
.layout-table .right{
width: 300px;
height: 100px;
background-color: red;
}
.layout-table .left{
width: 300px;
height: 100px;
background-color: blue;
}
.layout-table .center{
height: 100px;
background-color: yellow;
}
</style>
<div class="left-center-right">
<div class="left"></div>
<div class="center"><h1>表格布局解决方案</h1></div>
<div class="right"></div>
</div>

3.5 网格布局解决方案

<style>
.layout-grid .left-center-right{
width: 100%;
display: grid;
grid-template-rows: 100px;
grid-template-columns: 300px auto 300px;
}
.layout-grid .right{
background-color: red;
}
.layout-grid .left{
background-color: blue;
}
.layout-grid .center{
background-color: yellow;
}
</style>
<div class="left-center-right">
<div class="left"></div>
<div class="center"><h1>网格布局解决方案</h1></div>
<div class="right"></div>
</div>

4. 效果图

(完)

如何用CSS实现中间自适应,两边定宽三栏布局的更多相关文章

  1. css高度已知,左右定宽,中间自适应三栏布局

    css高度已知,左右定宽,中间自适应三栏布局: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. 记一道css面试题 : 三栏布局两边宽度固定,中间宽度自适应,并且布局随屏幕大小改变。

    前几天面试时有道css题没做出来,回来好好学习一番后把其记录下来. 题目是这样的:左中右三栏布局,左右两栏宽度固定,左右两栏的宽度为200像素,中间栏宽度自适应.当屏幕小于600px时,3栏会分别占用 ...

  3. 如何用CSS实现左侧宽度固定,右侧自适应(两栏布局)?左右固定中间自适应(三栏布局)呢?

    在前端日常布局中,会经常遇到左侧宽度固定,右侧自适应或者左右两边固定,中间部分自适应的实用场景.本文例子中将列举出两种常用的两栏布局,左侧固定右侧自适应的常用方法以及代码和五种左右固定中间自适应的常用 ...

  4. Css三栏布局自适应实现几种方法

    Css三栏布局自适应实现几种方法 自适应实现方法我们可以从三个方法来做,一个是绝对定位 ,自身浮动法 和margin负值法了,下面我们一起来看看这三个例子吧,希望例子能帮助到各位同学. 绝对定位法三栏 ...

  5. CSS | 圣杯布局、双飞翼布局 | 自适应三栏布局

    圣杯布局和双飞翼布局是前端工程师需要日常掌握的重要布局方式.两者的功能相同,都是为了实现一个两侧宽度固定,中间宽度自适应的三栏布局 虽然两者的实现方法略有差异,不过都遵循了以下要点: 1.两侧宽度固定 ...

  6. 前端经典面试题之CSS实现三栏布局,左右宽度固定,中间宽度自适应

    前端常问的面试题,题目:假设高度一定,请写出三栏布局,左右宽度300px,中间自适应. 看到这里我希望你能停下来思考几分钟, 1分钟~2分钟~3分钟~4分钟~5分钟! 好了,那么你想出了几种答案呢? ...

  7. css中,在高度已知,写出三栏布局,其中左栏、右栏宽度各位300px,中间自适应

    解决方案主要有五种 首先写入全局样式 <style type="text/css"> html * { margin: ; padding: ; } .layout { ...

  8. CSS布局 - 三栏布局

    CSS布局技术可谓是前端技术中最基础的技术,就是因为基础,所以我认为要更加熟练,深入的去掌握,去梳理. 一. 传统 ---> 浮动实现的三栏布局 采用浮动实现的三栏布局有以下特点及注意事项: · ...

  9. CSS三栏布局

    一.绝对定位 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

随机推荐

  1. Java如何安装JDK,配置环境变量。超级详细图及操作

    突然想起自己大学刚接触java的时候,要下载JDK和配置环境变量,那时候我上网找了很多教学,结果发现很多的博主都是表达不太清晰,或者是我理解能力差点,导致我那时候搞了一个多小时才搞定,而且事后每次我重 ...

  2. MIPI CSI2-TX接口基于FPGA实现

    MIPI CSI2-TX用途: 跟海思的3559A芯片进行图像数据传输: MIPI CSI2-TX接口特性: xilinx 7系列芯片最大支持1.25Gbps: 最大支持lanes数量为4: 支持的图 ...

  3. 模拟telnet协议C语言客户端程序

    首先要了解telnet协议,一下两篇blog给了我初步的思路 https://www.cnblogs.com/liang-ling/p/5833489.html 这篇有比较基础的介绍 以及IAC命令含 ...

  4. MacOS 安装MysqlDB 问题解决方案( 解决 IndexError: string index out of range)

    pip install MySQL-python时报错如下: Command "python setup.py egg_info" failed with error code 1 ...

  5. 8 个 Python 实用脚本,【速】收藏备用!

    脚本写的好,下班下得早!程序员的日常工作除了编写程序代码,还不可避免地需要处理相关的测试和验证工作. 例如,访问某个网站一直不通,需要确定此地址是否可访问,服务器返回什么,进而确定问题在于什么.完成这 ...

  6. redhat5配置网络源

    最近适配了一堆linux系统, Redhat4/5/6, ubuntu 12/14/16, Suse 10/11/12 其中适配到Red5 时候配置网络源 # The mirror system us ...

  7. PHP list的赋值

    List右边的赋值对象是一个以数值为索引的数组,左边的变量的位置和赋值对象的键值一一对应,有些位置的变量可以省略不写.非末尾的被赋值变量省略时,分隔的逗号不能省略.左边变量被赋值的顺序是从右到左的. ...

  8. [Luogu2455] [SDOI2006]线性方程组

    题目描述 已知n元线性一次方程组. 其中:n<=50, 系数是[b][color=red]整数<=100(有负数),bi的值都是整数且<300(有负数)(特别感谢U14968 mmq ...

  9. 关于JavaScript if...else & if 判断简写

    <script type="text/javascript"> 如果你想写 if (!false){ alert('false'); } 不妨考虑写成: false | ...

  10. 使用zepto中animate报错“Uncaught TypeError: this.bind is not a function”的解决办法

    在使用zepto时,我先引入zepto.min.js,然后引入fx.js,但是在使用animate函数时,控制台却报如下错误: Uncaught TypeError: this.bind is not ...