1 of 14

Responsive Images

Progressive Web Apps

Progressive Web Apps

2 of 14

Images are important

Progressive Web Apps

3 of 14

What are responsive images?

A method for providing the browser with multiple image sources depending on display density, size of the image element in the page, or any number of other factors. �-- Jason Grigsby

Progressive Web Apps

4 of 14

What can we do with responsive images?

  • Resolution
  • Display size
  • Format support
  • Art direction

Live Demo

Progressive Web Apps

5 of 14

General principles for performance

  1. Avoid images wherever possible
  2. Use vector formats where possible: SVG or icon fonts
  3. Use the lowest possible resolution and quality
  4. Use the right format for the image type: WebP, PNG, JPEG

Progressive Web Apps

6 of 14

Responsive image elements & attributes

  • <picture>
  • <source>
  • srcset
  • sizes

Progressive Web Apps

7 of 14

The <picture> element

<picture>

<source srcset="kittens.webp" type="image/webp">

<source srcset="kittens.jpg" type="image/jpeg">

<img src="kittens.jpg" alt="Two grey tabby kittens">

</picture>

Progressive Web Apps

8 of 14

Art direction

<picture>

<source media="(min-width: 650px)" srcset="kitten-lg.png">

<source media="(min-width: 465px)" srcset="kitten-md.png">

<img src="kitten-small.png" alt="Cute kitten">

</picture>

Live Demo

Progressive Web Apps

9 of 14

The srcset attribute

<img src="wallaby_1x.jpg" alt="Wallaby"� srcset="wallaby_1x.jpg 1x, wallaby_2x.jpg 2x" />

Progressive Web Apps

10 of 14

The width descriptor

<img src="small.jpg" alt="Wallaby"� srcset="small.jpg 500w, med.jpg 1000w,

large.jpg 1500w">

Progressive Web Apps

11 of 14

Responsive images: what size?

<img src="small.jpg" srcset="small.jpg 500w, medium.jpg 1000w, large.jpg 1500w" sizes="50vw" alt="Wallaby" />

Progressive Web Apps

12 of 14

All together

<picture>

<source media="(min-width: 1000px)" srcset="bird_large_1x.jpg 1x, bird_large_2x.jpg 2x">

<source media="(min-width: 500px)" srcset="bird_med_1x.jpg 1x, bird_med_2x.jpg 2x">

<img src="bird_small.jpg" alt="A colorful bird">

</picture>

Progressive Web Apps

13 of 14

Resources

Resources

Progressive Web Apps

14 of 14

Lab Overview

  • Take a non-responsive site containing images and make it responsive
  • Use the srcset attribute and width descriptors
  • Use the sizes attribute
  • Use media queries to resize images based on window size
  • Use the picture and source elements for art direction

Progressive Web Apps