如何用CSS实现左侧宽度固定,右侧自适应(两栏布局)?左右固定中间自适应(三栏布局)呢?
在前端日常布局中,会经常遇到左侧宽度固定,右侧自适应或者左右两边固定,中间部分自适应的实用场景。本文例子中将列举出两种常用的两栏布局,左侧固定右侧自适应的常用方法以及代码和五种左右固定中间自适应的常用方法以及代码
具体实现效果展示如下:

1.二栏布局-flex弹性布局
<!-- flex弹性布局 -->
<div class="title">flex弹性布局:</div>
<div class="box1">
<div class="left1">左边</div>
<div class="right1">右边</div>
</div>
// flex弹性布局
.box1 {
display: flex;
height: 100px;
background: rgb(241, 210, 210);
padding: 10px;
.left1 {
width: 100px;
height: 100%;
background: yellow;
text-align: center;
line-height: 100px;
}
.right1 {
flex: 1;
background: grey;
text-align: center;
line-height: 100px;
}
}
2.二栏布局-左float右margin-left
<!-- 左float右margin-left -->
<div class="title">双栏布局(左float右margin-left):</div>
<div class="box">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
// 双栏布局(左float右margin-left)
.box {
overflow: hidden;
background: rgb(241, 210, 210);
padding: 10px;
.left {
float: left;
width: 200px;
background-color: gray;
height: 100px;
text-align: center;
line-height: 100px;
}
.right {
margin-left: 210px;
background-color: lightgray;
height: 100px;
text-align: center;
line-height: 100px;
}
}
3.三栏布局-两边使用 float,中间使用 margin
<!-- 三栏布局-两边使用 float,中间使用 margin -->
<div class="title">三栏布局-两边使用 float,中间使用 margin:</div>
<div class="box-float-margin">
<div class="left">三栏左侧</div>
<div class="right">三栏右侧</div>
<div class="center">三栏中间</div>
</div>
// 三栏布局-两边使用 float,中间使用 margin
.box-float-margin {
background: rgb(241, 210, 210);
overflow: hidden;
padding: 10px;
height: 100px;
.left {
width: 200px;
height: 100px;
float: left;
background: rgb(80, 255, 182);
text-align: center;
line-height: 100px;
}
.right {
width: 120px;
height: 100px;
float: right;
background: rgb(173, 130, 130);
text-align: center;
line-height: 100px;
}
.center {
margin-left: 220px;
height: 100px;
background: rgb(255, 223, 182);
margin-right: 140px;
text-align: center;
line-height: 100px;
}
}
4.三栏布局-两边使用 absolute,中间使用 margin
<!-- 三栏布局-两边使用 absolute,中间使用 margin -->
<div class="title">三栏布局-两边使用 absolute,中间使用 margin:</div>
<div class="box-absolute">
<div class="left">左边固定宽度</div>
<div class="right">右边固定宽度</div>
<div class="center">中间自适应</div>
</div>
// 三栏布局-两边使用 absolute,中间使用 margin
.box-absolute {
position: relative;
background: rgb(241, 210, 210);
padding: 10px;
div {
height: 100px;
text-align: center;
line-height: 100px;
}
.left {
position: absolute;
top: 10px;
left: 10px;
width: 100px;
background: rgb(128, 119, 119);
}
.right {
position: absolute;
top: 10px;
right: 10px;
width: 100px;
background: rgb(128, 168, 228);
}
.center {
margin: 0 110px;
background: rgb(126, 102, 143);
color: rgb(255, 255, 255);
}
}
5.三栏布局-flex弹性布局
<!-- 三栏布局-flex弹性布局 -->
<div class="title">三栏布局-flex弹性布局:</div>
<div class="box-flex-three">
<div class="left">左边固定宽度</div>
<div class="center">中间自适应</div>
<div class="right">右边固定宽度</div>
</div>
// 三栏布局-flex弹性布局
.box-flex-three {
display: flex;
justify-content: space-between;
background: rgb(241, 210, 210);
padding: 10px;
div {
text-align: center;
line-height: 100px;
height: 100px;
}
.left {
width: 500px;
background: rgb(165, 163, 162);
}
.right {
width: 300px;
background: rgb(209, 123, 226);
}
.center {
background: rgb(7, 245, 7);
width: 100%;
margin: 0 10px;
}
}
6.三栏布局-grid布局
<!-- 三栏布局-grid布局 -->
<div class="title">三栏布局-grid布局:</div>
<div class="box-grid">
<div class="left">左侧</div>
<div class="center">中间</div>
<div class="right">右侧</div>
</div>
// 三栏布局-grid布局
.box-grid {
display: grid;
width: 100%;
grid-template-columns: 300px auto 250px;
div {
text-align: center;
line-height: 100px;
height: 100px;
}
.left {
background: rgb(80, 255, 95);
}
.right {
width: 250px;
background: rgb(23, 127, 161);
}
.center {
background: rgb(54, 131, 73);
}
}
7.三栏布局-display: table
<!-- 三栏布局-display: table -->
<div class="title">三栏布局-display: table:</div>
<div class="box-table">
<div class="left">左边固定宽度</div>
<div class="center">中间自适应</div>
<div class="right">右边固定宽度</div>
</div>
// display: table
.box-table {
height: 100px;
line-height: 100px;
text-align: center;
display: table;
table-layout: fixed;
width: 100%;
.left,
.right {
width: 300px;
background: rgb(228, 20, 183);
display: table-cell;
}
.center {
background: rgb(18, 240, 118);
color: white;
width: 100%;
display: table-cell;
}
}
如何用CSS实现左侧宽度固定,右侧自适应(两栏布局)?左右固定中间自适应(三栏布局)呢?的更多相关文章
- [转]css实现左侧宽度自适应,右侧固定宽度
原文地址:https://segmentfault.com/a/1190000008411418 页面布局中经常用会遇到左侧宽度自适应,右侧固定宽度,或者左侧宽度固定,右侧自适应.总之就是一边固定宽度 ...
- css实现左侧固定宽度,右侧宽度自适应
#centerDIV { height: 550px; width: 100%; } #mainDIV { height: 100%; border: 1px solid #F00; margin-l ...
- CSS布局:Float布局过程与老生常谈的三栏布局
原文见博客主站,欢迎大家去评论. 使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloa ...
- 转:CSS布局:Float布局过程与老生常谈的三栏布局
使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloat属性布局.前者适合布局首页,因为 ...
- CSS 布局实例系列(三)如何实现一个左右宽度固定,中间自适应的三列布局——也聊聊双飞翼
今天聊聊一个经典的布局实例: 实现一个三列布局,其中左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化 可能很多朋友已经笑了,这玩意儿通过双飞翼布局就能轻松实现.不过,还请容我在双飞 ...
- 【CSS】三栏/两栏宽高自适应布局大全
页面布局 注意方案多样性.各自原理.各自优缺点.如果不定高呢.兼容性如何 三栏自适应布局,左右两侧300px,中间宽度自适应 (1) 给出5种方案 方案一: float (左右浮动,中间不用给宽,设置 ...
- CSS布局 - 三栏布局
CSS布局技术可谓是前端技术中最基础的技术,就是因为基础,所以我认为要更加熟练,深入的去掌握,去梳理. 一. 传统 ---> 浮动实现的三栏布局 采用浮动实现的三栏布局有以下特点及注意事项: · ...
- 前端一面/面试常考题1-页面布局:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应。
题目:假设高度已知,请写出三栏布局,其中左栏.右栏宽度各为300px,中间自适应. [题外话:日常宣读我的目标===想要成为一名优雅的程序媛] 一.分析 1. 题目真的像我们想得这么简单吗? 其实不然 ...
- css常见的各种布局下----三列布局
css 三列布局,左右固定宽度右边自适应 1不使用定位,只使用浮动可以实现左右固定,中间宽度自适应布局 1.1.1 自适应部分一定要放第一个位子,使用浮动,并且设置宽度为100%,不设置浮动元素内容不 ...
随机推荐
- pandas常用操作详解——pandas的去重操作df.duplicated()与df.drop_duplicates()
df.duplicated() 参数详解: subset:检测重复的数据范围.默认为数据集的所有列,可指定特定数据列: keep: 标记哪个重复数据,默认为'first'.1.'first':标记重复 ...
- 软件工程homework-001
一. 回顾你过去将近3年的学习经历 1.当初你报考的时候,是真正喜欢计算机这个专业吗? 答:不喜欢,高中时候就已经对计算机比较抵触了,家里小时候比较富裕,九几年就在日本买了第一批家用台式机 ...
- npm vue路由配置
npm vue路由 复习:1.README.md文件:保存如何使用的命令 (1) npm install:拷项目时可以不拷node_modules文件,运行该命令时,会自动下载node_mod ...
- Wireshark OpenFlow解析器拒绝服务漏洞
受影响系统:Wireshark Wireshark 2.2.0 - 2.2.1Wireshark Wireshark 2.0.0 - 2.0.7描述:CVE(CAN) ID: CVE-2016-937 ...
- IO笔记(学习尚硅谷java基础教程)
一.基础知识 1. 在普通方法和测试方法中文件路径的差异 在普通方法中:文件路径相当于在当前项目中,而不是当前Module(以项目为基准) 在测试方法中:文件路径相当于在当前Module中,而不是当前 ...
- Spring Cache缓存框架
一.序言 Spring Cache是Spring体系下标准化缓存框架.Spring Cache有如下优势: 缓存品种多 支持缓存品种多,常见缓存Redis.EhCache.Caffeine均支持.它们 ...
- Android studio Error occurred during initialization of VM
Unable to start the daemon process. This problem might be caused by incorrect configuration of the d ...
- 记-beego项目调用Jenkins API获取job信息
type JenkinsController struct { beego.Controller } type Job struct { Name string `json:"name&qu ...
- Docker提交镜像-数据卷-可视化
在熟悉完Docker的安装及基本命令使用之后,我们开始学习下Docker的进阶操作:包括但不限于新建Docker镜像,数据卷的挂载,以及Docker的可视化等. Docker提交镜像 启动镜像 我们先 ...
- 简单面试前算法一览java
1.排序 冒泡,快速排序 2.查找 二分查找 3.链表 翻转链表 合并链表 是否有环 b. 快慢指针 public class QuickSort { public static void qui ...