css3-实现任意元素居中显示

2019-08-27  本文已影响0人  AssertDo
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            width: 400px;
            height: 400px;
            border-radius: 200px;
            background-color: #ccc;
            margin:100px auto;
            position: relative;
        }
        .rec{
            width: 200px;
            height: 200px;
            background-color: red;
            position: absolute;
            /*定位的百分比是参照父容器的宽高*/
            left: 50%;
            top: 50%;
            /*使用transform实现元素的居中  百分比是参照元素本身的宽高*/
            transform: translate(-50%,-50%);
        
        }
    </style>
</head>
<body>
<div class="box">
    <div class="rec"></div>
</div>
</body>
</html>

上一篇 下一篇

猜你喜欢

热点阅读