css 未知宽高水平垂直居中

面试经常问道的问题,其实不难的

<style>
        /* 方法好几种  未知宽高元素居中 */

        .div {
            width: 500px;
            height: 500px;
            background: pink;
            position: relative;
        }

        .span1 {
            position: absolute;
            top: 50%;
            left: 50%;
            color: #fff;
            transform: translate(-50%, -50%);
        }
    </style>
</head>

<body>
    <div class="div">
        <span class="span1">上下左右垂直居中,位置宽高</span>
    </div>
</body>

猜你喜欢

转载自blog.csdn.net/lbpro0412/article/details/81189548
今日推荐