margin设置百分比是基于父容器的宽度,所以无论是margin-top还是margin-bottom都是基于父容器的宽度。

看如下示例:

<style>
    .fu {
        width: 400px;
        height: 300px;
        background: blue;
        overflow: hidden;
    }
    .zi {
        width: 20px;
        height: 20px;
        background: red;
        margin-top: 50%;
    }
</style>
<body>
    <div class="fu">
        <div class="zi"></div>
    </div>
</body>

设置margin-top: 50%;基于父容器宽度相当于margin-top: 200px;

所以代码的效果如图所示。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