让浮动元素垂直居中

时间:2020-05-01
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        .parent {
            background-color: antiquewhite;
            width: 300px;
            height: 300px;
            position: relative;
        }
        .parent .son{
            width: 50px;
            height: 50px;
            background-color: green;
            float: left;
            position: absolute;
            left: 50%;
            top:50%;
            margin-left: -25px;
            margin-top: -25px;
        }
    </style>
</head>
<body>
<div class="parent">
    <div class="son">

    </div>
</div>
</body>
</html>