CSS垂直水平居中方式大全(二)----水平垂直居中

ゞ 浴缸里的玫瑰 2022-06-04 02:09 588阅读 0赞

1.利用绝对定位+transform(不固定宽高水平垂直居中)

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. position: relative;
  9. width: 500px;
  10. height: 400px;
  11. background: #faebcc;
  12. }
  13. #child {
  14. position: absolute;
  15. top: 50%;
  16. left: 50%;
  17. transform: translate(-50%);
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="box">
  23. <div id="child">Hello world</div>
  24. </div>
  25. </body>
  26. </html>

效果:

20171201145318463

2.利用绝对定位+margin(.child宽高固定)

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. position: relative;
  9. width: 500px;
  10. height: 400px;
  11. background: #faebcc;
  12. }
  13. #child {
  14. position: absolute;
  15. top: 50%;
  16. left: 50%;
  17. width: 100px;
  18. height: 80px;
  19. background: #999999;
  20. margin: -40px 0 0 -50px;/*负值取宽、高的一半*/
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="box">
  26. <div id="child">Hello world</div>
  27. </div>
  28. </body>
  29. </html>

效果:

20171201145452103

3.利用定位与margin:auto

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. position: relative;
  9. width: 500px;
  10. height: 400px;
  11. background: #faebcc;
  12. }
  13. #child {
  14. position: absolute;
  15. width: 100px;
  16. height: 80px;
  17. background: #DDDDDD;
  18. top: 0; right: 0; bottom: 0; left: 0; margin: auto;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="box">
  24. <div id="child">Hello world</div>
  25. </div>
  26. </body>
  27. </html>

效果:

20171201145628414

4.利用display:table-cell

CSS中有一个用于竖直居中的属性vertical-align,但只有当父元素为td或者th时,这个vertical-align属性
才会生效,对于其他块级元素,例如 div、p等,默认情况下是不支持vertical-align属性的,设置块级元素的
display类型为table-cell,激活vertical-align属性,但display:table-cell存在兼容性问题,
所以这种方法没办法跨浏览器兼容。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #box {
  8. display: table-cell;
  9. vertical-align: middle;/*垂直*/
  10. text-align: center;/*水平*/
  11. width: 500px;
  12. height: 400px;
  13. background: #faebcc;
  14. }
  15. #child {
  16. display: inline-block;
  17. width: 100px;
  18. height: 80px;
  19. background: #f65f57;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="box">
  25. <div id="child">Hello world</div>
  26. </div>
  27. </body>
  28. </html>

效果:

20171201145848312

5.页面居中

方法一:

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #child {
  8. width:100px;
  9. height:100px;
  10. background:aqua;
  11. position:absolute;
  12. top:0;
  13. left:0;
  14. right:0;
  15. bottom:0;
  16. margin:auto;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div id="child">Hello world</div>
  22. </body>
  23. </html>

效果:

20171201161220507

方法二:

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #child {
  8. width:100px;
  9. height:100px;
  10. background:aqua;
  11. position:absolute;
  12. top:50%;
  13. left:50%;
  14. margin-left:-50px;
  15. margin-bottom:-50px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="child">Hello world</div>
  21. </body>
  22. </html>

效果:

20171201161350359

方法三:

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #child {
  8. background:aqua;
  9. position: absolute;
  10. width: 6em;
  11. height: 6em;
  12. top: calc(50% - 3em);
  13. left: calc(50% - 3em);
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="child">Hello world</div>
  19. </body>
  20. </html>

效果:

20171201161500494

方法四:

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>居中</title>
  6. <style>
  7. #child {
  8. background: #999999;
  9. position: absolute;
  10. top:50%;
  11. left:50%;
  12. transform:translate(-50% -50%);
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="child">Hello world</div>
  18. </body>
  19. </html>

效果:

20171201161649334

发表评论

表情:
评论列表 (有 0 条评论,588人围观)

还没有评论,来说两句吧...

相关阅读

    相关 CSS水平垂直居中

            在实际前端开发中,我们会遇到让一些元素居中的情况,如水平居中、垂直居中或者水平垂直居中等等。CSS的表示方法有很多,现在小编在这里总结一下,以便开发的时候可以快