html
<div class="parent">
<div class="child">demo</div>
</div>
- inline-block + text-align + table-cell + vertical-align
.parent{
text-align: center;
display: table-cell;
vertical-align: middle;
}
.child{
display: inline-block;
}
parent变成单元格,为了让text-align和vertical-align起作用,让child变成inline-block
2. absolute + transform
.parent{
position: relative;
}
.child{
position: absolute;
left: 50%;
right: 50%;
transform: translate(-50%, -50%);
}
- flex + justify-content + align-items
.parent{
display: flex;
justify-content: center;
align-items: center;
}