css中几种最优布局

一、水平居中(使用text-align+inline-block)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.parent{
	text-align:center;
	background:red;
	width:200px;
	height:200px;
	}
.child{
	display:inline-block;
	background:yellow;
	width:100px;
	height:100px;
	}
</style>
</head>
<body>
<div class="parent">
<div class="child">
</div>
</div>
</body>
</html>

运行结果:

水平居中

二、垂直居中(使用table-cell+vertical-align)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.parent{
    display:table-cell;
	vertical-align:middle;
	background:red;
	width:200px;
	height:200px;
	}
.child{
	display:inline-block;
	background:yellow;
	width:100px;
	height:100px;
	}
</style>
</head>
<body>
<div class="parent">
<div class="child">
</div>
</div>
</body>
</html>

运行效果:

垂直居中

三、水平垂直居中(使用text-align+inline-block+table-cell+vetical-align)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.parent{
	text-align:center;
    display:table-cell;
	vertical-align:middle;
	background:red;
	width:200px;
	height:200px;
	}
.child{
	display:inline-block;
	background:yellow;
	width:100px;
	height:100px;
	}
</style>
</head>
<body>
<div class="parent">
<div class="child">
</div>
</div>
</body>
</html>

运行效果:

水平垂直居中

四、多列布局

(1)定宽+自适应(使用float+margin)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>

.left{
	background-color:red;
	width:100px;
	float:left;
	position:relative;
	}
.rightfix{
	background-color:blue;
	float:right;
	margin-left:-100px;
	width:100%;
	}
.right{
	margin-left:120px;
	}
</style>
</head>
<body>

<div class="left">left</div>
<div class="rightfix">
<div class="right">
<p>right</p>
<p>right</p>
</div>
</div>


</body>
</html>

运行结果:

定宽加自适应

(2)两列定宽+一列自适应(使用float+overflow)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.left{
	background-color:red;
	}
.center{
	background-color:yellow;
	}
.left,.center{
	width:100px;
	float:left;
	margin-right:20px;
	}

.right{
	overflow:hidden;
	background-color:blue;
	}
</style>
</head>
<body>

<div class="left">left</div>
<div class="center">center</div>
<div class="right"><p>right</p><p>right</p></div>




</body>
</html>

运行结果:

两列定宽加一列自适应

(3) 不定宽+自适应(使用float+overflow)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>

.left{
	background-color:red;
	float:left;
	margin-right:20px;
	}
.right{
	background-color:blue;
	overflow:hidden;
	}
.left p{
	width:200px;
	}
</style>
</head>
<body>

<div class="left"><p>left</p></div>
<div class="right">
<p>right</p>
<p>right</p>
</div>



</body>
</html>

运行结果:

不定宽加自适应

 

 

 

(4)两列不定宽+一列自适应(使用float+overflow)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>

.left,.center{
	float:left;
	margin-right:20px;
	}
.left{	
	background-color:red;
}
.center{
	background-color:yellow;
	}
.right{
	background-color:blue;
	overflow:hidden;
	}
.left p,.center p{
	width:200px;
	}
</style>
</head>
<body>

<div class="left"><p>left</p></div>
<div class="center"><p>center</p></div>
<div class="right">
<p>right</p>
<p>right</p>
</div>



</body>
</html>

运行结果:

两列不定宽加一列自适应

(5)等分布局(使用float)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.parentwrap{
	width:960px;
	overflow:hidden;

	
}
.parent{
	margin-left:-20px;
	overflow:hidden;
	background-color:lightgray;
}
.child{
	background-color:red;
	width:25%;
	height:100px;
	float:left;
	padding-left:20px;
	box-sizing:border-box;
	background-clip:content-box;
	}
</style>
</head>
<body>
<div class="parentwrap">
<div class="parent">
<div class="child">part1</div>
<div class="child">part2</div>
<div class="child">part3</div>
<div class="child">part4</div>
</div>
</div>
</body>
</html>

运行结果:

等列分布

(6)定宽+自适应+两块高度一样高

 代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.parent{
	display:table;
	width:100%;
	layout:fixed;
	}
.left{
	background-color:red;
	width:100px;
	padding-right:20px;}
.right{
	background-color:blue;
	}
.left,.right{
	display:table-cell;
	}
</style>
</head>
<body>
<div class="parent">
<div class="left"><p>left</p></div>
<div class="right"><p>right</p><p>right</p></div>
</div>
</body>
</html>

运行结果:

定宽加自适应加两块高度一样

 

 

 

5.全局布局(使用position)

代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
.top{
	height:100px;
	position:absolute;
	top:0px;
	left:0px;
	right:0px;
	background-color:red;
	}
.left{
	position:absolute;
	top:100px;
	left:0px;
	bottom:50px;
	width:200px;
	background-color:blue;
	}
.right{
	position:absolute;
	top:100px;
	left:200px;
	right:0px;
	bottom:50px;
	background-color:yellow;
	}
.bottom{
	position:absolute;
	left:0px;
	right:0px;
	bottom:0px;
	height:50px;
	background-color:pink;
	}
.inner{
	min-height:100px;
	}
</style>
</head>
<body>
<div class="top"><p>top</p></div>
<div class="left"><p>left</p></div>
<div class="right"><div class="inner">right</div></div>
<div class="bottom"><p>bottom</p></div>
</body>
</html>

运行结果:

全局布局

 

 

 

 

 

 

 

 

 

 

 

 

 

 

全屏布局的特点:

  • 滚动条不是全局滚动条,而是出现在内容区域里,往往是主内容区域
  • 浏览器变大时,撑满窗口

全屏布局的方法:

全局布局方法

 

 

 

 

 

 

 

 

 

 

 

 

全局布局兼容性,自适应,性能一览表:

方案兼容性性能是否自适应
position部分自适应
flex较差可自适应
Grid较好可自适应

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值