If you are looking to utilize the object-fit
CSS rule, keep in mind that it is not supported in MSIE and MS Edge Browsers. While there are polyfills available for IE, none of them seem to work in Edge from my experience.
For instance, the polyfill fitie by Jonathan Neal works with IE but seems to fail in Edge (at least on the systems I've tested). This could be due to fitie relying on JS objects like element.currentStyle
and element.runtimeStyle
, which are no longer supported in Edge browsers. Using
window.getComputedStyle(element).getPropertyValue('object-fit')
doesn't return any value in Edge.
So how can one retrieve the value of the CSS rule object-fit
using JavaScript in MS Edge browser?
img = document.getElementById('i');
s = self.getComputedStyle(img);
console.log('object-fit: ', s.getPropertyValue('object-fit'));
console.log('-ms-object-fit: ', s.getPropertyValue('-ms-object-fit'));
div {
width: 400px;
height: 320px;
overflow: hidden;
}
#i {
display: block;
width: 100%;
height: 100%;
-ms-object-fit: cover;
object-fit: cover;
}
div {
box-sizing: border-box;
border: 1px solid gold;
}
div,
p {
font-family: sans-serif;
font-size: 20px;
width: 400px;
margin: 0 auto;
}
<p>img: Has object-fit CSS rule, but does not appear in MS Edge JavaScript log</p>
<div>
<img src="https://dummyimage.com/640x240/333/444.png" id="i" />
</div>
Edit
There must be a way around this limitation since it's not completely ignored. The Developer Tools do show the rule underlined as expected.
https://i.sstatic.net/mAhYH.png
Bonus question:
Are there any polyfills available for object-fit that actually work in Edge?