Convert Figma logo to code with AI

scottjehl logopicturefill

A responsive image polyfill for <picture>, srcset, sizes, and more

9,893
1,075
9,893
1

Top Related Projects

17,454

High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.

🗻 Polyfill object-fit/object-position on <img>: IE9, IE10, IE11, Edge, Safari, ...

JavaScript, SCSS, Sass, Less, and Stylus helpers for rendering high-resolution image variants

Quick Overview

Picturefill is a responsive image polyfill that enables the use of the <picture> element and associated features in browsers that don't natively support them. It allows developers to deliver optimized images for different screen sizes and resolutions, improving performance and user experience across devices.

Pros

  • Enhances cross-browser compatibility for responsive images
  • Improves page load times by serving appropriately sized images
  • Seamlessly falls back to standard <img> elements in supported browsers
  • Lightweight and easy to implement

Cons

  • Adds additional JavaScript overhead to the page
  • May not be necessary for projects targeting only modern browsers
  • Requires careful management of multiple image versions
  • Could potentially lead to increased complexity in image management workflows

Code Examples

  1. Basic usage of the <picture> element with Picturefill:
<picture>
    <source srcset="large.jpg" media="(min-width: 800px)">
    <source srcset="medium.jpg" media="(min-width: 400px)">
    <img src="small.jpg" alt="A responsive image">
</picture>
  1. Using srcset for resolution switching:
<img src="small.jpg"
     srcset="small.jpg 1x, medium.jpg 2x, large.jpg 3x"
     alt="Image with resolution options">
  1. Combining srcset and sizes attributes:
<img src="small.jpg"
     srcset="small.jpg 400w, medium.jpg 800w, large.jpg 1200w"
     sizes="(max-width: 400px) 100vw, (max-width: 800px) 50vw, 33vw"
     alt="Image with size and resolution options">

Getting Started

To use Picturefill in your project, follow these steps:

  1. Include the Picturefill script in your HTML:
<script src="https://cdnjs.cloudflare.com/ajax/libs/picturefill/3.0.3/picturefill.min.js"></script>
  1. Use the <picture> element or srcset and sizes attributes in your HTML as shown in the code examples above.

  2. Picturefill will automatically polyfill the responsive image features in browsers that don't support them natively.

Competitor Comparisons

17,454

High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

Pros of lazysizes

  • More comprehensive lazy loading solution, handling images, iframes, and scripts
  • Automatic size calculation for responsive images
  • Extensive plugin ecosystem for additional features

Cons of lazysizes

  • Larger file size due to more features
  • Steeper learning curve for advanced usage
  • May be overkill for simple image loading scenarios

Code Comparison

lazysizes:

<img data-src="image.jpg" class="lazyload" alt="Lazy loaded image">

picturefill:

<picture>
  <source srcset="image.webp" type="image/webp">
  <img src="image.jpg" alt="Responsive image">
</picture>

Key Differences

  • lazysizes focuses on lazy loading and performance optimization
  • picturefill primarily addresses responsive images and browser compatibility
  • lazysizes offers more features but requires additional configuration
  • picturefill is simpler to implement for basic responsive image needs

Use Cases

  • Choose lazysizes for complex lazy loading requirements and performance optimization
  • Opt for picturefill when primarily dealing with responsive images and browser support

Community and Maintenance

  • lazysizes: More active development, larger community, frequent updates
  • picturefill: Less frequent updates, smaller but dedicated community

LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.

Pros of vanilla-lazyload

  • Focuses specifically on lazy loading images and other media
  • Supports modern browsers and techniques like Intersection Observer
  • Lightweight and doesn't require any external dependencies

Cons of vanilla-lazyload

  • Doesn't provide responsive image capabilities like srcset and sizes attributes
  • May require more manual configuration for complex image loading scenarios

Code Comparison

vanilla-lazyload:

const lazyLoadInstance = new LazyLoad({
  elements_selector: ".lazy",
  threshold: 0,
  callback_enter: (element) => {
    console.log("Loaded:", element);
  }
});

Picturefill:

<picture>
  <source srcset="large.jpg" media="(min-width: 800px)">
  <source srcset="medium.jpg" media="(min-width: 400px)">
  <img src="small.jpg" alt="Image description">
</picture>

Summary

Vanilla-lazyload is a specialized library for lazy loading images and other media, offering modern browser support and lightweight implementation. It excels in performance optimization but may require additional work for responsive image scenarios.

