我有一个保持长宽比的
object-fit: contain
- 仅适用于img
。
我希望
只使用CSS是否可能? 如果可以,如何实现?
更新: 一篇很好的文章,介绍了目前的情况。
代码(可以是任何其他解决方案,不必基于此片段构建):
html,
body {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
.container {
position: relative;
width: 100%;
}
.container:before {
content: "";
display: block;
width: 50%;
padding-top: 50%;
}
.embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: red;
}
<div class="container">
<div class="embed">
this should accommodate all the available space and maintain aspect ratio, no crop when width is too wide
</div>
</div>
width:100%;
吗? - Candy Crunch100%
吗? - Adam