1 | # object-fit-images [![gzipped size][badge-gzip]](#no-link) [![build status][badge-travis]][link-travis] [![npm][badge-version]][link-npm] [![CDNJS][badge-cdnjs]][link-cdnjs] [![jsDelivr][badge-jsdelivr]][link-jsdelivr]
|
2 |
|
3 | [badge-gzip]: https://badges.herokuapp.com/size/github/bfred-it/object-fit-images/master/dist/ofi.min.js?gzip=true&label=gzipped%20size
|
4 | [badge-travis]: https://api.travis-ci.org/bfred-it/object-fit-images.svg
|
5 | [badge-version]: https://img.shields.io/npm/v/object-fit-images.svg
|
6 | [badge-cdnjs]: https://img.shields.io/cdnjs/v/object-fit-images.svg
|
7 | [badge-jsdelivr]: https://data.jsdelivr.com/v1/package/npm/object-fit-images/badge?style=rounded
|
8 | [link-travis]: https://travis-ci.org/bfred-it/object-fit-images
|
9 | [link-npm]: https://www.npmjs.com/package/object-fit-images
|
10 | [link-cdnjs]: https://cdnjs.com/libraries/object-fit-images
|
11 | [link-jsdelivr]: https://www.jsdelivr.com/package/npm/object-fit-images
|
12 |
|
13 | > π» Polyfill object-fit/object-position on `<img>`: IE9, IE10, IE11, Edge, Safari, ...
|
14 |
|
15 | - Fast and lightweight ([demo](http://bfred-it.github.io/object-fit-images/demo/))
|
16 | - No additional elements are created
|
17 | - Setup is done via CSS
|
18 | - Scaling is taken care by the browser (it uses `background-size`)
|
19 | - `srcset` support
|
20 | - `src` and `srcset` properties and attributes keep working: `img.src = 'other-image.jpg'`
|
21 |
|
22 | ## Alternative solutions
|
23 |
|
24 |
|
25 | Comparison | bfred-it<br>/object-fit-imagesπ | [constancecchen<br>/object-fit-polyfill](https://github.com/constancecchen/object-fit-polyfill) | [tonipinel<br>/object-fit-polyfill](https://github.com/tonipinel/object-fit-polyfill)
|
26 | --- | --- | --- | ---
|
27 | Browsers | <sub>IEdge 9-14, Android<5, Safari<10</sub> | <- Same | "All browsers"
|
28 | Tags | `img` | `image` `video` `picture` | `img`
|
29 | `cover/contain` | π | π | π
|
30 | `fill` | π | π | π
|
31 | `none` | π | π | π
|
32 | `scale-down` | π <sub>using [`{watchMQ:true}`](#apply-on-resize)</sub> | π | π
|
33 | `object-position` | π | π | π
|
34 | `srcset` support | π Native or [picturefill](https://github.com/scottjehl/picturefill) <sub>[notes](detailed-support-tables.md)</sub> | π | π
|
35 | Extra elements | π No | π Yes | π Yes
|
36 | Settings | π via CSS | π via HTML | π via HTML
|
37 |
|
38 |
|
39 | ## Usage
|
40 |
|
41 | You will need 3 things
|
42 |
|
43 | 1. one or more `<img>` elements with `src` or `srcset`
|
44 |
|
45 | ```html
|
46 | <img class='your-favorite-image' src='image.jpg'>
|
47 | ```
|
48 |
|
49 | 2. CSS defining `object-fit` and a special `font-family` property to allow IE to read the correct value
|
50 |
|
51 | ```css
|
52 | .your-favorite-image {
|
53 | object-fit: contain;
|
54 | font-family: 'object-fit: contain;';
|
55 | }
|
56 | ```
|
57 |
|
58 | or, if you also need `object-position`
|
59 |
|
60 | ```css
|
61 | .your-favorite-image {
|
62 | object-fit: cover;
|
63 | object-position: bottom;
|
64 | font-family: 'object-fit: cover; object-position: bottom;';
|
65 | }
|
66 | ```
|
67 |
|
68 | To generate the `font-family` automatically, you can use the [PostCSS plugin](https://github.com/ronik-design/postcss-object-fit-images) or the [SCSS/SASS/Less mixins.](/preprocessors)
|
69 |
|
70 | If you set the `font-family` via JavaScript (which must be followed by calling `objectFitImages()`), make sure to include the quotes [**in** the property.](https://github.com/bfred-it/object-fit-images/issues/29#issuecomment-227491892)
|
71 |
|
72 | 3. <a name="activation"></a> the activation call before `</body>`, or _on DOM ready_
|
73 |
|
74 | ```js
|
75 | objectFitImages();
|
76 | // if you use jQuery, the code is: $(function () { objectFitImages() });
|
77 | ```
|
78 |
|
79 | This will fix all the images on the page **and** also all the images added later (auto mode).
|
80 |
|
81 | Alternatively, only fix the images you want, once:
|
82 |
|
83 | ```js
|
84 | // pass a selector
|
85 | objectFitImages('img.some-image');
|
86 | ```
|
87 |
|
88 | ```js
|
89 | // an array/NodeList
|
90 | var someImages = document.querySelectorAll('img.some-image');
|
91 | objectFitImages(someImages);
|
92 | ```
|
93 |
|
94 | ```js
|
95 | // a single element
|
96 | var oneImage = document.querySelector('img.some-image');
|
97 | objectFitImages(oneImage);
|
98 | ```
|
99 |
|
100 | ```js
|
101 | // or with jQuery
|
102 | var $someImages = $('img.some-image');
|
103 | objectFitImages($someImages);
|
104 | ```
|
105 |
|
106 | You can call `objectFitImages()` on the same elements more than once without issues, for example to manually request an update of the `object-fit` value.
|
107 |
|
108 | ## Apply on `resize`
|
109 |
|
110 | You don't need to re-apply it on `resize`, unless:
|
111 |
|
112 | * You're using `scale-down`, or
|
113 | * <a id="media-query-affects-object-fit-value">your media queries change the value of `object-fit`,</a> like this
|
114 |
|
115 | ```css
|
116 | img { object-fit: cover; }
|
117 | @media (max-width: 500px) { img { object-fit: contain; } }
|
118 | ```
|
119 |
|
120 | In one of those cases, use the `watchMQ` option:
|
121 |
|
122 | ```js
|
123 | objectFitImages('img.some-image', {watchMQ: true});
|
124 | // or objectFitImages(null, {watchMQ: true}); // for the auto mode
|
125 | ```
|
126 |
|
127 | ## Install
|
128 |
|
129 | Pick your favorite:
|
130 |
|
131 | ```html
|
132 | <script src="dist/ofi.min.js"></script>
|
133 | <!-- CDN is also available, but I suggest you concatenate JS files instead -->
|
134 | <!-- Visit https://cdnjs.com/libraries/object-fit-images -->
|
135 | ```
|
136 |
|
137 | ```sh
|
138 | npm install --save object-fit-images
|
139 | ```
|
140 |
|
141 | ```js
|
142 | var objectFitImages = require('object-fit-images');
|
143 | ```
|
144 |
|
145 | ```js
|
146 | import objectFitImages from 'object-fit-images';
|
147 | ```
|
148 |
|
149 | ## API
|
150 |
|
151 | ### `objectFitImages(images, options)`
|
152 |
|
153 | Both parameters are optional.
|
154 |
|
155 | <table>
|
156 | <tr>
|
157 | <th>parameter</th>
|
158 | <th>description</th>
|
159 | </tr>
|
160 | <tr>
|
161 | <th><code>images</code></th>
|
162 | <td>
|
163 | Type: <code>string</code>, <code>element</code>, <code>array</code>, <code>NodeList</code>, <code>null</code><br>
|
164 | Default: <code>null</code><br><br>
|
165 | The images to fix. More info in the <a href="#usage">Usage</a> section
|
166 | </td>
|
167 | </tr>
|
168 | <tr>
|
169 | <th><code>options</code></th>
|
170 | <td>
|
171 | Type: <code>object</code><br>
|
172 | Default: <code>{}</code><br>
|
173 | Example: <code>{watchMQ:true}</code><br><br>
|
174 | <table>
|
175 | <tr>
|
176 | <th><code>watchMQ</code></th>
|
177 | <td>
|
178 | Type: <code>boolean</code><br>
|
179 | Default: <code>false</code><br><br>
|
180 | This enables the automatic re-fix of the selected images when the window resizes. You only need it <a href="#apply-on-resize">in some cases</a>
|
181 | </td>
|
182 | </tr>
|
183 | </table>
|
184 | </td>
|
185 | </tr>
|
186 | </table>
|
187 |
|
188 | ## License
|
189 |
|
190 | MIT Β© [Federico Brigante](http://twitter.com/bfred_it)
|