div水平居中和垂直居中

1:使用table和table-cell布局

    <div style=" 100%;height: 100%;display: table;text-align: center">
        <div style=" 100%;height: 100%;display: table-cell;vertical-align: middle;">
            <div style="display: inline-block; 100px;height: 100px;background: green;">

            </div>
        </div>
    </div>

2:使用flex布局

<style>
    .verticalTop{
        display: flex;
        align-items:flex-start;
        height: 100%;
    }
    .verticalBottom{
        display: flex;
        align-items:flex-end;
        height: 100%;
    }
    .verticalCenter{
        display: flex;
        align-items:center;
        height: 100%;
    }
    .horizaLeft{
        display: flex;
        justify-content:flex-start;
        text-align: left;
         100%;
    }
    .horizaRight{
        display: flex;
        justify-content:flex-end;
        text-align: right;
         100%;
    }
    .horizaCenter{
        display: flex;
        justify-content:center;
        text-align: center;
         100%;
    }
</style>
  <div class="verticalCenter horizaCenter">
        <div style=" 100px;height: 100px;background: yellow;">

        </div>
    </div>

居中效果:

原文地址:https://www.cnblogs.com/JimmyBright/p/6078746.html