05 CSS实现三栏自适应布局(两边宽度固定,中间自适应)

1.绝对定位法

2.使用浮动

3.圣杯布局

4.flex布局

1.绝对定位法

<style type="text/css">
		html,body{ margin: 0px;width: 100%; }   
		#left,#right {
			width: 200px;
			height: 200px; 
			background-color: #ffe6b8;
			position: absolute;
			}  
		#left {
			left:0px;
			}  
		#right {
			right: 0px;
			}  
		#center {
			margin:0 210px ;
			background-color: #eee;
			height: 200px; 
			}  
	</style>
	<body>
		<div id = "left">我是左边</div>
		<div id = "right">我是右边</div>
		<div id = "center">我是中间</div>
	</body>
  1. 原理:通过给左、右盒子绝对定位,然后让居中的盒子用margin值撑开。
  2. 优点:三个盒子的顺序可以任意
  3. 缺点:当页面宽度小于等于(左+右盒子宽度),绝对定位的层级更高会把中间盒子遮住。

2.使用浮动

<style type="text/css">
		html,body{ margin: 0px;width: 100%; }   
		#left,#right {
			width: 200px;
			height: 200px; 
			background-color: #ffe6b8;
			}  
		#left {
			float: left;
			}  
		#right {
			float: right;
			}  
		#center {
			margin:0 210px ;
			background-color: #eee;
			height: 200px; 
			}  
	</style>
	<body>
		<div id = "left">我是左边</div>
		<div id = "right">我是右边</div>
		<div id = "center">我是中间</div>
	</body>

3.圣杯布局

<style type="text/css">
		.content{padding: 0 100px;}
		.col{
		    float: left;
		    height: 200px;
		    position: relative;
		}
		.left,.right{
		    width: 100px;
		}
		.left{
		    background-color: blue;
		    margin-left: -100%;
		    right: 100px;
		}
		.right{
		    background-color: green;
		    margin-left: -100px;
		    left:100px;
		}
		.center{
		    width: 100%;
		    background-color: pink;
		}  
	</style>
	<body>
		<div class="content">
		    <div class="center col">
		    </div>
		    <div class="left col">
		    </div>
		    <div class="right col">
		    </div>
		</div>
	</body>
  1. content需要给左右内边距等于左右盒子的宽度
  2. 左中右三个盒子都要设置浮动和相对定位
  3. 左盒子:margin-left:-100%;right:自身宽度
  4. 右盒子:margin-left: -自身宽度;left:自身宽度;

4.flex布局

<style type="text/css">
		.content {
			width: 100%;
			display: flex;
		} 
		.left, .right {
			width: 200px;
			height: 100px;
			background: red;
		}
		.center {
			height: 100px;
			flex: 1;
			background: pink;
		}
	</style>
	<body>
		<div class="content">
			<div class="left"></div>
		    <div class="center"></div>
		    <div class="right"></div>
		</div>
	</body>
  1. 父盒子display:flex
  2. 左右盒子给宽高
  3. 中盒子flex: 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值