博主信息
博文 128
粉丝 9
评论 5
访问量 294837
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
【归纳】赶紧收藏css实现水平垂直居中的八种快速方法总结
 一纸荒凉* Armani
原创
1141人浏览过

1. 利用text-align实现行内元素居中

行内元素居中就很简单,只需要设置一下元素的text-align和行高

  1. .box {
  2. height: 300px;
  3. width: 300px;
  4. background-color: pink;
  5. margin: 100px auto;
  6. }
  7. .box p {
  8. text-align: center;
  9. line-height: 300px;
  10. }

2.利用定位再结合margin:auto实现

块元素的水平居中利用margin就可以实现,但垂直居中显然不太好实现。

  1. .box {
  2. height: 300px;
  3. width: 300px;
  4. background-color: pink;
  5. margin: 100px auto;
  6. position: relative;
  7. }
  8. .box div {
  9. width: 30px;
  10. height: 30px;
  11. background-color: red;
  12. position: absolute;
  13. top: 0;
  14. bottom: 0;
  15. left: 0;
  16. right: 0;
  17. margin: auto;
  18. }

3.已知宽高利用负margin和定位配合

  1. .box {
  2. height: 300px;
  3. width: 300px;
  4. background-color: pink;
  5. margin: 100px auto;
  6. position: relative;
  7. }
  8. .box div {
  9. width: 30px;
  10. height: 30px;
  11. background-color: red;
  12. position: absolute;
  13. left: 50%;
  14. top: 50%;
  15. margin-left: -15px;
  16. margin-top: -15px;
  17. }

4. 定位通过计算元素宽高实现居中

  1. <!DOCTYPE html>
  2. <head>
  3. <title>absolute+margin计算元素宽高判断居中</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <style>
  6. .content {
  7. width: 300px;
  8. height: 300px;
  9. border: 1px solid #109D71;
  10. position: relative;
  11. }
  12. .box {
  13. position: absolute;
  14. width: 100px;
  15. height: 100px;
  16. /* top: 50%;
  17. left: 50%;
  18. margin-top: -50px;
  19. margin-left: -50px; */
  20. top: calc(50% - 50px);
  21. left: calc(50% - 50px);
  22. background: #109D71;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="content">
  28. <div class="box">
  29. </div>
  30. </div>
  31. </body>
  32. </html>

5.通过translate将元素移动自身的50%

translate(-50%,-50%) 属性:向上(x轴)和左(y轴),移动自身长宽的 50%,使其居于中心位置。
top: 50%;left: 50%;:是以窗口左上角为原点,需要减掉自身宽高的一半,才能居中。
与使用margin实现居中不同的是,margin必须知道自身的宽高,而translate可以在不知道宽高的情况下进行居中,tranlate函数中的百分比是相对于自身宽高的百分比。

  1. <!DOCTYPE html>
  2. <head>
  3. <title>absolute+translate</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <style>
  6. .content {
  7. width: 300px;
  8. height: 300px;
  9. border: 1px solid #109D71;
  10. position: relative;
  11. }
  12. .box {
  13. position: absolute;
  14. width: 100px;
  15. height: 100px;
  16. top: 50%;
  17. left: 50%;
  18. transform: translate(-50%, -50%);
  19. background: #109D71;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="content">
  25. <div class="box">
  26. </div>
  27. </div>
  28. </body>
  29. </html>

6.flex 弹性布局实现

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex布局</title>
  6. <style>
  7. .content {
  8. width: 300px;
  9. height: 300px;
  10. margin: 50px;
  11. border: 1px solid #109D71;
  12. display: flex;
  13. align-items: center;
  14. justify-content: center;
  15. }
  16. .box{
  17. width: 150px;
  18. height: 150px;
  19. background-color: #109D71;
  20. text-align: center;
  21. margin: 0 auto;
  22. display: flex;
  23. align-items: center;
  24. justify-content: center;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div class="content">
  30. <div class="box">
  31. 水平垂直居中
  32. </div>
  33. </div>
  34. </body>
  35. </html>

7.grid网格布局实现

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>flex布局</title>
  6. <style>
  7. .content {
  8. width: 300px;
  9. height: 300px;
  10. margin: 50px;
  11. border: 1px solid #109D71;
  12. display: grid;
  13. place-items: center;
  14. place-content: center;
  15. }
  16. .box {
  17. width: 150px;
  18. height: 150px;
  19. background-color: #109D71;
  20. text-align: center;
  21. margin: 0 auto;
  22. display: grid;
  23. place-items: center;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="content">
  29. <div class="box">
  30. 水平垂直居中
  31. </div>
  32. </div>
  33. </body>
  34. </html>

8.利用display:table-cell属性来实现

display:table-cell;结合vertical-align: middle;使用实现垂直居中,margin:0 atuo;可以实现子元素的水平居中。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>table-cell居中</title>
  6. <style type="text/css">
  7. .content{
  8. border: 1px solid blue;
  9. display: table;
  10. margin: 50px auto;
  11. }
  12. .table{
  13. height: 300px;
  14. width: 300px;
  15. display: table-cell;
  16. vertical-align: middle;
  17. border: 1px solid red;
  18. }
  19. .box{
  20. height: 150px;
  21. width: 150px;
  22. background: #109D71;
  23. margin: 0 auto;
  24. display: table;
  25. }
  26. .word{
  27. display: table-cell;
  28. vertical-align: middle;
  29. text-align: center;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="content">
  35. <div class="table">
  36. <div class="box">
  37. <div class="word">
  38. 垂直水平居中
  39. </div>
  40. </div>
  41. </div>
  42. </div>
  43. </body>
  44. </html>

以上八种方法均可以实现水平垂直居中对齐,具体实现方法根据情况而使用!

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学