第一种方法
<!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>
div{
width:500px;
height:500px;
position: relative;
background-color: pink;
}
p{
width: 100px;
height:100px;
position: absolute;
top:50%;
left:50%;
margin-left:-50px;
margin-top:-50px;
background-color: blue;
}
</style>
</head>
<body>
<div>
<p></p>
</div>
</body>
</html>
第二种方法
<!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>
div{
width:500px;
height:500px;
position: relative;
background-color: pink;
}
p{
width: 100px;
height:100px;
position: absolute;
top:50%;
left:50%;
transform: translate(-50%,-50%);
background-color: blue;
}
</style>
</head>
<body>
<div>
<p></p>
</div>
</body>
</html>