我有以下代码:
#container {
background: steelblue;
width: 333px;
height: 333px;
}
picture img {
object-fit: cover;
}
<div id="container">
<picture>
<img src="http://placekitten.com/g/300/300" alt="kitten">
</picture>
</div>
如何让图片填满容器盒?
这是一个代码笔 https://codepen.io/Ciwan/pen/QaNvGa.
你必须使用display:flex
在“图片”元素中(与object-fit
“img”的属性)
这对于任何大小的容器都适用。
like so:
#container {
background: steelblue;
width: 333px;
height: 500px;
}
picture {
width: 100%;
height: 100%;
display: flex;
}
picture img {
object-fit: cover;
height: auto;
width:100%;
}
<div id="container">
<picture>
<img src="http://placekitten.com/g/300/300" alt="kitten">
</picture>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)