I want to use object-fit CSS rule.
You don't need to obtain the value of CSS rule object-fit
rule with JavaScript in MS Edge browser to be able to make an img
cover its parent.
Bonus: No need to use a polyfill and works across all browsers down to IE9
Side note, MS Edge ver. 16 supports object-fit
Here is a solution that works exactly as object-fit: cover
, making use of transform: translate
and min-width/height
.
div {
width: 400px;
height: 320px;
box-sizing: border-box;
border: 1px solid gold;
margin: 0 auto;
overflow: hidden;
}
#i {
position: relative;
display: block;
min-width: 100%;
min-height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div>
<img src="https://dummyimage.com/640x240/333/444.png" id="i" />
</div>
If you want you can use CSS @supports to also reset the above properties and add object-fit
.
div {
width: 400px;
height: 320px;
box-sizing: border-box;
border: 1px solid gold;
margin: 0 auto;
overflow: hidden;
}
#i {
position: relative;
display: block;
min-width: 100%;
min-height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
@supports (object-fit: cover) {
#i {
min-width: auto;
min-height: auto;
top: auto;
left: auto;
transform: none;
width: 100%;
height: 100%;
object-fit: cover;
}
}
<div>
<img src="https://dummyimage.com/640x240/333/444.png" id="i" />
</div>
Another overlooked option when one need to use an img
because one want to set the image source in the markup, is to use a inline style for the background-image
source.
Given the fact, in this case, where a div
with fixed width/height sets the boundaries for the image, here is a simple sample of that.
div {
width: 400px;
height: 320px;
box-sizing: border-box;
border: 1px solid gold;
margin: 0 auto;
overflow: hidden;
background-position: center;
background-size: cover;
}
<div style="background-image: url(https://dummyimage.com/640x240/333/444.png)">
</div>
Updated
You can of course mimic object-fit: contain
as well
div {
width: 400px;
height: 320px;
box-sizing: border-box;
border: 1px solid gold;
margin: 0 auto;
overflow: hidden;
}
#i {
position: relative;
display: block;
max-width: 100%; /* changed to max */
max-height: 100%; /* changed to max */
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div>
<img src="https://dummyimage.com/640x240/333/444.png" id="i" />
</div>
Updated based on a comment
When it comes to mimic object-fit
for video
things gets much more complicated.
The video
element doesn't respond in the same way an image does.
Here is 4 different samples showing that some video work when mimic cover
and some when mimic contain
To achieve a consistent and the same good result as with the image, one need to run a small script to get the video aspect ratio, and then set its width or height to 100% based on its and its container aspect ratio.
Here is another post at SO, simulate background-size:cover on <video> or <img>, which show some more approaches how one can go about this.
One other option I found is to use media query and its max/min-aspect-ratio
Fiddle demo
Src: https://fvsch.com/code/video-background/