使用absolute + margin将子元素设置为positionabsolute;再将其上下左右的margin设置为auto即可实现垂直居中。 什么意思
这段话的意思是,可以通过将子元素的定位方式设置为absolute,然后将其上下左右的margin都设置为auto,来实现垂直居中。具体实现方式可以参考以下代码:
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
其中,父元素需要设置为position:relative,子元素设置为position:absolute,并且将上下左右的值都设置为0,然后通过设置margin为auto来实现垂直居中。
原文地址: https://www.cveoy.top/t/topic/d0Xk 著作权归作者所有。请勿转载和采集!