 水平垂直居中
          水平垂直居中
        
  # 1. flex布局
elem {
  display: flex;
  align-items: center;
  justify-content: center;
}
1
2
3
4
5
2
3
4
5
移动端场景兼容
配合 line-height: normal; ;另外需要注意 font-size 最好不要使用奇数,即可完美解决移动端安卓按钮文字偏上问题。
# 2. position: absolute + translate
 parent {
  position: relative;
}
elem {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
上次更新: 2021-07-09 14:13:21