html中div居中

鉴于每次都有人问css垂直居中,如今写个小例子。。。css

<html>

<head>
    <title>测试</title>
    <style>
        div {
            position: absolute;
            top: 50%;
            left: 50%;
            margin: -150px 0 0 -200px;
            width: 400px;
            height: 300px;
            border: 1px solid red;
        }
    </style>
</head>

<body>
    <div></div>
</body>

</html>

效果是:html

相关文章
相关标签/搜索