css-定位让盒子居中显示

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .fu {
            position: relative;
            width: 400px;
            height: 400px;
            background-color: pink;
        }

        /* .fu div:first-child {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            background-color: purple;
        } */

        .fu div:first-of-type {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 200px;
            height: 200px;
            background-color: purple;
        }
    </style>
</head>

<body>
    <div class="fu">
        <div class="child"></div>
    </div>
</body>

</html>
posted @ 2025-09-24 09:17  紫川先生  阅读(8)  评论(0)    收藏  举报