美文网首页
利用position相对绝对定位实现水居中

利用position相对绝对定位实现水居中

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

虽然都是绝对定位,但是跟绝对定位加负边距的那种居中方式不一样,这种绝对定位的居中方式是将上下左右都设置为0;
代码如下:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>居中</title>
    <style type="text/css">
        .container {
            /*使行内元素或者行内块级元素水平居中*/
            text-align: center;  
            height: 400px;
            background-color: #ccc;
            position: relative;
        }
        .center-p {
            background-color: yellow;
            width: 100px;
            height: 80px;
            margin:auto;
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }

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

相关文章

网友评论

      本文标题:利用position相对绝对定位实现水居中

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