grid 布局


方法一

父元素使用 grid 布局,然后加上align-items:center;``justify-items:center;

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. display:grid;
  7. align-items:center;
  8. justify-items:center;
  9. }
  10. .child{
  11. background:green;
  12. }
  13. </style>
  14. <div class="box">
  15. <div class="child">奥特曼</div>
  16. </div>

https://jsbin.com/tasucaruya/edit?html,output

方法二

父元素使用 grid 布局,给子元素加上margin:auto;

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. display:grid;
  7. }
  8. .child{
  9. background:green;
  10. margin:auto;
  11. }
  12. </style>
  13. <div class="box">
  14. <div class="child">奥特曼</div>
  15. </div>

https://jsbin.com/yokiwisura/edit?html,output

flex 布局


方法一

父元素使用 flex 布局,然后加上justify-content:center;``align-items:center;

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. display:flex;
  7. justify-content:center;
  8. align-items:center;
  9. }
  10. .child{
  11. background:green;
  12. }
  13. </style>
  14. <div class="box">
  15. <div class="child">奥特曼</div>
  16. </div>

https://jsbin.com/guyezuzewe/edit?html,output

方法二

父元素使用 flex 布局,子元素加上margin:auto

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. display:flex;
  7. }
  8. .child{
  9. background:green;
  10. margin:auto;
  11. }
  12. </style>
  13. <div class="box">
  14. <div class="child">奥特曼</div>
  15. </div>

https://jsbin.com/gowokagiko/edit?html,output

绝对定位


方法一

父元素使用相对定位,子元素使用绝对定位,然后给子元素加上left:50%;``top:50%;``transform:translate(-50%,-50%)

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. position:relative;
  7. }
  8. .child{
  9. background:green;
  10. position:absolute;
  11. left:50%;
  12. top:50%;
  13. transform:translate(-50%,-50%)
  14. }
  15. </style>
  16. <div class="box">
  17. <div class="child">奥特曼</div>
  18. </div>

https://jsbin.com/tezenomobe/edit?html,output

方法二

父元素使用相对定位,子元素使用绝对定位,然后left:50%;``top:50%;,在使用这种方法时最好写死子元素的宽高,这样能更准确的居中。然后加上margin-left:-1/2width;``margin-top:-1/2height;

  1. <style>
  2. .box{
  3. height: 200px;
  4. border: 1px solid red;
  5. position: relative;
  6. }
  7. .child{
  8. border: 1px solid green;
  9. position: absolute;
  10. width:100px;
  11. height:100px;
  12. left:50%;
  13. top:50%;
  14. margin-left:-50px;
  15. margin-top:-50px;
  16. }
  17. </style>
  18. <div class="box">
  19. <div class="child">
  20. 奥特曼
  21. </div>
  22. </div>

https://jsbin.com/jacusekesi/edit?html,output

方法三

当父元素使用相对定位,子元素使用绝对定位时,如果使left:0;``top:0;``right:0;``bottom:0;在不写宽高的情况下子元素将会充满整个容器,加上宽高后,它就会跑到容器的左上角。这时我们再给子元素加上margin:auto;它就会内收到居中的位置

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. position:relative;
  7. }
  8. .child{
  9. background:green;
  10. width:50px;
  11. height:30px;
  12. position:absolute;
  13. left:0;
  14. top:0;
  15. right:0;
  16. bottom:0;
  17. margin:auto;
  18. }
  19. </style>
  20. <div class="box">
  21. <div class="child">奥特曼</div>
  22. </div>

https://jsbin.com/nomatadidu/edit?html,output

table-cell


父元素加上display:table-cell;然后vertical-align: middle;``text-align:center;因为父类使用了display:table-cell;所以要把子类变成行内块级元素即给子元素加上display:inline-block;

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. display:table-cell;
  7. vertical-align: middle;
  8. text-align:center;
  9. }
  10. .child{
  11. background:green;
  12. display:inline-block;
  13. }
  14. </style>
  15. <div class="box">
  16. <div class="child">奥特曼</div>
  17. </div>

https://jsbin.com/rozesizubu/edit?html,output

line-height


这种方法是创建了一个伪元素,这个伪元素相当于一个文字加在子元素的后面,这个文字的内容是content:""里面的内容,这个字的高度正好和容器一样高,那么子元素和我们的伪元素要并排到一起,所以就达成了让子元素居中的目的。

  1. <style>
  2. .box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid red;
  6. text-align:center;
  7. }
  8. .box::after{
  9. content:"";
  10. line-height:100px;
  11. }
  12. .child{
  13. display:inline-block;
  14. background:green
  15. }
  16. </style>
  17. <div class="box">
  18. <div class="child">奥特曼</div>
  19. </div>

https://jsbin.com/jovorivuwe/edit?html,output