1:父类相对定位,子级绝对定位
<style>
.a{
width: 100px;
height: 100px;
position: relative;
border: 5px solid red;
}
.b{
position: absolute;
width: 50px;
height: 50px;
margin: auto;
top: 0;
bottom: 0;
left: 0;
right: 0;
background-color: red;
}
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
</div>
2:父类display:flex
<style>
.a{
width: 100px;
height: 100px;
display: flex;
justify-content: center;
align-items: center;
border: 5px solid red;
}
.b{
width: 50px;
height: 50px;
background-color: red;
}
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
</div>
3:父类使用display:table-cell
<style>
.a{
width: 100px;
height: 100px;
display: table-cell;
vertical-align: middle;
border: 5px solid red;
}
.b{
width: 50px;
height: 50px;
margin: auto;
background-color: red;
}
</style>
</head>
<body>
<div class="a">
<div class="b"></div>
</div>
</body>