CSS实现垂直居中

1、最简单的单行文本line-height垂直居中

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{margin:0; padding:0;}
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; text-align:center;}
p{line-height:300px;}
</style>
</head>
<body>
<div class="box">
	<p>XXXXXXXXXXXX</p>
</div>
</body>
</html>
2、图片垂直居中,使用line-height和vertical-align:middle

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{margin:0; padding:0;}
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; text-align:center; line-height:300px;}
img{vertical-align:middle;}
</style>
</head>
<body>
<div class="box">
	<img src="images/img_27.gif" />
</div>
</body>
</html>
3、使用table布局,display:table和display:table-cell

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{margin:0; padding:0;}
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; text-align:center; display:table;}
p{display:table-cell; vertical-align:middle;}
</style>
</head>
<body>
<div class="box">
	<p>XXXXXXXXXXXXXXXXXXXX</p>
</div>
</body>
</html>
4、绝对居中,使用margin:auto和top、bottom、left、right同设为0

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{margin:0; padding:0;}
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; position:relative;}
.box-1{width:50%; height:50%; border:1px solid blue; margin:auto; position:absolute; top:0; bottom:0; left:0; right:0; overflow:auto;}
</style>
</head>
<body>
<div class="box">
	<div class="box-1"></div>
</div>
</body>
</html>
5、使用flex布局,display:flex和justify-content:center水平居中,align-items:center垂直居中

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
*{margin:0; padding:0;}
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; display:flex; justify-content:center; align-items:center;}
.box-1{width:200px; height:100px; border:1px solid blue;}
</style>
</head>
<body>
<div class="box">
	<div class="box-1"></div>
</div>
</body>
</html>

关于flex弹性布局参考链接:

http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

6、使用margin负值(缺点是必须知道宽高)

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">  
<title></title>  
<style>  
*{margin:0; padding:0;}  
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; position:relative;}  
.box-1{width:200px; height:100px; border:1px solid blue; position:absolute; top:50%; left:50%; margin-left:-100px; margin-top:-50px;}  
</style>  
</head>  
<body>  
<div class="box">  
    <div class="box-1"></div>  
</div>  
</body>  
</html>

7、使用transform:translate(-50%,-50%),移动端用的较多,可以不用知道宽高

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">  
<title></title>  
<style>  
*{margin:0; padding:0;}  
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; position:relative;}  
.box-1{width:200px; height:100px; border:1px solid blue; position:absolute; top:50%; left:50%; transform:translate(-50%,-50%); -webkit-transform:translate(-50%,-50%);}  
</style>  
</head>  
<body>  
<div class="box">  
    <div class="box-1"></div>  
</div>  
</body>  
</html>

8、使用display:-webkit-box,设置-webkit-box-pack:center和-webit-box-align:center;

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="utf-8">  
<title></title>  
<style>  
*{margin:0; padding:0;}  
.box{width:500px; height:300px; border:1px solid red; margin-top:100px; margin-left:200px; display:-webkit-box; -webkit-box-pack:center; -webkit-box-align:center;}  
.box-1{width:200px; height:100px; border:1px solid blue;}
</style>  
</head>  
<body>  
<div class="box">  
    <div class="box-1"></div>  
</div>  
</body>  
</html>
关于CSS垂直居中参考链接:

https://www.zhihu.com/question/20543196

http://blog.youkuaiyun.com/freshlover/article/details/11579669

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值