不确定div的宽高,将子div垂直水平居中在父div中
<style type="text/css">
.parent {
display: flex;
width: 100%;
height: 300px;
background: #cecece;
align-items: center;
/* 垂直居中 */
justify-content: center;
/* 水平居中 */
}
.child {
background: #000;
width: 200px;
height: 100px;
}
</style>
<div class="parent">
<div class="child"></div>
</div>
.parent {
width: 100%;
height: 300px;
background: #eee;
position: relative;
}
.child {
position: absolute;
margin: auto;
top: 0;
left: 0;
right:0;
bottom: 0;
background: #000;
width: 200px;
height: 100px;
}