CSS实现居中的各种方式

120901.jpg

诗和远方需要路费,星辰大海需要门票

在工作中跟面试的时候经常会问居中,下面总结几种居中的用法

display:table-cell 垂直居中

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                display: table-cell;
                vertical-align: middle;
            }
            .box {
                width: 50px;
                height: 50px;
                background-color: aqua;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

absolute+margin

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                position: relative;
            }
            .box {
                width: 50px;
                height: 50px;
                background-color: aqua;
                position: absolute;
                margin: auto;
                top: 0;
                bottom: 0;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

absolute+负margin

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                position: relative;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
                position: absolute;
                margin-top: -50px;
                top: 50%;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

absolute+calc

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                position: relative;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
                position: absolute;
                top: calc(50% - 50px);
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

absolute + transform

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                position: relative;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

line-height

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                line-height: 400px;
                background-color: red;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
                display: inline-block;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

flex布局

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                display: flex;
                align-items: center;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

grid布局

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
                display: grid;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
                align-self: center;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

效果图


image.png

伪类after或before

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            .container {
                width: 400px;
                height: 400px;
                background-color: red;
            }
            .container::after {
                content: '';
                display: inline-block;
                vertical-align: middle;
                height: 100%;
            }
            .box {
                width: 100px;
                height: 100px;
                background-color: aqua;
                display: inline-block;
                vertical-align: middle;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="box">
                
            </div>
        </div>
    </body>
</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容