class: center, middle # **Responsive Images:** Lessons Learned .small[[CASCADE BOS meet up @Bocoup](http://www.meetup.com/cascadebos/events/225309830/) | September 22, 2015] by Daigo Fujiwara | [daigofuji.github.io/responsive-images](http://daigofuji.github.io/responsive-images/#1) --- class: center, middle ## Daigo Fujiwara Front-end Web Developer, Harvard Business Review Follow along: [daigofuji.github.io/responsive-images](http://daigofuji.github.io/responsive-images/#1) --- class: center, middle, inverse # Responsive Web Design Redesign of hbr.org in November 2014 --- class: center, middle, big-img, gray  --- class: center, middle ## Not a new idea --- class: center, middle, big-img, white [](http://alistapart.com/article/responsive-web-design) --- class: center, middle Media Query ```css @media (min-width:500px) { ... } ``` Flexible Images ```css img { max-width: 100%; } ``` and Fluid Grids --- class: center, middle # **PREMIUM** --- class: center, middle (What we thought) #Premium
=
**High Resolution** --- class: center, middle, big-img, gray  --- class: center, middle, big-img, white  --- class: center, middle, big-img, gray  --- class: center, middle, big-img, gray  --- class: center, middle, big-img, white  .small["bite off more than one can chew"] --- class: center, middle #Premium
=
**RIGHT Resolution** --- class: center, middle, inverse # Tools and references --- class: center, middle, white  [WebpageTest.org](http://www.webpagetest.org/) --- class: center, middle, white  [Google Page Insights](https://developers.google.com/speed/pagespeed/insights/) --- class: center, middle, white  [**Too Much Images**](https://github.com/addyosmani/tmi) (tmi for short) by [Addy Osmani](https://addyosmani.com/) --- class: center, middle, white  [**HTTP Archive's Stats**](http://httparchive.org/interesting.php) --- class: center, middle, white  [**BrowserDiet.com**](http://browserdiet.com/en/) Community-driven performance guide --- class: center, middle, inverse # Responsive Images --- class: center, middle, white [](http://responsiveimages.org/) RICG (Responsive Issues Community Group) and Mat "Wilto" Marquis to the rescue.  --- class: middle ### Examples - media query - `srcset` attribute - `srcset` and `sizes` attributes - `picture` tag --- class: center, middle, inverse # Media Query ```css @media (min-width:500px) { ... } ``` --- class: center, middle, white  --- class: middle ### Demo - [examples/mediaquery.html](http://daigofuji.github.io/responsive-images/examples/mediaquery.html) - [bit.ly/1Kxiflb](http://bit.ly/1Kxiflb) - [Test using ish](http://bradfrost.com/demo/ish/?url=http%3A%2F%2Fdaigofuji.github.io%2Fresponsive-images%2Fexamples%2Fmediaquery.html) - [view source on github](https://github.com/daigofuji/responsive-images/blob/gh-pages/examples/mediaquery.html) --- class: center, middle, inverse # srcset ```html
``` --- class: center, middle, white  --- class: center, middle ```html
``` ### Web Hypertext Application Technology Working Group’s (WHATWG) [Living Standard](https://html.spec.whatwg.org/multipage/embedded-content.html) --- class: middle ```html
``` An `img` element represents an image. The image referenced by the `src` and `srcset` attributes is the content to be embedded. The `srcset` attribute details the image candidates, from which the User Agent can select the most suitable image. --- class: middle Example **Retina Display image swap** ```html
``` - If your browser does not support srcset, you will see gray (default) 100x100 - If srcset is supported but is not retina, red 100x100 - If srcset is supported and is retina, blue 200x200 --- class: middle ### Demo - [examples/srcset.html](http://daigofuji.github.io/responsive-images/examples/srcset.html) - [bit.ly/1FrQYU9](http://bit.ly/1FrQYU9) - [view source on github](https://github.com/daigofuji/responsive-images/blob/gh-pages/examples/srcset.html) What to expect: - If your browser does not support srcset, you will see gray (default) 100x100 - If srcset is supported but is not retina, red 100x100 - If srcset is supported and is retina, blue 200x200 --- class: center, middle `2x` in srcset is a **descriptor**. Each image candidate string inside `srcset` must have at least one of the descriptors. You can also use w-descriptor like `200w` ("this image is 200 pixel wide") You can omit 1x in the example, too. Like: ```html
``` --- class: center, middle, inverse # srcset and sizes ```html
``` --- class: middle The `sizes` attribute can be used to specify additional context about the intended embed/render size of the image relative to the viewport width. > WHATWG's [Living Standard](https://html.spec.whatwg.org/multipage/embedded-content.html) --- class: center, middle, gray Great read: [Srcset and sizes](https://ericportis.com/posts/2014/srcset-sizes/) by Eric Portis  --- class: center, middle, gray  .small[from [Srcset and sizes](https://ericportis.com/posts/2014/srcset-sizes/) by Eric Portis] --- class: center, middle ### Example **HBR Article Page** Image is 100% of the viewport width (100vw) up to 1024px (Media query breakpoint). Then it is 83% of the viewport width (10 out of 12 column) ```
``` --- Demo - [examples/srcset-sizes.html](http://daigofuji.github.io/responsive-images/examples/srcset-sizes.html) - [bit.ly/1PoYgbJ](http://bit.ly/1PoYgbJ) - [Test using ish](http://bradfrost.com/demo/ish/?url=http%3A%2F%2Fdaigofuji.github.io%2Fresponsive-images%2Fexamples%2Fsrcset-sizes.html) - [view source on github](https://github.com/daigofuji/responsive-images/blob/gh-pages/examples/srcset-sizes.html) --- class: center, middle, inverse # Picture tag --- class: middle ```html
``` --- class: middle If the parent is a `picture` element, then any previous sibling source element’s `srcset` attributes may also determine the embedded content. > WHATWG's [Living Standard](https://html.spec.whatwg.org/multipage/embedded-content.html) --- class: center, middle, white  --- class: center, middle ### Use `picture` _only_ if you're doing art direction — otherwise, `srcset/sizes` are the way to go. > -- Wilto --- class: center, middle ## Fallback Browsers that do not support/understand `srcset` will just continue to use the `src` element, and those browsers that do support `srcset` can use its value instead. --- class: center, middle, white [**Picturefill** Polyfill](https://scottjehl.github.io/picturefill/) by Scott Jehl  --- class: center, middle Last but not least... ## What about images? --- class: center, middle, inverse # Tools for **automation** --- class: center, middle, white [RICG Responsive images plugin](https://wordpress.org/plugins/ricg-responsive-images/) for WordPress by Tim Evko et al  --- class: center, middle, white Sizes of images created using Responsive Images plugin .small[(Advanced Compression Option turned off)]  --- class: center, middle, white [Grunt Responsive Images](http://www.andismith.com/grunt-responsive-images/) by Andi Smith  --- class: center, middle, white Sizes of images created using Grunt Responsive Images  This [how to by Addy Osmani](http://addyosmani.com/blog/generate-multi-resolution-images-for-srcset-with-grunt/) was helpful --- class: middle, center **Thank you!**  daigo@daigofujiwara.com [
@daigofuji](https://github.com/daigofuji) [
@daigofuji](https://twitter.com/DaigoFuji) 