<!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>
/* 第一种 */
* {
margin: 0 auto;
}
.father {
position: relative;
width: 600px;
height: 600px;
background-color: skyblue;
}
.son {
position: absolute;
top: 50%;
left: 50%;
margin-left: -150px;
margin-top: -150px;
width: 300px;
height: 300px;
background-color: pink;
}
/* 第二种 transform */
.father {
position: relative;
width: 600px;
height: 600px;
background-color: skyblue;
}
.son {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) ;
width: 300px;
height: 300px;
background-color: pink;
}
/* 第三种 四边为0 */
.father {
position: relative;
width: 600px;
height: 600px;
background-color: skyblue;
}
.son {
position: absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
width: 300px;
height: 300px;
background-color: pink;
}
/* 第四种 flex布局 */
.father {
display: flex;
width: 600px;
height: 600px;
border: 1px dashed block;
background-color: skyblue;
justify-content: center;
align-items: center;
}
.son {
width: 300px;
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>