实现左边定宽,右边自适应布局的几种方法

HTML结构

<div class="twocol">
    <div class="left">left</div>
	<div class="right">right</div>
</div>

CSS样式

* {
	margin: 0;
	padding: 0;
}
.twocol {
	background: #ccc;
	font-size: 20px;
	text-align: center;
	height: 400px;
	line-height: 400px;
}

1、float + margin-left

.left{
	background: pink;
	float: left;
	width: 200px;
	height: 400px;
}
.right {
	margin-left: 200px;
	background: skyblue;
	height: 400px;
}

2、flex

.twocol {
	background: #ccc;
	font-size: 20px;
	text-align: center;
	height: 400px;
	line-height: 400px;
	display: flex;
}
.left{
	background: pink;
	width: 200px;
	height: 400px;
}
.right {
	background: skyblue;
	height: 400px;
	flex: 1;
}

3、table

.twocol {
	background: #ccc;
	font-size: 20px;
	text-align: center;
	width: 100%;
	height: 400px;
	line-height: 400px;
	display: table;
}
.left{
	background: pink;
	width: 200px;
	height: 400px;
	display: table-cell;
}
.right {
	background: skyblue;
	height: 400px;
	display: table-cell;
}

需要注意:使用table的时,父元素需要设置宽度。不然宽度为表格除了其他元素外,剩余空间。

4、float+overflow

.left{
	background: pink;
	float: left;
	width: 200px;
	height: 400px;
}
.right {
	background: skyblue;
	height: 400px;
	overflow: hidden;
}

5、absolute + margin-left

.twocol {
	background: #ccc;
	font-size: 20px;
	text-align: center;
	height: 400px;
	line-height: 400px;
	position: relative;
}
.left{
	background: pink;
	width: 200px;
	height: 400px;
	position: absolute;
	left: 0;
	top: 0;
}
.right {
	margin-left: 200px;
	background: skyblue;
	height: 400px;
}

6、absolute

.twocol {
	background: #ccc;
	font-size: 20px;
	text-align: center;
	height: 400px;
	line-height: 400px;
	position: relative;
}
.left{
	background: pink;
	width: 200px;
	height: 400px;
	position: absolute;
	left: 0;
	top: 0;
}
.right {
	background: skyblue;
	height: 400px;
	position: absolute;
	left: 200px;
	top: 0;
	width: 100%;
}

7、grid

.twocol {
	background: #ccc;
	font-size: 20px;
	text-align: center;
	line-height: 400px;
	display: grid;
	grid-template-rows: 400px;
	grid-template-columns: 200px auto;
}
.left{
	background: pink;
}
.right {
	background: skyblue;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值