CSS object fit position fill
, contain
are working but cover
works like fill
. It has to fill div without stretching no matter how much part of image would be cover
. I set div to flex
and apply object fit position
to images that are inside in div. Why cover
isn't working ? If I set height in px
to image it works, but Image has to be resized according to div, So I set height 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>