方案一 flex布局方式

    1. <div class="father">
    2. <div class="son"></div>
    3. </div>
    1. .father {
    2. width: 200px;
    3. height: 200px;
    4. background-color: pink;
    5. display: flex;
    6. justify-content: center;
    7. align-items: center;
    8. }
    9. .son {
    10. width: 50px;
    11. height: 50px;
    12. background-color: yellow;
    13. }

    看效果请点击案例
    方案二 用text-align

    1. .father {
    2. width: 200px;
    3. height: 200px;
    4. background-color: pink;
    5. text-align: center;
    6. line-height: 200px;
    7. }
    8. .son {
    9. display: inline-block;
    10. width: 50px;
    11. height: 50px;
    12. background-color: yellow;
    13. }

    看效果请点击案例 如何让子盒子在父盒子中垂直居中-方法二
    方案三 用子绝对父相对移动子盒子自身的一半

    1. .father {
    2. width: 200px;
    3. height: 200px;
    4. background-color: pink;
    5. position: relative;
    6. }
    7. .son {
    8. width: 50px;
    9. height: 50px;
    10. background-color: yellow;
    11. position: absolute;
    12. left: 50%;
    13. top: 50%;
    14. margin-top: -25px;
    15. margin-left: -25px;
    16. }

    看效果请点击案例 如何让子盒子在父盒子中垂直居中-方法三
    方案四 子绝父相和外边距margin实现

    1. .father {
    2. width: 200px;
    3. height: 200px;
    4. background-color: pink;
    5. position: relative;
    6. }
    7. .son {
    8. width: 50px;
    9. height: 50px;
    10. background-color: yellow;
    11. position: absolute;
    12. left: 0;
    13. top: 0;
    14. right: 0;
    15. bottom: 0;
    16. margin: auto;
    17. }

    看效果请点击案例 如何让子盒子在父盒子中垂直居中-方法四
    方案五子绝父相子位移实现

    1. .father {
    2. width: 200px;
    3. height: 200px;
    4. background-color: pink;
    5. position: relative;
    6. }
    7. .son {
    8. width: 50px;
    9. height: 50px;
    10. background-color: yellow;
    11. position: absolute;
    12. left: 50%;
    13. top: 50%;
    14. transform: translate(-50%, -50%);
    15. }

    看效果请点击案例 如何让子盒子在父盒子中垂直居中-方法五