Skip to content

居中对齐的实现方式

水平居中

  • inline 元素:text-align:center
  • block 元素:margin:auto
  • absolute 元素:left:50% + margin-left 宽度负值

垂直居中

  • inline 元素:line-height 的值等于 height 值
  • absolute 元素:top:50% + margin-top 高度负值
  • absolute 元素:top:50% + left:50% + transform(-50%,-50%)
  • absolute 元素:top,left,bottom,right=0 + margin:auto

基于 MIT 许可发布