css 不定宽高的元素居中布局解决方案

2020-04-27 07:33:25易采站长站整理

公共代码同[垂直居中]

常用方案一: absolute + transform


.parent {
position: relative;

.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
}

常用方案二: flex


.parent {
display: flex;
align-items: center;
justify-content: center;
}