1. 程式人生 > >內聯元素在塊級元素內部垂直水平居中

內聯元素在塊級元素內部垂直水平居中

<html>

<head>
    <style>
        *{margin:0;padding:0}
        .box {
            position: absolute;
            width:100%;
            height:100%;
            background-color:gray;
            text-align: center;
        }
        .middle{
            padding:3px;
            background-color: orange;
        }
        .box:after{
            display:inline-block;
            height:100%;
            content:'';
            vertical-align: middle;
        }
    </style>
</head>

<body>
    <div class="box">
    <span class="middle">行內元素</span>
    </div>
</body>

</html>