<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.father {
width: 500px;
height: 400px;
border: 1px solid #000;
margin: 0 auto;
position: relative;
}
.son {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
/* margin-left: -100px;
margin-top: -50px; */
width: 200px;
height: 100px;
background: #000;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
用位移实现盒子垂直水平居中
于 2022-03-07 21:58:10 首次发布