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. sprigboot项目中配置xml格式的logback

    slf4j依赖和logback的依赖 idea中springboot项目的resources目录下新建logback-spring.xml文件,内容大致如下: <?xml version=&qu ...

  2. LeetCode_933-Number of Recent Calls

    求最近3000毫秒内有多少次调用请求,每一次ping的时间一定比上一次的时间高:解法可以判断最后面一个数t1与最前一个数t2的差不大于3000毫秒,如果大于就直接舍弃,t1与t2之间的个数就是请求次数 ...

  3. Python高阶函数及函数柯里化

    1 Python高阶函数 接收函数为参数,或者把函数作为结果返回的函数为高阶函数. 1.1 自定义sort函数 要求:仿照内建函数sorted,自行实现一个sort函数.内建函数sorted函数是返回 ...

  4. PowerBI系列之入门案例动态销售报告

    本文将讲解如何从零开始使用PowerBI Desktop制作一份动态销售报告.帮助大家快速入门PowerBI Desktop的操作.我们先来看一下一份动态销售报告的构成. 1.左上角放置了小黎子数据分 ...

  5. vue-cli 中stylus写样式莫名报错?

    报错一: expected "indent", got "eos" 错误截图如下: 在确认stylus安装无误后,我们应该看看是否stylus代码不符合规范. ...

  6. 全平台正向tcp端口转发工具rinetd的使用

    Linux下做地址NAT有很多种方法.比如haproxy.nginx的4层代理,linux自带的iptables等都能实现.其实,Linux下有一个叫rinetd的工具,安装简单,配置也不复杂. 下载 ...

  7. css 文字间距

    letter-spacing :  字与字之间的距离 text-indent : 行的抬头间距 line-height : 行高度

  8. 【RabbitMQ 实战指南】一 RabbitMQ 开发

    1.RabbitMQ 安装 RabbitMQ 的安装可以参考官方文档:https://www.rabbitmq.com/download.html 2.管理页面 rabbitmq-management ...

  9. 【python数据分析实战】电影票房数据分析(二)数据可视化

    目录 图1 每年的月票房走势图 图2 年票房总值.上映影片总数及观影人次 图3 单片总票房及日均票房 图4 单片票房及上映月份关系图 在上一部分<[python数据分析实战]电影票房数据分析(一 ...

  10. 百万年薪python之路 -- 软件的开发规范

    一. 软件的开发规范 什么是开发规范?为什么要有开发规范呢? 你现在包括之前写的一些程序,所谓的'项目',都是在一个py文件下完成的,代码量撑死也就几百行,你认为没问题,挺好.但是真正的后端开发的项目 ...