npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-fit-cover

v1.0.4

Published

A polyfill for the background-image cover effect combined with responsive image behaviour with the `<img>` or `<picture>` element.

Downloads

4,105

Readme

ObjectFitCover

A polyfill for the background-image cover effect combined with responsive image behaviour with the <img> or <picture> element.

npm version

Intro

Covering an area with an image can be achieved with background-size: cover, but adding responsive image behaviour can be a pain with custom data-attributes or image-source references in CSS.

The cover effect together with responsive image behaviour can be achieved native in modern browsers (no javascript needed!) with the CSS property object-fit (support) combined with the <img> (with srcset) (support) or <picture> (support) element. ObjectFitCover adds a background-image fallback for browsers not supporting CSS property object-fit: cover. This polyfill works together with Picturefill, a polyfill for responsive image behaviour with <picture>, srcset, sizes and more.

Usage

1. Include ObjectFitCover Javascript

Preferably inline (0.6kb, 0.4kb gzipped) in the head section before the stylesheets. This to avoid flashes and have the best render performance. Copy the source from here

<head>
    <script>
      ..objectfitcover.min.js inline..
    </script>

    ...
</head>

2. Include ObjectFitCover CSS

Load it after the ObjectFitCover Javascript, to avoid flashes on the page. Preferably combined with your existing CSS.

<head>
    ...

    <link rel="stylesheet" href="objectfitcover.min.css" />
</head>

3. Include your images & CSS

.example {
    width: 100px;
    height: 300px;
}
<div class="object-fit-container cover example">
    <img src="fallback.jpg" srcset="[email protected] 1x, [email protected] 2x" />
</div>

See the examples

4. Include Picturefill

Preferably async, to avoid making it renderblocking.

<script src="picturefill.min.js" async></script>

Note: if you don't need picturefill, set window.picturefill = {} to make this polyfill work.

Browser support

object-fit: cover is supported in below browsers, so ObjectFitCover is not needed for these:

  • Chrome > 30
  • Safari > 7
  • Firefox > 35
  • Android Browser > 4.4
  • Opera > 18

ObjectFitCover works in:

  • Chrome
  • Safari > 3
  • Internet Explorer > 8
  • Edge
  • Firefox > 4
  • Android Browser > 2.3
  • Opera > 10.1

ObjectFitCover()

You can call objectFitCover() anytime to fix the image-cover in non-supporting browsers. For example with lazyloaded pictures. Option:

  • elements: An array of elements (parent of the actual img) that need to be fixed. The default is all .object-fit-container elements.

    objectFitCover({
      elements: [ document.getElementsByClassname('example') ]
    });

What is not (yet) supported?

  • CSS property background-attachment: fixed
  • CSS property object-position
  • Video's

What about?

  • Supporting all possible values of the CSS property object-fit? This polyfill can be small in size because of not supporting all possible values. The cover-effect is an often used effect on sites. It's not needed for those cases to load a complete polyfill for all possible (unused) values.

  • Supporting object-fit: contain? You can add this CSS to make it work as well:

    .object-fit-container.contain {
        background-repeat: no-repeat;
        background-size: contain;
    }
    .object-fit-container.contain img {
        object-fit: contain;
    }
  • Supporting browsers with Javascript disabled? Javascript disabled is not an issue in browsers that support object-fit: cover and <img> (with srcset) or <picture>. The image is scaled to fit the object-fitcontainer in browsers that don't support object-fit: cover. So it's not an ideal visual appearance, but it still got the image. Make sure if you use srcset to provide a fallback image in the src attribute for browsers that don't support srcset or <picture>. More info on the PictureFill website, point JS-Disabled Browsers only see alt text.