Roh*_*ith 5 html javascript css aspect-ratio
我想创建一个 div 来保持纵横比与父级的高度和宽度变化。

在上面的 gif 中,您可以看到我能够在更改宽度时保持框 div 的纵横比,但在更改父级的高度时无法保持它。
.box {
width: 100px;
background-color: #dfdfdf;
max-width: 100%;
}
.box:after {
content: '';
display: block;
width: 100%;
height: 100%;
padding-bottom: 160%;
}
.wrap9 {
width: 125px;
height: 190px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap9">
<div class="box"></div>
</div>Run Code Online (Sandbox Code Playgroud)
我希望灰色框的行为如下:

使用默认宽度和高度:
.box {
width: 100px;
background-color: #dfdfdf;
max-width: 100%;
max-height:100%;
}
.box:after {
content: '';
display: block;
width: 100%;
height: 100%;
padding-bottom: 160%;
}
.wrap9 {
width: 150px;
height: 200px;
border: 1px solid black;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap9">
<div class="box"></div>
</div>Run Code Online (Sandbox Code Playgroud)
宽度下宽度和高度:
.box {
width: 100px;
background-color: #dfdfdf;
max-width: 100%;
max-height:100%;
}
.box:after {
content: '';
display: block;
width: 100%;
height: 100%;
padding-bottom: 160%;
}
.wrap9 {
width: 100px;
height: 120px;
border: 1px solid black;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap9">
<div class="box"></div>
</div>Run Code Online (Sandbox Code Playgroud)
这是想要的效果吗?