这实际上是正确的行为。您的目的是让孩子 100% 宽度并与父母一起控制尺寸。
例子:
.parent {
width: 200px;
}
.child {
width: 100%;
background: red;
padding-top: 100%;
}
<div class="parent">
<div class="child"></div>
</div>
这是在 CSS-Tricks 上提出的一个很好的方法,可以帮助您获得所需比例所需的正确填充:
纵横比 - 2:1
.parent {
width: 200px;
}
.child {
width: 100%;
background: red;
padding-top: calc(1 / 2 * 100%); // will give you an aspect ratio of 2:1
}
<div class="parent">
<div class="child"></div>
</div>
纵横比 - 3:1
.parent {
width: 200px;
}
.child {
width: 100%;
background: red;
padding-top: calc(1 / 3 * 100%); // will give you an aspect ratio of 3:1
}
<div class="parent">
<div class="child"></div>
</div>
纵横比 - 16:9
.parent {
width: 200px;
}
.child {
width: 100%;
background: red;
padding-top: calc(9 / 16 * 100%); // will give you an aspect ratio of 16:9
}
<div class="parent">
<div class="child"></div>
</div>
你可以在这里找到完整的文章:
https ://css-tricks.com/aspect-ratio-boxes/