主要是父元素使用 position: relative; 子元素使用 position: absolute;
.div1 {
background: grey;
margin-top: 50px;
height: 300px;
width: 100%;
background-size: cover;
position: relative;
}
.div2 {
width: 300px;
height: 230px;
background-color: rgb(189, 172, 172);
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
效果