前端面试题之CSS实现一个三栏布局,左右两列列固定宽度,中间内容自适应


实现一个三栏布局,左右两列列固定宽度,中间内容自适应

1. 浮动float

让左盒子左浮动 右盒子右浮动

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>浮动float</title>
	</head>
	<style>
		*{
		    margin: 0;
		    padding:0;
		}
		 .left{
		    float: left;
		    width: 100px;
		    background: red;
		}
		 .right{
		    float: right;
		    width: 100px;
		    background: red;
		}
		 .center{
		    background: yellow;
		}
	</style>
	<body>
		<div class="left"></div>
		<div class="right"></div>
		<div class="center"></div>
	</body>
</html>

2. 绝对定位

左中右三个盒子都绝对定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>绝对定位</title>
	</head>
	<style>
		*{
		    margin: 0;
		    padding:0;
		}
		
		  .left,.center,.right{
		    position: absolute;
			text-align: center;
		}
		 .left{
		    left: 0rem;
		    width: 200px;
		    background: red;
		}
		 .right{
		    right: 0rem;
		    width: 200px;
		    background: red;
		}
		 .center{
		    left: 200px;
		    right: 200px;
		    background: yellow;
		}
	</style>
	<body>
		<div class="left"></div>
		<div class="center"></div>
		<div class="right"></div>
	</body>
</html>

3. 弹性盒

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>弹性盒</title>
	</head>
	<style>
		*{
		    margin: 0;
		    padding:0;
		}
		
		 body{
			 display: flex;
			 text-align: center;
		 }
		 .left{
		    width: 100px;
		    background: red;
		}
		 .center{
		    flex: 1;
		    /* 让中间部分适配整个宽度 */
		    background: yellow;
		}
		 .right{
		    width: 100px;
		    background: red;
		}
	</style>
	<body>
		<div class="left"></div>
		<div class="center"></div>
		<div class="right"></div>
	</body>
</html>

4. 表格布局

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>display: table</title>
	</head>
	<style>
		* {
			margin: 0;
			padding: 0;
		}
		.left-center-right {
			display: table;
			width: 100%;
			height: 100px;
		}
		.left-center-right>div {
			display: table-cell;
		}
		.left {
			width: 200px;
			background-color: red;

		}
		.center {
			background-color: yellow;
		}
		.right {
			width: 200px;
			background-color: red;
		}
	</style>
	<body>
		<div class="left-center-right">
			<div class="left"></div>
			<div class="center"></div>
			<div class="right"></div>
		</div>
	</body>
</html>

5. 网格布局

  <!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>display: grid</title>
	</head>
	<style>
		* {
			margin: 0;
			padding: 0;
		}
	</style>
	<body>
		<section class="layout grid">
			<style>
				 .left-center-right {
					display: grid;
					width: 100%;
					grid-template-rows: 100px;
					grid-template-columns: 300px auto 300px;
				}
				 .left {
					background: red;
				}
				 .center {
					background: yellow;
				}
				 .right {
					background: red;
				}
			</style>
			<div class="left-center-right">
				<div class="left"></div>
				<div class="center"></div>
				<div class="right"></div>
			</div>
		</section>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值