這個:html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>垂直居中</title>
<style>
html,
body {
height: 100%;
width: 100%;
}
/*relative定位,top:50% 利用margin-top上移自身一半*/
.content {
width: 300px;
height: 300px;
background: blue;
margin: 0 auto;
position: relative;
top: 50%;
margin-top: -150px; /*div上移自身高度(300)的一半*/
}
/*利用CSS3的transform: translateY(-50%)上移自身高度一半
.content {
width: 300px;
height: 300px;
background: blue;
margin: 0 auto;
position: relative;
top: 50%;
transform: translateY(-50%);
}*/
/*flex佈局 對父元素操做
body {
display: flex;
align-items: center;
justify-content: center;
}
.content {
width: 300px;
height: 300px;
background: blue;
}*/
</style>
</head>
<body>
<div class="content"></div>
</body>
</html>