css垂直水平居中最简单的三种实现方式

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/*定位居中1 缺点需要知道盒子宽高*/
div{
width: 200px;
height: 200px;
border: 1px solid red;
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
}
/*定位居中2*/
div{
width: 50%;
height: 50%;
border: 1px solid red;
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
/*css3 居中3 优点不需要考虑盒子具体宽高*/
div{
width: 50%;
height: 50%;
border: 1px solid red;
position: absolute;
top: 50%;
left:50%;
transform: translate(-50%,-50%);
}

</style>
</head>
<body>
<div></div>
</body>
</html>

 


posted @ 2018-03-31 21:48  zou1234  阅读(254)  评论(0编辑  收藏  举报