1. 定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.wrap {
position: relative;
width: 300px;
height: 300px;
background-color: pink;
}
.box1 {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 100px;
height: 100px;
background-color: yellow;
}
</style>
</head>
<body>
<div class='wrap'>
<div class="box1"></div>
</div>
</body>
</html>
2. 弹性盒模型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.wrap {
display: flex;
align-items: center;
justify-content: center;
width: 300px;
height: 300px;
margin: auto;
background-color: palevioletred;
}
.box {
width: 100px;
height: 100px;
background-color: peru;
}
</style>
</head>
<body>
<div class='wrap'>
<div class="box"></div>
</div>
</body>
</html>