css常用水平垂直居中总结

1.flex方法

<style>
        .flex{
            width: 200px;
            height: 200px;
            box-sizing: border-box;
            border: 1px solid red;
            float: left;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .flex > div{
            width: 100px;
            height: 100px;
            background-color: green;
            display: flex;
            justify-content: center;
            align-items: center;
        }
</style>
<body>
    <div class="flex">
        <div>flex</div>
    </div>
</body>

2.定位

<style>
       .ablot{
            width: 200px;
            height: 200px;
            float: left;
            box-sizing: border-box;
            border: 1px solid red;
            position: relative;
        }
        .ablot > div{
            width: 100px;
            height: 100px;
            background-color: green;
            text-align: center;
            line-height: 100px;
            position: absolute;
            left: 0;
            top: 0;
            right: 0;
            bottom: 0;
            margin: auto;
        }
</style>
<bosy>
    <div class="ablot">
        <div>定位</div>
    </div>
</body>
<style>
       .ding{
            width: 200px;
            height: 200px;
            float: left;
            box-sizing: border-box;
            border: 1px solid red;
            position: relative;
        }
        .ding > div{
            width: 100px;
            height: 100px;
            background-color: green;
            text-align: center;
            line-height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            margin: -50px 0 0 -50px;
        }
</style>
<body>
    <div class="ding">
        <div>定位</div>
    </div>
</body>

3.transform

<style>
     .transform{
            width: 200px;
            height: 200px;
            float: left;
            box-sizing: border-box;
            border: 1px solid red;
            position: relative;
        }
        .transform > div{
            width: 100px;
            height: 100px;
            background-color: green;
            text-align: center;
            line-height: 100px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
</style>
<body>
    <div class="transform">
        <div>transform</div>
    </div>
</body>

4.table-cell

<style>
    .table{
            width: 200px;
            height: 200px;
            float: left;
            box-sizing: border-box;
            border: 1px solid red;
            display: table-cell;
            text-align: center;
            line-height: 200px;
        }
        .table > div{
            width: 100px;
            height: 100px;
            display: inline-block;
            background-color: green;
            text-align: center;
            line-height: 100px;
        }
</style>
<body>
    <div class="table">
        <div>table</div>
    </div>
</body>

最后看效果

css常用水平垂直居中总结

 

上一篇:CSS3盒子模型


下一篇:左边宽度固定,右边自适应(方法3)