Polyfill object-fit and object-position on images on IE9, IE10, IE11, Edge, Safari, ...
npm install object-fit-images [badge-gzip]: https://badges.herokuapp.com/size/github/bfred-it/object-fit-images/master/dist/ofi.min.js?gzip=true&label=gzipped%20size
[badge-travis]: https://api.travis-ci.org/bfred-it/object-fit-images.svg
[badge-version]: https://img.shields.io/npm/v/object-fit-images.svg
[badge-cdnjs]: https://img.shields.io/cdnjs/v/object-fit-images.svg
[badge-jsdelivr]: https://data.jsdelivr.com/v1/package/npm/object-fit-images/badge?style=rounded
[link-travis]: https://travis-ci.org/bfred-it/object-fit-images
[link-npm]: https://www.npmjs.com/package/object-fit-images
[link-cdnjs]: https://cdnjs.com/libraries/object-fit-images
[link-jsdelivr]: https://www.jsdelivr.com/package/npm/object-fit-images
> π» Polyfill object-fit/object-position on : IE9, IE10, IE11, Edge, Safari, ...
- Fast and lightweight (demo)
- No additional elements are created
- Setup is done via CSS
- Scaling is taken care by the browser (it uses background-size)
- srcset support
- src and srcset properties and attributes keep working: img.src = 'other-image.jpg'
Comparison | bfred-it
/object-fit-imagesπ | constancecchen
/object-fit-polyfill | tonipinel
/object-fit-polyfill
--- | --- | --- | ---
Browsers | IEdge 9-14, Android<5, Safari<10 | <- Same | "All browsers"
Tags | img | image video picture | imgcover/contain | π | π | πfill | π | π | πnone | π | π | πscale-down | π using {watchMQ:true} | π | πobject-position | π | π | πsrcset support | π Native or picturefill notes | π | π
Extra elements | π No | π Yes | π Yes
Settings | π via CSS | π via HTML | π via HTML
You will need 3 things
1. one or more elements with src or srcset
``html`

object-fit
2. CSS defining and a special font-family property to allow IE to read the correct value
`css`
.your-favorite-image {
object-fit: contain;
font-family: 'object-fit: contain;';
}
object-position
or, if you also need `
css`
.your-favorite-image {
object-fit: cover;
object-position: bottom;
font-family: 'object-fit: cover; object-position: bottom;';
}
font-family
To generate the automatically, you can use the PostCSS plugin or the SCSS/SASS/Less mixins.font-family
If you set the via JavaScript (which must be followed by calling objectFitImages()), make sure to include the quotes in the property.
, or _on DOM ready_
`js`
objectFitImages();
// if you use jQuery, the code is: $(function () { objectFitImages() });
`
This will fix all the images on the page and also all the images added later (auto mode).
Alternatively, only fix the images you want, once:
js`
// pass a selector
objectFitImages('img.some-image');
`
js`
// an array/NodeList
var someImages = document.querySelectorAll('img.some-image');
objectFitImages(someImages);
`
js`
// a single element
var oneImage = document.querySelector('img.some-image');
objectFitImages(oneImage);
`
js`
// or with jQuery
var $someImages = $('img.some-image');
objectFitImages($someImages);
objectFitImages()
You can call on the same elements more than once without issues, for example to manually request an update of the object-fit value.
You don't need to re-apply it on resize, unless:
* You're using scale-down, orobject-fit
* your media queries change the value of , like this
`css`
img { object-fit: cover; }
@media (max-width: 500px) { img { object-fit: contain; } }
In one of those cases, use the watchMQ option:
`js`
objectFitImages('img.some-image', {watchMQ: true});
// or objectFitImages(null, {watchMQ: true}); // for the auto mode
Pick your favorite:
`html`
`sh`
npm install --save object-fit-images
`js`
var objectFitImages = require('object-fit-images');
`js``
import objectFitImages from 'object-fit-images';
Both parameters are optional.
| parameter | description | ||
|---|---|---|---|
images | Type: string, element, array, NodeList, nullDefault: nullThe images to fix. More info in the Usage section | ||
options | Type: objectDefault: {}Example: {watchMQ:true}
|
MIT Β© Federico Brigante