CSS 对象适合位置fill
,contain
正在工作,但cover
工作方式类似fill
. 无论图像有多少部分,它都必须在不拉伸的情况下填充 div cover
。我将 div 设置为flex
并应用于object fit position
div 内部的图像。为什么cover
不工作?如果我将高度设置px
为图像它可以工作,但图像必须根据 div 调整大小,所以我设置了高度auto
<html>
<head>
<style>
.left{
float:left;
}
.col100{
width:100%;
}
.col33{
width:33%;
}
.h30{
height:30vw;
}
.mright10{
margin-right:10px;
}
.bgred{
background:red;
}
.bold{
font-weight:bold;
}
.cover{
object-fit: cover;
}
.contain{
object-fit: contain;
}
.fill{
object-fit: fill;
}
.flex{
display:flex;
}
</style>
</head>
<body>
<div class="left col33 mright10">
<p class="left col100">Original Size</p>
<section class="col100 left h30 bgred">
<img class="col100" src="https://www.gstatic.com/webp/gallery/1.sm.jpg" />
</section>
</div>
<div class="left col33">
<p class="left col100">contain</p>
<section class="col100 left h30 bgred flex">
<img class="col100 cover contain" src="https://www.gstatic.com/webp/gallery/1.sm.jpg" />
</section>
</div>
<div class="left col33 mright10">
<p class="left col100">fill</p>
<section class="col100 left h30 bgred flex">
<img class="col100 cover fill" src="https://www.gstatic.com/webp/gallery/1.sm.jpg" />
</section>
</div>
<div class="left col33">
<p class="left col100 bold">Cover - but image is being stretched !</p>
<section class="col100 left h30 bgred flex">
<img class="col100 cover cover" src="https://www.gstatic.com/webp/gallery/1.sm.jpg" />
</section>
</div>
</body>
</html>