注转载: https://www.cnblogs.com/hutuzhu/p/4450850.html

1. table-cell

# css垂直居中

结果:
# css垂直居中

2. display:flex

# css垂直居中

3. translate

# css垂直居中

4. 绝对定位和0

# css垂直居中

这个方法使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 top:0; bottom:0;。但是因为它有固定高度,其实并不能和上下都间距为 0,因此 margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中是很简单的。

# css垂直居中

6. display:flex和margin:auto

# css垂直居中

7. display:-webkit-box

# css垂直居中

8. display:flex

# css垂直居中

原转载有一个有点问题,还有一个没实现,又加了一个别人实现的。

关于display:flex的知识,看阮一峰的官网:
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

相关文章:

  • 2021-06-08
  • 2022-12-23
猜你喜欢
  • 2021-12-04
  • 2021-04-23
  • 2022-01-17
相关资源
相似解决方案