Working with Responsive Images

Chrome for Developers · Advanced ·🌐 Frontend Engineering ·9y ago

Key Takeaways

Optimizes images for web performance and responsiveness using various techniques

Full Transcript

[Music] On average, images consume by far the most bytes on most web pages. We can do better. Responsive images are a huge topic. So, I'm just going to give a whistle stop tour. First though, some general principles for using images on the web. The goal here is to increase performance and reduce data cost. Now the first step in that is to avoid images wherever possible, text, CSS and so on. Use vector formats where possible, SVG. You could use icon fonts, but there are good reasons not to use them. I I won't go into that here. Anyway, always use the lowest possible resolution and quality image. Use the right format for the image type, WEBP, PNG, JPEG, and so on. Speaking of image formats, there are also a few use cases for inline images with data URIs. When you're using responsive design with PWAs, be especially careful about aspect ratio with images width and height. If you specify height as well as width, which may be good for performance, make sure to get it right. You can incorporate lossless compression tools in your workflow. And remember to use online tools and to add testing to your workflow. Page speed insights, web page test, and many other free tools are available. It's a great time to be doing responsive image design on the web. You have really powerful elements and attributes for sizing, resolution, and to reduce data cost. This chart is for picture and source set. As you can see, there's great support across modern browsers. There is also a picture polyfill available for older browsers. So what makes responsive images responsive? Firstly, the resolution of the screen. You need to serve images with more pixels to devices with higher resolution screens and vice versa. Secondly, image display size. This is the size at which the image is actually displayed and that can vary significantly even on the same device. 100% width in a maximized browser window on a big monitor is going to need an image with much larger dimensions than if the image is displayed at say 30% width in a smaller window. Think about format support. Different browsers support different formats. For example, you might want to serve WEBP images to browsers that support WEBP and provide a fallback to browsers that don't. Art direction. Now, you might even want to serve a different version of an image depending on display size, just like we mentioned earlier. Let's take a look at the source set attribute. This example shows what are called pixel density descriptors. The source set attribute tells the browser which image to choose depending on the pixel density of the display. This might be 1x for a desktop monitor, 2x for a laptop screen, or even more on a phone, for example. Note that fallback to source. So this will work on browsers that don't support source set. Also note the use of the alt attribute to improve accessibility. Now unfortunately we're not going to cover a lot of accessibility in this course, but the alt attribute is fundamental for those using screen readers or for users with images turned off. For a browser, there's a catch 22 when it comes to choosing which image to download. The browser needs to know the dimensions of each image, but it can't know that without downloading each image to check. Enter the Wunit. The Wunit tells the browser the width of each image in pixels, thereby enabling the browser to choose the right image to retrieve depending on the screen pixel density and the viewport size. Just to be clear, the source set attribute is giving the browser information about the image width. It does not specify the size to display the image. You still need CSS for that. Now, note that we can't specify both a pixel density and width descriptor in the same source set. It must be all pixel densities or all width descriptors. You can go further than just telling the browser the image width. The sizes attribute describes the display size of the image element. The sizes attribute here is telling the browser this walabe will always be displayed at 50% of viewport width. Given the image files available, please choose appropriately. This enables the browser to select the smallest possible image size to download. The picture and source elements enable us to provide alternative sources for the same resource. the browser will stop and load the first source element that it understands and load that image. If the browser can't read the files specified in the source elements or if the browser doesn't support the picture and source elements, the default image will be loaded from the source. The source elements can include different file formats. In this example, we use WEBP first and then JPEG as a fallback and provide the same JPEG file as the default image in the image element. You can also use picture for art direction. That means choosing different images for different display sizes. The source element can take an optional media attribute where you can specify a media query. When that media query is triggered, then the image in the associated source set attribute will be loaded. Only one image will be loaded at a time. Kitten large when the window width is 650 pixels or larger. Kitten medium when the window width is at least 465 but no more than 650 pixels and kitten small when the window is less than 465 pixels wide. If the browser doesn't support picture and source then only the image tag will be used and you will see the small kitten image. The live demo link will take you to an example of this code for you to test in practice. Okay, hold on to your hat. Here's everything put together where we've combined media queries and source set to specify images for smaller and larger viewports with different images for different pixel densities. Don't forget there are tools for making this part of your workflow. So you don't need to manually code all this. There are lots of resources to help you get started in the materials that go with this video. The lab exercises will help you get started with responsive elements and attributes.

Original Description

