Git Product home page Git Product logo

object-fit-images's Introduction

object-fit-images

Polyfill object-fit and object-position on images on IE9, IE10, IE11, Edge, Safari, ...

gzipped size Travis build status npm version

This adds support for object-fit and object-position to IEdgeΒ 9-13, AndroidΒ <Β 5, SafariΒ <Β 10 and skips browsers that already support them.

Take a look at the demo.

Main features

  • Fast and lightweight
  • No additional elements are created or necessary
  • Once set, position is taken care by the browser
  • srcset support
  • You can still access/change src and srcset properties and attributes: img.src = 'other-image.jpg'

Comparison table with alternative solutions

Support

              | 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) | [jonathantneal<br>/fitie](https://github.com/jonathantneal/fitie)

:--- | :--- | :--- | :--- | :--- Browsers | IEdge 9-14, Android<5, Safari<10 | <- Same | "All browsers" | IE 8-11 Tags | img | image video picture | img | img video cover/contain | πŸ’š | πŸ’š | πŸ’š | πŸ’š fill | πŸ’š | πŸ’š | πŸ’š | πŸ’š none | πŸ’š | πŸ’š | πŸ’š | πŸ’” scale-down | πŸ’š using {watchMQ:true} | πŸ’š | πŸ’” | πŸ’” object-position | πŸ’š | πŸ’š | πŸ’” | πŸ’” srcset support | πŸ’š Native or picturefill notes | πŸ’š | πŸ’” | πŸ’” Extra elements | πŸ’š No | πŸ’” Yes | πŸ’” Yes | πŸ’” Yes Settings | πŸ’š via CSS | πŸ’” via HTML | πŸ’” via HTML | πŸ’” via HTML

Usage

You will need 3 things

  1. one or more <img> elements with src or srcset

    <img class='your-favorite-image' src='image.jpg'>
  2. CSS defining object-fit and a special font-family property to allow IE to read the correct value

    .your-favorite-image {
    	object-fit: contain;
    	font-family: 'object-fit: contain;'
    }

    or, if you also need object-position

    .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 or the SCSS/SASS/Less mixins.

    If you set the font-family via javascript (which must be followed by calling objectFitImages()), make sure to include the quotes in the property.

  3. the activation call before </body>, or on DOM ready

    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:

    // pass a selector
    objectFitImages('img.some-image');
    // an array/NodeList
    var someImages = document.querySelectorAll('img.some-image');
    objectFitImages(someImages);
    // a single element
    var oneImage = document.querySelector('img.some-image');
    objectFitImages(oneImage);
    // or with jQuery
    var $someImages = $('img.some-image');
    objectFitImages($someImages);

    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.

Apply on resize

You don't need to re-apply it on resize, unless:

In one of those cases, use the watchMQ option:

objectFitImages('img.some-image', {watchMQ: true});
// or objectFitImages(null, {watchMQ: true}); // for the auto mode

Install

npm install --save object-fit-images
var objectFitImages = require('object-fit-images');

If you don't use browserify/webpack, include this instead:

<script src="dist/ofi.min.js"></script>

or from the unpkg.com CDN:

<script src="https://unpkg.com/object-fit-images@VERSION/dist/ofi.min.js"></script>

Instead of @VERSION you should specify exact version like @1.1.1. The latest version is

API

objectFitImages([images, [options]])

parameter description
images Type: string, element, array, NodeList, null
Default: null

        The images to fix. More info in the <a href="#usage">Usage</a> section 

    </td>
</tr>
<tr>
    <th><code>options</code></th>
    <td>
        
        Type: <code>object</code><br>
        Default: <code>{}</code><br>
        Example: <code>{watchMQ:true}</code><br><br>
        
        <table>
            <tr>
                <th><code>watchMQ</code></th>
                <td>
                    Type: <code>boolean</code><br>
                    Default: <code>false</code>

                    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>
                </td>
            </tr>
        </table>
    </td>
</tr>

License

MIT Β© Federico Brigante

object-fit-images's People

Contributors

amaldare93 avatar bfred-it avatar innocentiv avatar patricknelson avatar paulradzkov avatar skwakman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.