5种实现3栏布局的方法(左右顶宽,高度已知,中间自适应)

5种实现3栏表布局的方法,html相同,仅改变css

<section class="layout">
	<article class="box">
		<div class="left"></div>
		<div class="center">
			<h1>各种方法</h1>
			中间
		</div>
		<div class="right"></div>
	</article>
</section>

浮动布局(float)

.layout article div{
	height: 100px;
}
.layout .left{
	float: left;
	width: 300px;
	background: red;
}
.layout .right{
	float: right;
	background: blue;
	width: 300px;
}
.layout .center{
	background: green;	
}

优点:兼容性好,时候多种浏览器

缺点:使结构脱离文档流。

定位布局(position)

.layout{
	position: relative;
}
.layout article div{
	height: 100px;
	position: absolute;
}
.layout .left{
	width: 300px;
	left: 0;
	background: red;
}
.layout .right{
	background: blue;
	width: 300px;
	right: 0;
}
.layout .center{
	left: 300px;
	right: 300px;
	background: green;
}

优点:快捷方便,配合js可以快速生成和修改 

缺点:结构内子元素脱离文档流

弹性布局(flex)

.box{
	display: flex;
}
.layout article div{
	height: 100px;
}
.layout .left{
	width: 300px;
	background: red;
}
.layout .right{
	background: blue;
	width: 300px;
}
.layout .center{
	background: green;
	flex: 1;
}

优点: 相对来说最优的一种方法,随着浏览器支持越来越多。

表格布局(table)

.box{
	width: 100%;
	height: 100px;
	display: table;
}
.layout article div{
	display: table-cell;
}
.layout .left{
	width: 300px;
	background: red;
}
.layout .right{
	background: blue;
	width: 300px;
}
.layout .center{
	background: green;
}

 优点:兼容性(ie8等)

缺点:整个table是一个整体,在高度不确定的时候不适合

网格布局

.box{
	width: 100%;
	height: 100px;
	display: grid;
	grid-template-rows: 100px;
	grid-template-columns: 300px auto 300px;
}
.layout article div{

}
.layout .left{
	width: 300px;
	background: red;
}
.layout .right{
	background: blue;
	width: 300px;
}
.layout .center{
	background: green;
}

优点:简化代码量,实现更多效果

缺点:技术新,浏览器支持率低

 当高度不确定时,只有flex布局和table布局还可以使用,其他布局会出现问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值