Skip to content

水平垂直居中

inline 行内元素

提示

行内元素设置 widthheight 是不生效的。

  1. 通过 text-align: center 来实现水平居中。
  2. 通过 line-heightheight 来实现垂直居中,不过需要设置 line-height 的高度与 height 相等。

flex

justify-contentalign-items

flex 容器设置 justify-contentalign-items

margin: auto

子元素设置 margin: auto

grid

place-items

margin: auto

子元素设置 margin: auto

position

偏移量 + transform

通过 position 的 偏移量(top、left)transform(translate) 来实现。

偏移量 + margin

将 position 的各方向的偏移量设置相同,然后使用 margin: auto

table

display: table-cell 下,通过 text-alignvertical-align 来实现水平垂直居中。

基于 MIT 许可发布