Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bfred-it/object-fit-images

πŸ—» Polyfill object-fit/object-position on <img>: IE9, IE10, IE11, Edge, Safari, ...
https://github.com/bfred-it/object-fit-images

css edge ie images js polyfill

Last synced: 3 months ago
JSON representation

πŸ—» Polyfill object-fit/object-position on <img>: IE9, IE10, IE11, Edge, Safari, ...

Awesome Lists containing this project

README

        

# 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]

[badge-gzip]: https://badges.herokuapp.com/size/github/fregante/object-fit-images/master/dist/ofi.min.js?gzip=true&label=gzipped%20size
[badge-travis]: https://api.travis-ci.org/fregante/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/fregante/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](http://fregante.github.io/object-fit-images/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'`

## Alternative solutions

Comparison | fregante
/object-fit-images🌟 | [constancecchen
/object-fit-polyfill](https://github.com/constancecchen/object-fit-polyfill) | [tonipinel
/object-fit-polyfill](https://github.com/tonipinel/object-fit-polyfill)
--- | --- | --- | ---
Browsers | IEdge 9-14, Android<5, Safari<10 | <- Same | "All browsers"
Tags | `img` | `image` `video` `picture` | `img`
`cover/contain` | πŸ’š | πŸ’š | πŸ’š
`fill` | πŸ’š | πŸ’š | πŸ’š
`none` | πŸ’š | πŸ’š | πŸ’š
`scale-down` | πŸ’š using [`{watchMQ:true}`](#apply-on-resize) | πŸ’š | πŸ’”
`object-position` | πŸ’š | πŸ’š | πŸ’”
`srcset` support | πŸ’š Native or [picturefill](https://github.com/scottjehl/picturefill) [notes](detailed-support-tables.md) | πŸ’š | πŸ’”
Extra elements | πŸ’š No | πŸ’” Yes | πŸ’” Yes
Settings | πŸ’š via CSS | πŸ’” via HTML | πŸ’” via HTML

## Usage

You will need 3 things

1. one or more `` elements with `src` or `srcset`

```html

```

2. CSS defining `object-fit` 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;';
}
```

or, if you also need `object-position`

```css
.your-favorite-image {
object-fit: cover;
object-position: bottom;
font-family: 'object-fit: cover; object-position: bottom;';
}
```

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)

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/fregante/object-fit-images/issues/29#issuecomment-227491892)

3. the activation call before `