盒子绝对定位 position:foxed ; 居中

方法1

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        body{
            width:  100%;
            height: 1600px;
            background-color: #c9c7c7;

        }
        p{
            max-width: 500px;
            width: 100%;
            background: red;
            position: fixed;color: white;
            text-align: center;
            left: 0;
            right: 0;
            margin: auto;
        }
    </style>
</head>
<body>

<p>我是绝对定位 我需要居中</p>

</body>
</html>

方法2  (替换上面style 的p标签的样式      )

p{
            max-width: 500px;
            width: 100%;
            background: red;
            position: fixed;color: white;
            text-align: center;
            left:50%;
            transform: translateX(-50%);

        }

 

posted @ 2017-12-02 09:27  阿奇blog  阅读(458)  评论(0)    收藏  举报