css常见的布局方式

一,两列布局

HTML代码

<body>
    <div class="left">ccc</div>
	<div class="right">cccccc</div>
</body>
  1. 左边固定宽度右边自适应
  • position+margin
* {
	margin: 0px;
    padding: 0px;
}
.left{
	position: absolute;
	left: 0px;
	top: 0px;
	width: 100px;;
	background-color: red;
}
.right{
	margin-left: 110px;
	background-color: green;
}
  • float+overflow
* {
	margin: 0px;
	padding: 0px;
}
.left{
	float: left;
	width: 100px;
	background-color: red;
}
.right{
	overflow: hidden;
	background-color: green;
}
  • flex
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			.main {
				display: flex;
			}
			.left{
				width: 100px;
				background-color: red;
			}
			.right{
				flex: 1;
				background-color: green;
			}
		</style>
	</head>
	<body>
		<div class="main">
			<div class="left">ccc</div>
			<div class="right">cccccc</div>
		</div>
		
	</body>
</html>

右边宽度固定左边自适应

flex

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			.main {
				display: flex;
			}
			.left{
				
				flex: 1;
				background-color: green;
			}
			.right{
				width: 100px;
				background-color: red;
			}
		</style>
	</head>
	<body>
		<div class="main">
			<div class="left">ccc</div>
			<div class="right">cccccc</div>
		</div>
		
	</body>
</html>

float+margin

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			.main {
				width: 100%;
			}
			.left{
				margin-right:200px;
				background-color: red;
				height:100px;
			}
			.right{
				float: right;
				width: 200px;
				background-color: green;
				height:100px;
			}
		</style>
	</head>
	<body>
		<div class="main">
			<div class="right">cccccc</div>
			<div class="left">ccc</div>
		</div>
		
	</body>
</html>

 position+margin

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			.main {
				width: 100%;
			}
			.left{
				margin-right:200px;
				background-color: red;
				height:100px;
			}
			.right{
				position: absolute;
				right: 0;
				top: 0;
				width: 200px;
				background-color: green;
				height:100px;
			}
		</style>
	</head>
	<body>
		<div class="main">
			<div class="right">cccccc</div>
			<div class="left">ccc</div>
		</div>
		
	</body>
</html>


二,三列布局

三列布局通常是两侧定宽,中间自适应,布局方案类似于二列布局

position+margin

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			.main {
				width: 100%;
			}
			.left{
				position: absolute;
				left: 0;
				top: 0;
				width: 200px;
				background-color: green;
				height:100px;
			}
			.middle {
				height: 100px;
				margin: 0px 200px;
				background-color: black;
			}
			.right{
				position: absolute;
				right: 0;
				top: 0;
				width: 200px;
				background-color: green;
				height:100px;
			}
		</style>
	</head>
	<body>
		<div class="main">
			<div class="left">ccc</div>
			<div class="middle">fff</div>
			<div class="right">cccccc</div>
		</div>
		
	</body>
</html>

float+overflow+margin

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			* {
				margin: 0px;
				padding: 0px;
			}
			.main {
				width: 100%;
			}
			.left{
				float: left;
				width: 200px;
				background-color: green;
				height:100px;
			}
			.middle {
				height: 100px;
				margin: 0px 200px;
				background-color: black;
			}
			.right{
				float: right;
				overflow: auto;
				width: 200px;
				background-color: green;
				height:100px;
			}
		</style>
	</head>
	<body>
		<div class="main">
			<div class="left">ccc</div>
			<div class="right">cccccc</div>
			<div class="middle">fff</div>
		</div>
		
	</body>
</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值