<!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>2D转换居中效果</title>
<style>
div{
position:relative;
width:500px;
height:500px;
background-color:rgb(3, 255, 36);
}
p{
position:absolute;
left:50%;
top:50%;
width:200px;
height:200px;
background-color:rgb(253, 7, 7);
transform: translate(-50%,-50%);
/* margin-left:-100px;
margin-top:-100px; */
}
</style>
</head>
<body>
</body>
<div>
<p></p>
</div>
</html>
运行结果: