1. 参考
  2. 水平居中
    1. 行内(内联)元素水平居中
    2. 块状元素水平居中
    3. 多个块状元素水平居中
  3. 垂直居中
    1. 行内元素|行内块状元素垂直居中
      1. 单行
      2. 多行
    2. 块状元素
      1. 块状元素高度已知
      2. 块状元素未知高度
      3. 无外边距的块状元素

元素居中

参考

Centering in CSS: A Complete Guide

position:absolute和margin:auto连用居中的原理

水平居中

行内(内联)元素水平居中

.box{text-align: center;}
inline element
<style>
    .box-1 {width:200px;border:1px solid;}
</style>
<div class="box-1" style="text-align:center">
    <span>inline element</span>
</div>

块状元素水平居中

.child{margin: 0 auto;}
<style>
    .box-2 {width:200px;border:1px solid;}
    .child-2 {background:yellow;width:100px;height:20px;}
</style>
<div class="box-2">
    <div class="child-2" style="margin: 0 auto"></div>
</div>

多个块状元素水平居中

1 转换为行内块元素

.box{text-align:center;}
.child{display: inline-block;}  
1
2
<style>
    .box-3 {width:200px;border:1px solid;}
    .child-3 {background:yellow;width:50px;height:20px;}
</style>        
<div class="box-3" style="text-align: center">
    <div class="child-3" style="display:inline-block;">1</div>
    <div class="child-3" style="display:inline-block;">2</div>
</div>

内联块元素默认对齐基线,所以父容器底部会空出来一块

1
2
<style>
    .box-31 {width:200px;border:1px solid;font-size:0;}
    .child-31 {background:yellow;width:50px;height:20px;font-size:14px;}
</style>        
<div class="box-31" style="text-align: center">
    <div class="child-31" style="display:inline-block;">1</div>
    <div class="child-31" style="display:inline-block;">2</div>
</div>

2 利用flex布局

.box{
    display: flex;
    justify-content: center;
}
1
2
<style>
    .box-4 {width:200px;border:1px solid;}
    .child-4 {background:yellow;width:50px;height:20px;}
</style> 
<div class="box-4" style="text-align: center">
    <div class="child-4" style="display:inline-block;">1</div>
    <div class="child-4" style="display:inline-block;">2</div>
</div>

垂直居中

行内元素|行内块状元素垂直居中

单行

1 利用父容器内边距垂直居中

    .box {
        padding-top:30px;
        padding-bottom:30px;
    }
inline
<style>
    .box-5 {width:200px;border:1px solid;}
    .child-5 {background:yellow;}
</style>     
<div class="box-5" style="padding:20px 0 ;">
    <span class="child-5">inline</span>
</div> 

2 容器高度固定,利用行高垂直居中

.box {
    height:100px;
    line-height:100px;
}
inline

多行

1 利用表的特性

.box{display:table;vertical-align:middle;}
.child{display:table-cell;vertical-align:middle;}
Alice was beginning to get very tired of sitting by her sister on the bank
<style>
    .box-7 {width:200px;height:100px;border:1px solid;}
</style>
<div class="box-7" style="display:table;">
    <span style="display:table-cell;vertical-align:middle;">
        Alice was beginning to get very tired of sitting by her sister on the bank
    </span>
</div>

2 利用伪元素和vertical-align:middle

.box::before {
    content: "";
    display: inline-block;
    height: 100%;
    width: 0;
    vertical-align: middle;
}
.child {
    display: inline-block;
    vertical-align: middle;
}
Alice was beginning to get very tired of sitting by her sister on the bank
<style type="text/css">
    .box-8 {
        width: 200px;
        height: 150px;
        border: 1px solid;
    }
    .child-8 {
        width: 180px;
        background: green;
    }
    /*添加伪元素*/
    .box-8:before {
        content:"";
        display: inline-block;
        width: 4px;
        height: 100%;
        background: yellow;
        vertical-align: middle;
    }
</style>
<div class="box-8">
    <span class="child-8" style="display:inline-block;vertical-align: middle;">
        Alice was beginning to get very tired of sitting by her sister on the bank
    </span>
</div>      

inline-block元素间有缝隙,当子元素宽度为100%时会被挤下去(伪元素width:0也一样)

My width is 100% - 4px;


块状元素

块状元素高度已知

position: absolute;
height: 100px;
top: 50%;
margin-top: -50px;
<style>
   .box-10 {
        width:200px;
        height:100px;
        position: relative;
        border: 1px solid;
   }
   .child-10 {
        position: absolute;
        top:50%;
        margin-top: -30px;
        width:50px;
        height:60px;
        background:yellow;
   } 
</style>
<div class="box-10">
    <div class="child-10"></div>
</div> 

块状元素未知高度

display:absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
<style>
    .box-11 {
        width:200px;
        height:100px;
        position: relative;
        border: 1px solid;
    }
    .child-11 {
        position: absolute;
        top:50%;
        transform: translateY(-50%);
        width:50px;
        height: 30%;
        background:yellow;
    } 
</style>
<div class="box-11">
    <div class="child-11"></div>
</div> 

absolute会导致margin: 0 auto失效,可用.box{left: 50%; transform: translateX(-50%);}代替

无外边距的块状元素

display:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin: auto;   
<style>
   .box-12 {
        width:100px;
        height:100px;
        position: relative;
        border: 1px solid;
   }
   .child-12 {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        width:50px;
        height: 50px;
        background:yellow;
   } 
</style>
<div class="box-12">
    <div class="child-12"></div>
</div>