picture-background
v1.0.3
Published
Converts picture tags into background-image CSS styles.
Downloads
2
Readme
picture-background
Converts picture tags into background-image CSS styles. Updates with window resize events, just like <picture>
does by default.
Motivation
One solution to help fulfill three needs:
- Background cover styling.
- Conditional image loading.
- Meaningful and accessible HTML markup.
Requirements
- Browser support for matchMedia.
- jQuery (vanilla support planned).
Installation Options
- Download .zip from Github.
- NPM.
Usage
HTML:
<div id="parentOfPicture">
<picture>
<source srcset="800x600.png" media="(min-width: 600px)" />
<img alt="placeholder" src="400x300.png" />
</picture>
</div>
jQuery:
$('#parentOfPicture').pictureBackground();
Output if window width < 600px:
<div id="parentOfPicture" style="background-image: url("400x300.png");">
<picture>
<source srcset="800x600.png" media="(min-width: 600px)" />
<img alt="placeholder" src="400x300.png" />
</picture>
</div>
Output otherwise:
<div id="parentOfPicture" style="background-image: url("800x600.png");">
<picture>
<source srcset="800x600.png" media="(min-width: 600px)" />
<img alt="placeholder" src="400x300.png" />
</picture>
</div>