by constancecchen

constancecchen / object-fit-polyfill

A Javascript polyfill for browsers that don't support the object-fit CSS property.

480 Stars 100 Forks Last release: over 1 year ago (2.3.0) 66 Commits 12 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:


A polyfill for browsers that don't support the

CSS property. Unsure of what the
does? Essentially
is to
tags what
is to
. You can check out the MDN page for more details.


  • Lightweight - 3KB (2KB with the basic version)
  • Vanilla Javascript - works with or without jQuery
  • Supports IE 9+, iOS 7-, and Android 4.4-
  • Supports
  • Works with
    , and
  • Plug and play - just include the .js file and set data attributes on your elements.
  • Please note: This plugin makes the assumption that the parent container is acting as a picture frame, and already has a height & width set.


You can check out the bare-bones demo here. Note that the plugin simply won't do anything if you're on a browser that already supports object-fit, so you'll want to test it on IE or older iOS/Android browsers.

How does it work?

Unlike object-fit-images or Primož Cigler's method (both excellent alternatives if you'd rather not use this one), this polyfill does not set a background image on the parent container, but instead resizes and repositions the image (using inline CSS for height, width, absolute positioning, and negative margins).

The polyfilled item will receive the class

if styling issues occur that require overrides.

Why bother?

If you're wondering: why bother using

tags versus
? Here's a couple reasons:
  1. tags have better SEO/crawling visibility.
  2. In cases where images are dynamically returned and can't simply be added to your stylesheets (e.g., CMS's), you're forced to inline your background-image. This solves that somewhat-ugly-looking inline CSS.
  3. background-image
    doesn't work with
    , or

Of course, there's still plenty of cases where using a background image makes more sense than a regular image.



Customized object-fit/object-position:

If you're only interested in using the basic polyfill (which assumes

object-fit: cover
object-position: 50% 50%
), you can save yourself some bytes by using:

Installation via package managers

Alternatively, if you prefer not to manually add Javascript files to your sites, you can use bower and npm like so:

bower install objectFitPolyfill
npm install objectFitPolyfill

Advanced usage

If you need to dynamically call the polyfill on the fly for any reason (for example, carousels or lazy-loaded images), you can do so quite easily:

// Rerun the polyfill on all elements with the data attribute

// Rerun the polyfill on a single DOM node var element = document.querySelector('.foo'); objectFitPolyfill(element);

// Rerun the polyfill on multiple elements var elements = document.querySelectorAll('.bar'); objectFitPolyfill(elements);

// Rerun the polyfill with a jQuery selector objectFitPolyfill($('.baz'));


If you'd like to make feature requests such as IE 8- or adding object-position support for Safari, feel free to open an issue or pull request! It's doable and on my radar, but I probably won't get to it without some prodding.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.