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

8 篇文章 0 订阅

实现方式:

  1. float
  2. 绝对定位
  3. flexbox布局
  4. table
  5. grid网格布局
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>三栏自适应布局</title>
	<style type="text/css">
		*{
			margin: 0;
			padding: 0;
		}
		.layout .left-center-right>div {
			height: 100px;
		}
		.layout {
			margin-top: 20px;
		}
	</style>
</head>
<body>
	<!-- 浮动布局 -->
	<section class="layout float">
		<style type="text/css">
			.float .left {
				float: left;
				width: 300px;
				background: red;
			}
			.float .right {
				float: right;
				width: 300px;
				background: blue;
			}
			.float .center {
				background: yellow;
			}
		</style>
		<article class="left-center-right">
			<div class="left">left</div>
			<div class="right">right</div>
			<div class="center">
				<h3>浮动布局</h3>
				center
			</div>
		</article>
	</section>

	<!-- 绝对定位布局 -->
	<section class="layout absolute">
		<style type="text/css">
			.absolute .left-center-right>div {
				position: absolute;
			}
			.absolute .left {
				left: 0;
				width: 300px;
				background: red;
			}
			.absolute .center {
				left: 300px;
				right: 300px;
				background: yellow;
			}
			.absolute .right {
				right: 0;
				width: 300px;
				background: blue;
			}
		</style>
		<arcticle class="left-center-right">
			<div class="left">left</div>
			<div class="center">
				<h3>绝对定位布局</h3>
				center
			</div>
			<div class="right">right</div>
		</arcticle>
	</section>

	<!-- flexbox布局 -->
	<section class="layout flexbox">
		<style type="text/css">
			.flexbox {
				margin-top: 140px;
			}
			.flexbox .left-center-right {
				display: flex;
			}
			.flexbox .left {
				width: 300px;
				background: red;
			}
			.flexbox .center {
				flex: 1;
				background: yellow;
			}
			.flexbox .right {
				width: 300px;
				background: blue;
			}
		</style>
		<arcticle class="left-center-right">
			<div class="left">left</div>
			<div class="center">
				<h3>flexbox布局</h3>
				center
			</div>
			<div class="right">right</div>
		</arcticle>
	</section>

	<!-- table布局 -->
	<section class="layout table">
		<style type="text/css">
			.table .left-center-right {
				display: table;
				width: 100%;
			}
			.table .left-center-right>div {
				display: table-cell;
			}
			.table .left {
				width: 300px;
				background: red;
			}
			.table .center {
				background: yellow;
			}
			.table .right {
				width: 300px;
				background: blue;
			}
		</style>
		<article class="left-center-right">
			<div class="left">left</div>
			<div class="center">
				<h3>表格布局</h3>
				center
			</div>
			<div class="right">right</div>
		</article>
	</section>

	<!-- grid布局 -->
	<section class="layout grid">
		<style type="text/css">
			.grid .left-center-right {
				display: grid;
				grid-template-rows: 100px;
				grid-template-columns: 300px auto 300px;
			}
			.grid .left {
				background: red;
			}
			.grid .center {
				background: yellow;
			}
			.grid .right {
				background: blue;
			}
		</style>
		<article class="left-center-right">
			<div class="left">left</div>
			<div class="center">
				<h3>grid布局</h3>
				center
			</div>
			<div class="right">right</div>
		</article>
	</section>
</body>
</html>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值