Picturefill, on the other hand, focuses on providing responsive image capabilities and fallbacks for older browsers. It offers more robust support for complex image loading scenarios but may be overkill if lazy loading is the primary concern.

Choose vanilla-lazyload for efficient lazy loading in modern web applications, or Picturefill for comprehensive responsive image support across a wide range of browsers.

🗻 Polyfill object-fit/object-position on <img>: IE9, IE10, IE11, Edge, Safari, ...

Pros of object-fit-images

  • Focuses specifically on the object-fit and object-position CSS properties
  • Lightweight and easy to implement
  • Supports a wider range of browsers, including older versions of IE

Cons of object-fit-images

  • Limited to handling only object-fit and object-position
  • May require additional polyfills for full responsive image support
  • Less actively maintained compared to Picturefill

Code Comparison

Picturefill usage:

<picture>
  <source srcset="large.jpg" media="(min-width: 800px)">
  <img src="small.jpg" alt="Image description">
</picture>

object-fit-images usage:

<img src="image.jpg" style="object-fit: cover; width: 100%; height: 300px;">
<script>objectFitImages();</script>

Summary

Picturefill is a comprehensive responsive images polyfill that supports various image loading techniques, while object-fit-images focuses specifically on the object-fit and object-position properties. Picturefill offers more flexibility for responsive image solutions but may be overkill for simpler use cases. object-fit-images provides a lightweight alternative for specific CSS property support but may require additional polyfills for full responsive image functionality.

JavaScript, SCSS, Sass, Less, and Stylus helpers for rendering high-resolution image variants

Pros of Retinajs

  • Lightweight and simple to implement
  • Automatically detects and replaces images for high-DPI displays
  • Works with both inline <img> tags and CSS background images

Cons of Retinajs

  • Less flexible than Picturefill for complex responsive image scenarios
  • Requires specific naming conventions for high-resolution images
  • May not be as well-maintained or widely supported as Picturefill

Code Comparison

Picturefill usage:

<picture>
  <source srcset="large.jpg" media="(min-width: 800px)">
  <source srcset="medium.jpg" media="(min-width: 400px)">
  <img src="small.jpg" alt="A responsive image">
</picture>

Retinajs usage:

<img src="image.jpg" data-rjs="image@2x.jpg" alt="A retina image">

Retinajs focuses on simplicity and automatic detection of high-DPI displays, while Picturefill offers more control over responsive image loading based on various factors like viewport size. Picturefill uses the <picture> element and multiple <source> tags, allowing for more complex responsive scenarios. Retinajs, on the other hand, relies on a simpler data attribute approach for specifying high-resolution images.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Picturefill

A responsive image polyfill.

This project is archived and deprecated!

At the time, it helped us transition to responsive image HTML patterns until browsers supported them. Support and fallback strategies are now very good, and this project is no longer needed or recommended. Thanks everyone!`

build-status Join Slack channel picturefill on npm

Picturefill has three versions:

  • Version 1 mimics the Picture element pattern with span elements. It is no longer maintained.
  • Version 2 is a lightweight polyfill of the Picture element draft specification.
  • Version 3.0.3 is the current stable release.

Usage, Demos, Docs

To find out how to use Picturefill, visit the project site.

The gotchas

Be it browser issues, the responsive images specifications, or Picturefill itself, there are a couple gotchas you should be aware of when working with Picturefill:

  • Firefox 38 and 39 has some bugs [1] [2] [3] where images won't update on screen resize. These issues are addressed by Picturefill 3.0.0, and was fixed in Firefox 41.

  • Per the picture spec, using % isn't allowed in the sizes attribute. Using % will fallback to 100vw.

  • Trying to use the src attribute in a browser that doesn't support picture natively can result in a double download. To avoid this, don't use the src attribute on the img tag:

<picture>
    <source srcset="../img/sample.svg" media="(min-width: 768px)" />
    <img srcset="default.png" alt="Sample pic" />
</picture>
  • If you only want to have an image show up at certain sizes, and not show up at others, you will need to use a transparent placeholder gif:
<picture>
    <source srcset="../img/sample.svg" media="(min-width: 768px)" />
    <img srcset="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
        alt="Sample pic" />
</picture>

Contributing

For information on how to contribute code to Picturefill, check out CONTRIBUTING.md

Issues

If you find a bug in Picturefill, please add it to the issue tracker

Discussion

Picturefill discussion takes place via Slack. For an invitation, visit https://pf-slackin.herokuapp.com/

Support

There are currently no known unsupported browsers, provided that you use the markup patterns provided.

NPM DownloadsLast 30 Days