html中div居中

鉴于每次都有人问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>

效果是:

猜你喜欢

转载自www.cnblogs.com/Cein/p/9025560.html