常面:《css七种方式实现垂直居中》

74 阅读1分钟

七种方式实现垂直居中

如果 .parent 的 height 不写,你只需要 padding: 10px 0; 就能将 .child 垂直居中;

如果 .parent 的 height 写死了,就很难把 .child 居中,以下是垂直居中的方法。

忠告:能不写 height 就千万别写 height。

  1. table自带功能
  2. 100% 高度的 afrer before 加上 inline block\
  3. 这个方法还有一个[优化版本](http://js.jirengu.com/poveg/3/edit)
    
  4. div 装成 table
  5. margin-top -50%
  6. translate -50%
  7. absolute margin auto
  8. flex