I am trying to use the css object-fit: cover property in an img element so that my image fills it containing div and transform: scale(xx) so that the image is scaled down on hover.
Here is an example script: https://jsfiddle.net/96kbuncq/
Edit: sample with real images: https://jsfiddle.net/96kbuncq/3/
HTML:
<div> <div class="category"> <img src="http://placehold.it/1200x950&text=1200x950+-+Category+1+-" /> </div> <div class="category"> <img src="http://placehold.it/1200x950&text=1200x950+-+Category+2+-" /> </div> <div class="category"> <img src="http://placehold.it/1200x950&text=1200x950+-+Category+3+-" /> </div> </div>
CSS
..... div.category img { display: block; height: 100%; width: 100%; object-fit: cover; object-position: center; } div.category img { transition: transform 0.35s; transform: scale(1.12); } div.category:hover img { transform: scale(1); }
This works fine in Firefox, but in Chrome and Opera I have the following problems:
- When you hover the first
div , others influence it (and when you hover the second, it affects the third), - When a
div falls, the image inside is fully displayed completely (we can see that the whole image is stretched according to the div ) before properly scaling and โcoveringโ the div .
I do not know how to solve these problems.
About the first issue (affected siblings), I found other answers saying to use translateZ(0) , but when I add this, object-fit: cover no longer works (the whole image is stretched to fit inside the div ).
Any ideas how to make this work in Chrome? (Both object-fit and transform work as expected when used without the other.)
source share