Giter Site home page Giter Site logo

picturefill's Introduction

Picturefill

A Responsive Images approach that you can use today, that mimics the proposed picture element using divs, for safety sake.

  • Author: Scott Jehl (c) 2012
  • License: MIT/GPLv3

Demo URL: http://scottjehl.github.com/picturefill/

Note: Picturefill works best in browsers that support CSS3 media queries. It includes (externally) the matchMedia polyfill which makes matchMedia work in media-query-supporting browsers that don't have matchMedia, or at least allows media types to be tested in most any browser. matchMedia and the matchMedia polyfill are not required for picturefill to work, but they are required to support the media attributes on picture source elements.

Size and delivery

Currently, picturefill.js compresses to around 498bytes (~0.5kb), after minify and gzip. To minify, you might try these online tools: [Uglify]:(http://marijnhaverbeke.nl/uglifyjs), [Yahoo Compressor]:(http://refresh-sf.com/yui/), or Closure Compiler. Serve with gzip compression.

Markup pattern and explanation

Mark up your responsive images like this. The data-media attribute on each div[data-src] element accepts and and all CSS3 media queries, such as min or max width, or even min-device-pixel-ratio for HD (retina) displays.

	<div data-picture data-alt="A giant stone face at The Bayon temple in Angkor Thom, Cambodia">
		<div data-src="small.jpg"></div>
		<div data-src="medium.jpg" data-media="(min-width: 400px)"></div>
		<div data-src="large.jpg" data-media="(min-width: 800px)"></div>
		<div data-src="extralarge.jpg" data-media="(min-width: 1000px)"></div>

		<!-- Fallback content for non-JS browsers. Same img src as the initial, unqualified source element. -->
		<noscript><img src="external/imgs/small.jpg" alt="A giant stone face at The Bayon temple in Angkor Thom, Cambodia"></noscript>
	</div>

Explained...

Notes on the markup above...

  • The div[data-picture] element's alt attribute is used as alternate text for the generated img element.
  • The div[data-picture] element can have any number of source elements. The above example may contain more than the average situation would call for.
  • Each div[data-src] element must have a data-src attribute specifying the image path.
  • It's generally a good idea to include one source element with no media qualifier, so it'll apply everywhere.
  • Each data-src element can have an optional media attribute to make it apply in different media settings. Both media types and queries can be used, like any media attribute, but support for media queries depends on the browser (unsupporting browsers fail silently).
  • The matchMedia polyfill (included in /external) is necessary to support the media attribute across browsers, even in browsers that support media queries, although it is becoming more widely supported in new browsers.
  • The noscript element wraps the fallback image for non-JavaScript environments, and including this wrapper prevents browsers from fetching the fallback image during page load (causing unnecessary overhead). Generally, it's a good idea to reference a small image here, as it's likely to be loaded in older/underpowered mobile devices.

Support

Picturefill supports a broad range of browsers and devices (there are currently no known unsupported browsers), provided that you stick with the markup conventions provided.

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.