<!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>
.transform-center {
/* 先给transform-center添加相对定位 */
position: relative;
width: 500px;
height: 500px;
border: 10px solid rebeccapurple;
}
.transform-center .box {
/* 绝对定位 */
position: absolute;
/* 先box2让定位到父盒子的上左各50% */
top: 50%;
left: 50%;
/* 再transform左上自身的50% */
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
background-color: aqua;
}
</style>
</head>
<body>
<div class="transform-center">
<div class="box"></div>
</div>
</body>
</html>