微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

css实现左右宽度固定,中间自适应及上下宽度固定,中间自适应的方法

文章目录

一、左右宽度固定,中间宽度自适应
1. 使用浮动布局

(1)左侧元素与右侧元素优先渲染,分别向左和向右浮动
(2)中间元素在文档流的最后渲染,则会自动插入到左右两列元素的中间

在这里插入图片描述

2. 使用弹性布局

父元素开启flex,中间设置flex:1

在这里插入图片描述

3. 使用绝对定位

在这里插入图片描述

二、实现上下宽度固定,中间自适应
1. 绝对定位
	<head>
		<Meta charset="utf-8">
		<style type="text/css">
			body {
				height: 100%;
				width: 100%;
			}
			.Box>div {
				position: absolute;
				width: 100%;
			}
			.top {
				top: 0;
				height: 200px;
				background-color: antiquewhite;
			}
			.bottom {
				bottom: 0;
				height: 200px;
				background-color: aqua;
			}
			.center {
				top: 200px;
				bottom: 200px;
				background-color: gold;
			}
		</style>
	</head>
	<body>
		<div class="Box">
			<div class="top"></div>
			<div class="center"></div>
			<div class="bottom"></div>
		</div>
	</body>			
2. flex弹性布局
<!DOCTYPE html>
<html>
	<head>
		<Meta charset="utf-8">
		<style type="text/css">
			body {
				height: 100%;
				width: 100%;
			}
			.Box {
				display: flex;
				width: 100%;
			}
			.top {
				height: 200px;
				background-color: antiquewhite;
			}
			.bottom {
				height: 200px;
				background-color: aqua;
			}
			.center {
				flex: 1;
				background-color: gold;
			}
		</style>
	</head>
	<body>
		<div class="Box">
			<div class="top"></div>
			<div class="center"></div>
			<div class="bottom"></div>
		</div>
	</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