前端常问的面试题,题目:假设高度一定,请写出三栏布局,左右宽度300px,中间自适应。

看到这里我希望你能停下来思考几分钟,

1分钟~2分钟~3分钟~4分钟~5分钟!

好了,那么你想出了几种答案呢?

下面提供这道题的五种解决方案:

首先要写好整个页面的布局(初始化等)

<style>
html * {
padding: 0;
margin: 0;
} .layout {
margin-top: 20px;
} .layout article div {
min-height: 100px;
}
</style>

1.浮动的解决方案

<!-- 浮动布局解决方案 -->
<section class="layout float">
<style>
.layout.float .left {
float: left;
width: 300px;
background: red;
} .layout.float .right {
float: right;
width: 300px;
background: blue;
} .layout.float .center {
background: yellow;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="right"></div>
<div class="center">
<h1>浮动解决方案</h1>
<p>1.这是布局的中间部分</p>
<p>2.这是布局的中间部分</p>
</div>
</article>
</section>

2.绝对定位的解决方案

<!-- 绝对定位的解决方案 -->
<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>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>绝对定位的解决方案</h1>
<p>1.这是布局的中间部分</p>
<p>2.这是布局的中间部分</p>
</div>
<div class="right"></div>
</article>
</section>

3.flexbox的解决方案

<!-- flexbox解决方案 -->
<section class="layout flexbox">
<style>
.layout.flexbox {
margin-top: 140px;
} .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>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>flexbox的解决方案</h1>
<p>1.这是布局的中间部分</p>
<p>2.这是布局的中间部分</p>
</div>
<div class="right"></div>
</article>
</section>

4.表格布局的解决方案

<!-- 表格布局的解决方案 -->
<section class="layout table">
<style>
.layout.table .left-center-right {
width: 100%;
display: table;
height: 100px;
} .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>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
<h1>表格布局的解决方案</h1>
<p>1.这是布局的中间部分</p>
<p>2.这是布局的中间部分</p>
</div>
<div class="right"></div>
</article>
</section>

5.网格布局的解决方案

<!-- 网格布局的解决方案     -->
<section class="layout grid">
<style>
.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">
<h1>网格布局的解决方案</h1>
<p>1.这是布局的中间部分</p>
<p>2.这是布局的中间部分</p>
</div>
<div class="right"></div>
</article>
</section>

更详细的内容可以访问 http://www.waigai.cn

前端经典面试题之CSS实现三栏布局,左右宽度固定,中间宽度自适应的更多相关文章

  1. css 关于两栏布局,左边固定,右边自适应

    好几个星期都没写博客了,最近不忙也不闲,稀里糊涂过了两个星期,之前几个月内天天坚持签到.最近也没签到.哈哈,说正事. 今天做东钿互金平台后台页面,昨天做了一个登录页面,业偶碰到了一个难题.等下也要把它 ...

  2. CSS 实现:两栏布局(一边固定,一边自适应)

    ☊[实现要求]:CSS实现左边固定,右边自适应父容器宽度的两栏布局. <body> <div class="left"></div> <d ...

  3. CSS实现三栏布局(5种)

    常见的布局方式: float布局.Position定位.table布局.弹性(flex)布局.网格(grid)布局 那么我们就是用以上5种方式完成三栏布局,不过前提是左右宽度(假如左右宽度为300px ...

  4. css 实现三栏布局的四种方式

    三栏布局就是左中右,左右两边固定,中间自适应. 1. 绝对定位 <div class="left">左边</div> <div class=" ...

  5. 【CSS】三栏布局的经典实现

    要求:自适应宽度,左右两栏固定宽度,中间栏优先加载: <!DOCTYPE html> <html> <head> <title>layout</t ...

  6. css实现三栏布局,两边定宽,中间自适应

    1.利用定位实现 css代码如下: .box{overflow: hidden;height: 100px;margin: 10px 0;} .box>div{height: 100%;} #b ...

  7. css实现两栏布局,左侧固定宽,右侧自适应的7中方法

    一个面试会问的问题,如何实现两个盒子,左侧固定宽度,右侧自适应. 1.利用 calc 计算宽度的方法 css代码如下: .box{overflow: hidden;height: 100px;marg ...

  8. 三种方法实现CSS三栏布局

    本文由云+社区发表 作者:前端林子 本文会分别介绍三种CSS实现三栏布局的方法,可在浏览器中打开查看效果 1.方法一:自身浮动的方法 实现方法:需要左栏向左浮动,右栏向右浮动,中间设左右margin来 ...

  9. CSS3使用盒模型实现三栏布局

    本篇文章由:http://xinpure.com/css3-box-model-to-implement-a-three-column-layout/ 使用 Position 绝对定位也是可以实现三栏 ...

随机推荐

  1. 小容量的byteBuffer 读取大文本

    利用死循环和判断是否 读到0个字节,便能判断是否读取完成,但它存在如下问题,如果输入是中文的话,可能没有读取完中文的全部3个字节,导致乱码.如果数据足够随机,这样的情况肯定会出现的 @Test pub ...

  2. appium+python3+pycharm踩得坑2

    没相当刚把上一个错误解决,这个马上就解决了: selenium.common.exceptions.WebDriverException: Message: A new session could n ...

  3. 在vim下打开终端

    注意:仅在vim8.1下可用 使用方法: :term 打开默认的终端 如果是linux,或者使用wsl, :term bash Ctrl+W/Ctrl+N 将终端设置成normal模式

  4. 标定版制作(棋盘、圆点、aruco等)

    标定板这个东西,对于双目.立体视觉来说那都是必须的.我们这里提供一些做好的标定板,也提供制作标定板的制作方法 一.基本制作思路(以棋盘标定板为例) 1.  “插入” - “表格” 根据提示选择多少行乘 ...

  5. IDEA 初建Spring项目(Hello Spring)

    新建项目 在文件夹中建立一个项目文件 打开项目 打开IDEA,点击Open,根据所建项目路径找到该项目 依赖注入 点击项目名右键,点击new,点击file,创建pom.xml 内容为: <pro ...

  6. javascript 之 第七章第一节(递归)

    先举例: function factorial(num) { ) { return num; } else { ); } } //输出120 //进一步去思考有名字的函数,且名字不会有变化的情况下,这 ...

  7. maven 安装本地jar

    mvn install:install-file -Dfile=D:/open-api-sdk-2.0.jar -DgroupId=com.jd.open -DartifactId=jd-api-sd ...

  8. vue+vuex 回退定位到初始位置

    先放出两张图(没错,你还在9012,做为一名资深设计师我唯一的技能点就是留白),简单说明下问题未做回退定位(从落地页回退,每次都回到A位置)想死啊有木有,每次都需要手动重新定位来选择,你大哥看到你做个 ...

  9. JavaScript基础数据类型(一)

    动态类型 JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行过程中,类型会被自动确定.这也意味着你可以使用同一个变量保存不同类型的数据: var foo = ...

  10. thinkphp 操作xml格式

    前言:虽然xml的格式看起来跟html差不多,但是最近做项目由于用的是thinkphp5.0的版本,做的过程中还是遇到了一些问题.在这里做一下记录. 首先我们需要定义一个dom对象,我们都知道 php ...