Skip to content

Instantly share code, notes, and snippets.

@JayHoltslander
Created September 27, 2017 21:22
Show Gist options
  • Save JayHoltslander/b8f973b49f7baa19134a297191149851 to your computer and use it in GitHub Desktop.
Save JayHoltslander/b8f973b49f7baa19134a297191149851 to your computer and use it in GitHub Desktop.
<picture> Element for responsive images.
<!--
SOURCE REFERENCES
See:
* https://www.html5rocks.com/en/tutorials/responsive/picture-element/
* https://cloudfour.com/thinks/dont-use-picture-most-of-the-time/
-->
<style>
.responsive-image {
height: 100%;
object-fit: cover;
width: 100%
}
</style>
<picture class="responsive-image">
<source
media="(min-width: 1600px)"
srcset="img/puppy-1600.jpg,
img/[email protected] 2x,
img/[email protected] 3x,
img/[email protected] 4x,">
<source
media="(min-width: 1200px)"
srcset="img/puppy-1200.jpg,
img/[email protected] 2x,
img/[email protected] 3x,
img/[email protected] 4x,">
<source
media="(min-width: 992px)"
srcset="img/puppy-992.jpg,
img/[email protected] 2x,
img/[email protected] 3x,
img/[email protected] 4x,">
<source
media="(min-width: 768px)"
srcset="img/puppy-768.jpg,
img/[email protected] 2x,
img/[email protected] 3x,
img/[email protected] 4x,">
<source
media="(min-width: 576px)"
srcset="img/puppy-576.jpg,
img/[email protected] 2x,
img/[email protected] 3x,
img/[email protected] 4x,">
<!-- Fallback <img> element (REQUIRED FOR <picture> ELEMENT.) -->
<img src="img/puppy-576.jpg"
srcset="img/puppy-576.jpg 576w,
img/puppy-768.jpg 768w,
img/puppy-992.jpg 992w,
img/puppy-1200.jpg 1200w,
img/puppy-1600.jpg 1600w"
alt="Cute little dog">
</picture>
@JayHoltslander
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment