This simple jQuery plugin lets you scale images to fit within their container element, as specified for the new CSS object-fit: cover property. It should work in all modern browsers that do not yet support object-fit natively and is compatible with jQuery 1.7+ and 2.*. This has the same effect as using background-size: cover with background images, but lets you use img tags and the new picture module instead.
A simple demo shows you the desired effect
All images within the targeted container object will fill its entire area without changing their aspect ratio. The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box: its concrete object size is resolved as a cover constraint against the element’s used width and height.
As of November 2014 the latest Webkit browsers and Firefox 36 support object-fit. However, Internet Explorer, Firefox before version 36 and older versions of Safari prior to version 7.1 do not.
This plugin only implements object-fit: cover property for img tags wrapped in the targeted containers.
Other plugins, most notably Simon Schmid's jquery.object-fit and Anselm Hannemann's vanilla javascript polyfill, provide more complete support for object-fit and implement other values such as fill, contain and scaled-down and work directly on any HTML object that should respond to object-fit rather than only on elements containing image tags.
Our approach may be less flexible, but is more efficient for situations where you need to fit images within an area within a different aspect ratio. As a jQuery plugin it can be combined with numerous other plugins for slide shows and other effects. Inline styles are added only to browsers that do not support object-fit via a simple test on initial page load.
The plugin will now only check for support once and adds two body classes object-fit-checked and, if support is detected, object-fit-supported. This works much better with multiple images or when loading content asynchronously.
<figure class="image-cover"> <img src="/path-to-images-folder/image.jpg" width="800" height="600" /> </figure>
figure.image-cover { position: relative; width: 50%; height: 20vh; /* viewport height, not supported in IE8*/ overflow: hidden; /* important */ }figure.image-cover img { position: relative; /must have a non-static position to work in browsers that do not support object-fit/ width: 100%; /* initial value overriden by object-fit and/or plugin / height: 100%; / initial value overriden by object-fit and/or plugin / object-fit: cover; / works in latest Webkit browsers + FF 36+ */ }
$('figure.image-fit').imageFitCover();
The plugin does not wait for images to load fully via an onload event handler, as it can work height and width attributes alone if added to your <img /> tags. In the absence of height and width attributes, the plugin will use naturalWidth or naturalHeight with recent browses and width and height alone with legacy IE. For best performance, I would recommend adding height and width attributes that match correct natural pixel size and aspect ratio. The plugin does not override rendered image sizes in pixels but merely sets one axis to 100% and the other to auto. It needs the correct image aspect ratio to calculate top or left margin offsets. Only top or left margins are in set in pixels.
When integrated with other Javascript libraries such as picturefills.js it may be necessary to add an extra delay. Just specify milliseconds as the first parameter to wait for the other script to complete. Alternatively, if the other script provides a callback function, call imageFitCover() on completion.
$('picture').imageFitCover(200); /* will apply offsets after 0.2 seconds */