There are two major issues to consider when working with images on the web: performance and responsiveness. This video describes a set of techniques to make your images load as quickly as possible and work well in any form factor. Codelabs, slides and more information are available from the Progressive Web Apps Training home page: https://developers.google.com/web/ilt/pwa
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Chrome for Developers · Chrome for Developers · 0 of 60

← Previous Next →
1 Polymer Performance Patterns (The Polymer Summit 2015)
Polymer Performance Patterns (The Polymer Summit 2015)
Chrome for Developers
2 Polymer Power Tools (The Polymer Summit 2015)
Polymer Power Tools (The Polymer Summit 2015)
Chrome for Developers
3 Chrome Dev Summit 2014 – Chrome Case Studies
Chrome Dev Summit 2014 – Chrome Case Studies
Chrome for Developers
4 Web Directions Code 2015 round up
Web Directions Code 2015 round up
Chrome for Developers
5 Maintainable Code - HTTP203
Maintainable Code - HTTP203
Chrome for Developers
6 iron-ajax… wat?! -- Polycasts #26
iron-ajax… wat?! -- Polycasts #26
Chrome for Developers
7 The Guardian - Supercharged
The Guardian - Supercharged
Chrome for Developers
8 ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
ES2015 (next version of JavaScript), Totally Tooling Tips (S2 Ep1)
Chrome for Developers
9 #AskPolymer: Rob answers all the questions ever -- Polycasts #27
#AskPolymer: Rob answers all the questions ever -- Polycasts #27
Chrome for Developers
10 The Future of JavaScript - HTTP203
The Future of JavaScript - HTTP203
Chrome for Developers
11 Data Binding 101 -- Polycasts #28
Data Binding 101 -- Polycasts #28
Chrome for Developers
12 The Guardian part 2 - Supercharged
The Guardian part 2 - Supercharged
Chrome for Developers
13 The Future of Web Audio: with Chris Wilson and Chris Lowis
The Future of Web Audio: with Chris Wilson and Chris Lowis
Chrome for Developers
14 Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome 46: New motion-path animations, client hints and service worker improvements
Chrome for Developers
15 Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Sublime Snippets, Totally Tooling Tips (S2 Ep2)
Chrome for Developers
16 #AskPolymer: How do you make the show? -- Polycasts #29
#AskPolymer: How do you make the show? -- Polycasts #29
Chrome for Developers
17 Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Critical Path CSS, Totally Tooling Tips (S2 Mini Tip #1)
Chrome for Developers
18 Binding to Objects -- Polycasts #30
Binding to Objects -- Polycasts #30
Chrome for Developers
19 Player FM - Supercharged
Player FM - Supercharged
Chrome for Developers
20 Where’s the Designer? #AskPolymer -- Polycasts #31
Where’s the Designer? #AskPolymer -- Polycasts #31
Chrome for Developers
21 Jake Beats Wikipedia - HTTP203
Jake Beats Wikipedia - HTTP203
Chrome for Developers
22 Supercharged Observers! -- Polycasts #32
Supercharged Observers! -- Polycasts #32
Chrome for Developers
23 Jai's Web blog - Supercharged
Jai's Web blog - Supercharged
Chrome for Developers
24 Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Windows Command-line Tooling, Totally Tooling Tips (S2, Ep4)
Chrome for Developers
25 What about internationalization? #AskPolymer -- Polycasts #33
What about internationalization? #AskPolymer -- Polycasts #33
Chrome for Developers
26 Developing for Billions (Chrome Dev Summit 2015)
Developing for Billions (Chrome Dev Summit 2015)
Chrome for Developers
27 Google+ Performance Improvement Comparison
Google+ Performance Improvement Comparison
Chrome for Developers
28 Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Deploying HTTPS: The Green Lock and Beyond (Chrome Dev Summit 2015)
Chrome for Developers
29 Progressive Web Apps (Chrome Dev Summit 2015)
Progressive Web Apps (Chrome Dev Summit 2015)
Chrome for Developers
30 Instant Loading with Service Workers (Chrome Dev Summit 2015)
Instant Loading with Service Workers (Chrome Dev Summit 2015)
Chrome for Developers
31 Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Increase Engagement with Web Push Notifications (Chrome Dev Summit 2015)
Chrome for Developers
32 Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Engaging with the Real World: Web Bluetooth and Physical Web (Chrome Dev Summit 2015)
Chrome for Developers
33 Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Asking for Permission: respectful, opinionated UI (Chrome Dev Summit 2015)
Chrome for Developers
34 Polymer - State of the Union (Chrome Dev Summit 2015)
Polymer - State of the Union (Chrome Dev Summit 2015)
Chrome for Developers
35 Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Building Progressive Web Apps with Polymer (Chrome Dev Summit 2015)
Chrome for Developers
36 Introduction to RAIL (Chrome Dev Summit 2015)
Introduction to RAIL (Chrome Dev Summit 2015)
Chrome for Developers
37 DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
DevTools in 2015: Authoring to the max (Chrome Dev Summit 2015)
Chrome for Developers
38 RAIL in the real world (Chrome Dev Summit 2015)
RAIL in the real world (Chrome Dev Summit 2015)
Chrome for Developers
39 #ChromeDevSummit talks are up - W00T! -- Polycast #34
#ChromeDevSummit talks are up - W00T! -- Polycast #34
Chrome for Developers
40 V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
V8 Performance from the Driver's Seat (Chrome Dev Summit 2015)
Chrome for Developers
41 Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Quantify and improve real-world RAIL (Chrome Dev Summit 2015)
Chrome for Developers
42 Owning your performance: RAIL (Chrome Dev Summit 2015)
Owning your performance: RAIL (Chrome Dev Summit 2015)
Chrome for Developers
43 HTTP/2 101 (Chrome Dev Summit 2015)
HTTP/2 101 (Chrome Dev Summit 2015)
Chrome for Developers
44 Leadership Panel (Chrome Dev Summit 2015)
Leadership Panel (Chrome Dev Summit 2015)
Chrome for Developers
45 Build Processes, Totally Tooling Tips (S2, Ep 5)
Build Processes, Totally Tooling Tips (S2, Ep 5)
Chrome for Developers
46 Accessibility (Chrome Dev Summit 2015)
Accessibility (Chrome Dev Summit 2015)
Chrome for Developers
47 Binding to Arrays -- Polycasts #35
Binding to Arrays -- Polycasts #35
Chrome for Developers
48 HTTP2 - HTTP203
HTTP2 - HTTP203
Chrome for Developers
49 Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome 47: Splash Screens, requestIdleCallback and better desktop notifications (New in Chrome)
Chrome for Developers
50 Call For Submissions - Supercharged
Call For Submissions - Supercharged
Chrome for Developers
51 Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Cross Device Testing, Totally Tooling Tips (S2 Ep6)
Chrome for Developers
52 Testing AJAX with Web Component Tester -- Polycasts #37
Testing AJAX with Web Component Tester -- Polycasts #37
Chrome for Developers
53 Slack: Extended Xmas Special - Supercharged
Slack: Extended Xmas Special - Supercharged
Chrome for Developers
54 Browser testing with Travis & Sauce Labs -- Polycasts #38
Browser testing with Travis & Sauce Labs -- Polycasts #38
Chrome for Developers
55 Optimize for production with Vulcanize -- Polycasts #39
Optimize for production with Vulcanize -- Polycasts #39
Chrome for Developers
56 Highlights from Chrome Dev Summit 2015
Highlights from Chrome Dev Summit 2015
Chrome for Developers
57 Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome 48: Custom buttons in notifications, DevTools Security panel, and Presentation mode
Chrome for Developers
58 Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Crisper: Protecting your Polymer app with CSP -- Polycasts #40
Chrome for Developers
59 How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
How do I use Sass with Polymer? #AskPolymer -- Polycasts #41
Chrome for Developers
60 Colors – DevTools Tonight #0 (Pilot)
Colors – DevTools Tonight #0 (Pilot)
Chrome for Developers

Related Reads

📰
Angular vs React: Key Differences, Pros & Cons Compared
Learn the key differences between Angular and React to decide which framework suits your project needs, considering factors like team size, project complexity, and desired level of flexibility.
Dev.to · Elsie Rainee
📰
Next.js Web App Development: Complete Guide for Fast, Scalable Applications (2026)
Learn how to build fast and scalable web applications using Next.js, a popular React framework, to improve user experience and conversion rates
Dev.to · Avanexa Technologies
📰
Next.js Quietly Fixed the Prefetch Problem Nobody Wanted to Talk About
Next.js fixed the prefetch problem, learn how to leverage this fix for better performance
Medium · JavaScript
📰
A Fast Request, a Fast Parse and a Slow Page
Optimize each phase of webpage loading to achieve target performance
Medium · JavaScript
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →