CSS面试题:垂直居中的一些方式?

46 阅读1分钟

如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中; 如果 .parent 的 height 写死了,就很难把 .child 居中,以下是垂直居中的方法。 忠告:能不写 height 就千万别写 height。

table自带功能

div 装成 table

margin-top -50%

translate -50%

子绝父相 absolute margin auto

flex

display:flex;
align-items:center;
justify-content:center;
flex-direction:column ;