I need to be able to make an image fill the entire container (the container’s width/height set with % or vh/vw), not necessarily keeping the image’s original aspect ratio, but with no overflow or unused space.
I have tried:
.container{
width: 80%; /* for example */
height: 30%;
}
.item{
width: 100%;
height: 100%;
object-fit: cover;
}
but this doesn’t seem to work.
I know this can be done with JS, but I would prefer to do it in a cleaner way.
2
Answers
Yes, you can do it.
Will be like this. Without keeping image aspect ratio.
Check it on codepen https://codepen.io/EkaterinaLabutina/pen/eYwaZgy
Paddings were added on purpose.
It looks like you’re on the right track with the object-fit: cover approach.
You can try this