div内容水平垂直居中的正确玩法

PioneerFan 2020-06-10

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style>
        .container {
            text-align: center;
            height: 300px;
            border: 1px solid rebeccapurple;
        }
        .container:before
        {
            display: inline-block;
            vertical-align: middle;
            content: "";
            height: 100%;
        }
        .center {
            display: inline-block;
            vertical-align: middle;
        }
    </style>
</head>

<body>
<div class="container">
    <div class="center">
        <h1>这个是居中标题</h1>
        <p>这里是一些文字,文字</p>
    </div>
</div>
</body>
</html>

改自:http://www.mamicode.com/info-detail-1275208.html

相关推荐