美文网首页
使用table-cell,inline-block实现水平垂直居

使用table-cell,inline-block实现水平垂直居

作者: 凌Linny | 来源:发表于2017-03-05 13:12 被阅读0次

适合在不知道居中元素的宽高的情况下使用;

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>居中</title>
    <style type="text/css">
        .container {
            /*父级元素需要规定宽高*/
            width: 600px;
            height: 300px;
            /*使行内元素或者行内块级元素水平居中*/
            text-align: center;  
            background-color: #ccc;
            display: table-cell;
            vertical-align: middle;
        }
        .center-p {
            background-color: yellow;
            display: inline-block;
        }

    </style>
</head>
<body>
    <div class="container">
        <div class="center-p">
            table-cell居中
        </div>
    </div>
</body>
</html>

相关文章

网友评论

      本文标题:使用table-cell,inline-block实现水平垂直居

      本文链接:https://www.haomeiwen.com/subject/vxwsgttx.html