如何用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%,不设置浮动元素内容不 ...
随机推荐
- tensorflow源码解析之framework拾遗
把framework中剩余的内容,按照文件名进行了简单解析.时间原因写的很仓促,算是占个坑,后面有了新的理解再来补充. allocation_description.proto 一个对单次内存分配结果 ...
- Jira8.0.2安装及破解
最近开发部总监需要部署JIRA管理项目,就安装了一个JIRA8.5.7版安装并破解后,有天断电重启了,发现启动不了提示连接不上数据库.后来我又换了台机器重新安装后又进行了重启发现此破解版本存在问题.并 ...
- 华为交换机配置ACL详细步骤
ACL 介绍 #2000-2999普通ACL,根据源IP过滤 #3000-3999高级ACL,根据源目的端口和源目的地址等过滤 #4000-4999二层ACL,根据源目的MAC等过滤 配置举例: 拒绝 ...
- 明火烟雾目标检测项目部署(YoloV5+Flask)
明火烟雾目标检测项目部署 目录 明火烟雾目标检测项目部署 1. 拉取Docker PyToch镜像 2. 配置项目环境 2.1 更换软件源 2.2 下载vim 2.3 解决vim中文乱码问题 3. 运 ...
- vscode使用python虚拟环境
vscode使用python虚拟环境 创建好虚拟环境之后,在vscode中配置使用python的虚拟环境. 首先打开设置,然后搜索python venv, 在python: Venv Path中设置为 ...
- OpenCv基础_四
Harris角点检测 理解 内部点:蓝框所示,无论滑动窗口水平滑动还是竖直滑动,框内像素值都不会发生大的变化 边界点:黑框所示,滑动窗口沿着某一个方向滑动框内像素点不会发生大的改变,但是沿着另一个方向 ...
- Spring——自动装配的三种实现方式
依赖注入的本质是装配,装配是依赖注入的具体行为 spring会在上下文中自动寻找,并自动给bean装配属性 自动装配的三种方式 (1).在xml中显式的装配 (2).在java中显式的装配 (3).隐 ...
- centos7升级openssh8.7&openssl1.1.1l脚本
脚本下载地址或首页联系本人获取 https://files.cnblogs.com/files/blogs/705493/update_ssh.sh 需搭配openssh8.7和openssl1.1. ...
- 如何批量修改图片名称(win下)
深度学习目标检测任务中常常需要大量的图片,这些图片一般来自网络爬虫或是自行批量下载,但下载下的图片常常在保存时被命名为长段英文数字混写,因此规律化命名下载的图片数据名称就显得尤为重要了,下面我演示在本 ...
- 浅析Java反射--Java
前言 上篇文章我们提到了可以使用反射机制破解单例模式.这篇文章我们就来谈一谈什么是反射,反射有什么用,怎么用,怎么实现反射. 概述 Java的反射(reflection)机制:是指在程序的运行状态中, ...