html div左中右上中下,CSS三栏自适应布局,左中右,上中下

本文详细介绍了如何使用浮动、绝对定位、flexbox、表格和网格布局来实现HTML页面的三栏布局,其中左右两栏固定宽度,中间栏自适应,以及上下两栏固定高度,中间栏自适应的布局方法。通过实例代码展示了不同布局方案的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文对可以实现三栏布局的方法进行了整理

左右固定宽度,中间自适应(5种方法)

1.float方法

2.absolute方法

3.flexbox方法

4.table方法

5.grid方法

c45f58f06116

image.png

css布局全解

html *{

padding: 0;

margin: 0

}

.layout article div{

min-height: 50px;

}

.layout{

margin-top: 10px;

}

.layout.float .right{

float: right;

width: 300px;

background: red;

}

.layout.float .left{

float: left;

width: 300px;

background: blue;

}

.layout.float .center{

background: yellow;

}

浮动解决方案

.layout.absolute .left-center-right div{

position: absolute;

}

.layout.absolute .left{

width: 300px;

left: 0;

background: blue;

}

.layout.absolute .center{

left: 300px;

right: 300px;

background: yellow;

}

.layout.absolute .right{

width: 300px;

right: 0px;

background: red;

}

绝对定位解决方案

.layout.flexbox{

margin-top: 70px;

}

.layout.flexbox .left-center-right{

display: flex;

}

.layout.flexbox .left{

width: 300px;

background: blue;

}

.layout.flexbox .center{

flex:1;

background: yellow;

}

.layout.flexbox .right{

width: 300px;

background: red;

}

flex定位解决方案

.layout.table .left-center-right{

display: table;

width: 100%;

height: 50px;

}

.layout.table .left-center-right div{

display: table-cell;

}

.layout.table .left{

width: 300px;

background: blue;

}

.layout.table .center{

background: yellow;

}

.layout.table .right{

width: 300px;

background: red;

}

表格定位解决方案

.layout.grid .left-center-right{

display: grid;

width: 100%;

grid-template-rows: 50px;

grid-template-columns: 300px auto 300px;

}

.layout.grid .left{

background: blue;

}

.layout.grid .center{

background: yellow;

}

.layout.grid .right{

background: red;

}

网格定位解决方案

上下固定高度,中间自适应(4种方法)

1.absolute方法

2.flexbox方法

3.table方法

4.grid方法

c45f58f06116

image.png

css上下高度不变,中间自适应布局

html *{

padding: 0;

margin: 0;

}

#wrapper div{

width: 150px;

}

#wrapper div.one,.two,.three,.four{

width: 150px;

height: 100%;

display: inline-block;

float: left;

}

.one,.two,.three{

background: green;

}

.layout.absolute div{

position: absolute;

float: left;

}

.layout.absolute .top{

top: 0;

height: 100px;

background: red;

}

.layout.absolute .bottom{

bottom: 0;

height: 100px;

background: blue;

float: left;

}

.layout.absolute .center{

top: 100px;

bottom: 100px;

background: yellow;

overflow: auto;

}

absolute中间自适应元素

.two{

margin-left:10px;

}

.layout.flexbox{

display: flex;

width: 100%;

height: 100%;

flex-direction:column;

}

.layout.flexbox .top{

height: 100px;

background: red;

}

.layout.flexbox .center{

flex:1;

background: yellow;

}

.layout.flexbox .bottom{

height: 100px;

background: blue;

}

flexbox中间自适应元素

.three{

margin-left: 10px;

}

.layout.table{

display: table;

height: 100%;

width: 100%;

}

.layout.table div{

display: table-row;

/*特别注意*/

display: inline-block;

}

.layout.table .top{

height: 100px;

background: red;

}

.layout.table .center{

height:calc(100% - 200px);

background: yellow;

}

.layout.table .bottom{

height: 100px;

background: blue;

}

table方法中间自适应元素

.four{

margin-left: 10px;

}

.layout.grid{

display: grid;

height: 100%;

grid-template-rows: 100px auto 100px;

grid-template-columns: 150px;

}

.layout.grid .top{

background: red;

}

.layout.grid .center{

background: yellow;

}

.layout.grid .bottom{

background: blue;

}

grid方法中间自适应元素

看似简单的题目,还是要多练习增加熟练度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值