当前位置:Gxlcms > 前端框架 > css中有哪些方法可以实现垂直居中

css中有哪些方法可以实现垂直居中

时间:2021-07-01 10:21:17 帮助过:16人阅读

css实现垂直居中的方法如下:

1、利用line-height实现居中,这种方法适合纯文字类的;

  1. <!-- css -->
  2. <style>
  3. .parents {
  4. height: 400px;
  5. line-height: 400px;
  6. width: 400px;
  7. border: 1px solid red;
  8. text-align: center;
  9. }
  10. .child {
  11. background-color: blue;
  12. color: #fff;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <!-- html -->
  18. <div class="parents">
  19. <span class="child">css布局,实现垂直居中</span>
  20. </div>
  21. </body>

效果:

cf2107043f8587b8a39d71d4f3c1811.png

(推荐教程:CSS教程)

2、通过设置父容器相对定位,子级设置绝对定位,标签通过margin实现自适应居中;

  1. <!-- css -->
  2. <style>
  3. .parents {
  4. height: 400px;
  5. width: 400px;
  6. border: 1px solid red;
  7. position: relative;
  8. }
  9. .child {
  10. width: 200px;
  11. height: 100px;
  12. line-height: 100px;
  13. text-align: center;
  14. color: #fff;
  15. background-color: blue;
  16. /* 四个方向设置为0, 然后通过margin为auto自适应居中 */
  17. position: absolute;
  18. top: 0;
  19. right: 0;
  20. bottom: 0;
  21. left: 0;
  22. margin: auto;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <!-- html -->
  28. <div class="parents">
  29. <span class="child">css布局,实现垂直居中</span>
  30. </div>
  31. </body>

效果:

35bcabfea590fb5cb7841e201d54757.png

3、弹性布局flex 父级设置display: flex; 子级设置margin为auto实现自适应居中;

  1. <!-- css -->
  2. <style>
  3. .parents {
  4. height: 400px;
  5. width: 400px;
  6. border: 1px solid red;
  7. display: flex;
  8. }
  9. .child {
  10. width: 200px;
  11. height: 100px;
  12. line-height: 100px;
  13. text-align: center;
  14. color: #333;
  15. background-color: yellow;
  16. margin: auto;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <!-- html -->
  22. <div class="parents">
  23. <span class="child">css布局,实现垂直居中</span>
  24. </div>
  25. </body>

效果:

9a1371f372f924ea7d2c3bd0715c4fe.png

4、父级设置相对定位,子级设置绝对定位,并且通过位移transform实现;

  1. <!-- css -->
  2. <style>
  3. .parents {
  4. height: 400px;
  5. width: 400px;
  6. border: 1px solid red;
  7. position: relative;
  8. }
  9. .child {
  10. width: 200px;
  11. height: 100px;
  12. line-height: 100px;
  13. text-align: center;
  14. color: #fff;
  15. background-color: green;
  16. position: absolute;
  17. top: 50%;
  18. left: 50%;
  19. transform: translate(-50%, -50%);
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <!-- html -->
  25. <div class="parents">
  26. <span class="child">css布局,实现垂直居中</span>
  27. </div>
  28. </body>

效果:

368fe3867a606d438284336fcd7ae2d.png

5、父级设置弹性盒子,并设置弹性盒子相关属性;

  1. <!-- css -->
  2. <style>
  3. .parents {
  4. height: 400px;
  5. width: 400px;
  6. border: 1px solid red;
  7. display: flex;
  8. justify-content: center; /* 水平 */
  9. align-items: center; /* 垂直 */
  10. }
  11. .child {
  12. width: 200px;
  13. height: 100px;
  14. color: black;
  15. background-color: orange;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <!-- html -->
  21. <div class="parents">
  22. <span class="child"></span>
  23. </div>
  24. </body>

效果:

6c1569ec27b4fceb8990290d14a1174.png

6、网格布局,父级通过转换成表格形式,然后子级设置行内或行内块实现。(需要注意的是:vertical-align: middle使用的前提条件是内联元素以及display值为table-cell的元素)。

效果:

9a5d39375c8d1d473b006c7509a8c75.png

  1. <!-- css -->
  2. <style>
  3. .parents {
  4. height: 400px;
  5. width: 400px;
  6. border: 1px solid red;
  7. display: table-cell;
  8. text-align: center;
  9. vertical-align: middle;
  10. }
  11. .child {
  12. width: 200px;
  13. height: 100px;
  14. color: #fff;
  15. background-color: blue;
  16. display: inline-block; /* 子元素设置行内或行内块 */
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <!-- html -->
  22. <div class="parents">
  23. <span class="child"></span>
  24. </div>
  25. </body>

相关视频教程推荐:css视频教程

以上就是css中有哪些方法可以实现垂直居中的详细内容,更多请关注Gxlcms其它相关文章!

人气教程排行