首先写个基本样式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
<style>
html,body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.content {
width: 300px;
height: 300px;
background: orange;
}
</style>
</head>
<body>
<div class="content"></div>
</body>
</html>
1.display:flex实现
body{
display: flex;
align-items: center;
}
2.display:absolute(已知元素高度)
.content {
width: 300px;
height: 300px;
background: orange;
display:absolute;
top:50%;
margin-top:-150px;//向上移元素一半的距离
}
3.display:absolute(未知元素高度)
.content {
width: 300px;
height: 300px;
background: orange;
display:absolute;
top:50%;
left:50%
transform:translate(-50%,-50%);//向左向上移元素一半的距离
}