<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yuval Papish</title>
    <description>The latest articles on DEV Community by Yuval Papish (@yuvalpapish).</description>
    <link>https://dev.to/yuvalpapish</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1262361%2F7addb0c9-e5eb-4746-a1ba-bb0290af3537.jpg</url>
      <title>DEV Community: Yuval Papish</title>
      <link>https://dev.to/yuvalpapish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yuvalpapish"/>
    <language>en</language>
    <item>
      <title>Mastering Responsive HTML Images</title>
      <dc:creator>Yuval Papish</dc:creator>
      <pubDate>Mon, 05 Feb 2024 11:37:21 +0000</pubDate>
      <link>https://dev.to/yuvalpapish/mastering-responsive-html-images-1a9o</link>
      <guid>https://dev.to/yuvalpapish/mastering-responsive-html-images-1a9o</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;&lt;span&gt; &lt;/span&gt;As websites strive for optimal performance and adaptability across various devices, understanding the intricacies of handling images becomes paramount. There is an abundance of resources available on this topic, but the implementation of this feature can be more intricate than it seems. This technical blog post aims to explore both the challenges and solutions to achieve robust image responsiveness.&lt;/p&gt;

&lt;p&gt;&lt;span&gt; &lt;/span&gt;Responsive HTML images offer a dual promise - performance and robustness. Getting a resized image directly from HTML amps up performance, while the flexibility to employ lazy/eager loading and fetch priority mechanisms mirrors the traditional &lt;em&gt;img&lt;/em&gt; tag.&lt;/p&gt;

&lt;p&gt;A refresher on the pros and cons of responsive HTML images, before delving into the ‘how’:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pros&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Performance: Easily obtain a resized image directly from HTML, which is very useful to keep your LCP (Largest Contentful Paint) tight.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Robustness: Leverage browser lazy/eager loading and fetch priority mechanisms that hint the browser on your optimal assets loading sequence, similar to standard &lt;em&gt;img&lt;/em&gt; tags.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Cons&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Complexity: Here lies the crux of our journey, the reason for this post.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fragility: Changes in page design may disrupt responsive images, requiring adjustments each time your website evolves. I'll address this challenge in a successive post.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Deep Dive into Responsive HTML
&lt;/h2&gt;

&lt;p&gt;Let's explore a common example of the &lt;em&gt;img&lt;/em&gt; tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img style="max-width:100%" srcset="w_500/superman.jpg 500w, w_1000/superman.jpg 1000w"
   sizes="(max-width: 600px) 500px, 1000px" src="w_1000/superman.jpg"
   alt="It's a Bird... It's a Plane... It's Superman" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: 'w' denotes intrinsic pixel width in &lt;em&gt;srcset&lt;/em&gt;; all other units (or unitless) use CSS pixels. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key Questions&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;How do I determine the right numbers in the &lt;em&gt;sizes&lt;/em&gt; and &lt;em&gt;srcset&lt;/em&gt; attributes for my image?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How are different DPRs (device pixel ratios) treated?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How do I test a responsive image?&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let's answer&lt;/strong&gt; these questions one by one, see some unexpected results when answering the third question, and later answer a hidden question. &lt;/p&gt;

&lt;p&gt;1.&lt;/p&gt;

&lt;p&gt;The first thing to agree on is that art-direction is out-of-scope. All we care about here is to get a sensible number of pixels under any viewport. Now we can focus on the &lt;em&gt;srcset&lt;/em&gt; and &lt;em&gt;sizes&lt;/em&gt; attributes:&lt;/p&gt;

&lt;p&gt;&lt;span&gt; &lt;/span&gt;&lt;strong&gt;Srcset&lt;/strong&gt; - In the example above we've two Superman images, with intrinsic sizes of 500w, 1000w which we match to intrinsic widths of 500w, 1000w (by using a space in the &lt;em&gt;srcset&lt;/em&gt; attribute). This is fairly simple. Usually, you would like to have 5 - 10 breakpoints, and there are many automation solutions for image resizing. I'll use the Cloudinary notation throughout this post.&lt;br&gt;
&lt;span&gt; &lt;/span&gt;For advanced breakpoints generation, based on the bytes size of the image rather than static width steps, you could use the Cloudinary &lt;a href="https://www.responsivebreakpoints.com/" rel="noopener noreferrer"&gt;Responsive Breakpoints Generator&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span&gt; &lt;/span&gt;&lt;strong&gt;Sizes&lt;/strong&gt; - The media queries in the &lt;em&gt;sizes&lt;/em&gt; attributes should correlate to the media queries in your main CSS. Let's explore a scenario: imagine a webpage with a 2-column layout above 600px and a single-column layout for smaller viewports. The hero image spans the entire width for all forms.&lt;/p&gt;

&lt;p&gt;In the 2-column layout, each column occupies half the page, and the images within take up 90% of the column width (equivalent to 45% of the entire viewport). In the single-column layout, the column spans 90% of the page width, and the image uses 80% of the column width (or 72% of the viewport).&lt;/p&gt;

&lt;p&gt;For this layout, the &lt;em&gt;sizes&lt;/em&gt; attribute for column images would be &lt;code&gt;(min-width: 600px) 45vw, 72vw&lt;/code&gt;, and the hero image would have &lt;code&gt;sizes="100vw"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a visual representation:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frv6rwihjeaayqq6mbv3a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frv6rwihjeaayqq6mbv3a.png" alt="Large vs small screen" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may wonder how come the neat common example had 500, and 1000 in both the &lt;em&gt;srcset&lt;/em&gt; and &lt;em&gt;sizes&lt;/em&gt; have diverged. In fact, you shouldn't expect such a correlation. It is possible that your image doesn't need any media query. Let's say that you have a banner of 100% of the viewport across all layouts, then sizes="100vw" is the exact match. &lt;/p&gt;

&lt;p&gt;2.&lt;/p&gt;

&lt;p&gt;Different DPRs are handled automatically by the browser. CSS units and intrinsic width (w) are converted seamlessly according to the device characteristics. If the common example is used with a DPR 2 screen, it will try to match images of 1000w and 2000w.&lt;/p&gt;

&lt;p&gt;&lt;span&gt; &lt;/span&gt;Sometimes, you’ll need greater control over your images than the &lt;em&gt;img&lt;/em&gt; tag offers. This is where the &lt;em&gt;picture&lt;/em&gt; tag is handy—for instance, if you wish to limit the DPR factor to 2, ensuring optimal pixel delivery for large screens without compromising performance on smaller screens with higher DPR values. Let's take the example of a hero image with a layout of 100vw that is mapped to 1200w and 1800w. A DPR 2 desktop with 900px viewport can get 2400w, but for a mobile device with DPR 3 and a 600px viewport you want to deliver 1200w favoring performance over being pixel-perfect.&lt;br&gt;
While we simplify matters by not delving into the &lt;em&gt;picture&lt;/em&gt; tag, the principle remains—strategically control image delivery for an optimal visual experience.&lt;/p&gt;

&lt;p&gt;3.&lt;/p&gt;

&lt;p&gt;Testing is challenging because of two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Judging if an image has enough pixels or not requires visual concentration, and is slow. I searched for a &lt;a href="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a3/Elderly_Gambian_woman_face_portrait.jpg/1082px-Elderly_Gambian_woman_face_portrait.jpg" rel="noopener noreferrer"&gt;fine-detailed image&lt;/a&gt; to make it easier but it was still eye-straining and slow. So I created a synthetic test image (later below).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Browser cache images aggressively, and just hard-refresh or disabling caching on devtools is not enough to actually re-render an image when you downsize the viewport. I built a CSB project that you can use as well.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s deep dive into how I created an intuitive online tester (or skip to the link at the end of this section).&lt;/p&gt;

&lt;p&gt;For a synthetic test pattern that would ease visualizing pixels’ density changes, I  decided I'll take vertical interleaved stripes starting from 1px width and slowly increasing to 6px width. It sounds like a very simple SVG&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;svg width="300" height="150" viewport="0 0 100 100" xmlns="http://www.w3.org/2000/svg"&amp;gt;
   &amp;lt;defs&amp;gt;
       &amp;lt;pattern id="verticalLines" x="0" y="0" width="3" height="100%" patternUnits="userSpaceOnUse"&amp;gt;
           &amp;lt;line x1="0" y1="0" x2="0" y2="150" stroke="red" stroke-width="1" /&amp;gt;
           &amp;lt;line x1="1" y1="0" x2="1" y2="150" stroke="green" stroke-width="1" /&amp;gt;
           &amp;lt;line x1="2" y1="0" x2="2" y2="150" stroke="blue" stroke-width="1" /&amp;gt;
       &amp;lt;/pattern&amp;gt;
   &amp;lt;/defs&amp;gt;
   &amp;lt;rect width="100%" height="100%" fill="url(#verticalLines)" /&amp;gt;
&amp;lt;/svg&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, the result was poor. The first line came up split to white on its left and red on its right. It's possible on a DPR 2 screen, but that wasn't my intention.&lt;/p&gt;

&lt;p&gt;So I went creating a PNG, finally coding :)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"""
Script to generate synthetic test images for responsive HTML testing.
Uses the PIL library for image manipulation.
"""

from PIL import Image, ImageDraw  # pip install Pillow

def create_lines_image(width, height, colors, line_width, max_width, filename):
   image = Image.new("RGB", (width * max_width, height))
   draw = ImageDraw.Draw(image)
   colors_num = len(colors)

   for h in range(0, max_width):
       for x in range( width * h, width * (h + 1), line_width + h):
           draw.line([(x, 0), (x, height)], \
           fill=colors[(int(x / (h + 1))) % colors_num], width=line_width + h)


   image.save(filename)

create_lines_image(360, 150, "red,blue,green".split(','), 1, 6, 'rgb.png')
create_lines_image(360, 150, "cyan,magenta,yellow".split(','), 1, 6, 'cmy.png')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then I created a naive HTML page, which already gave some encouraging results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
   &amp;lt;meta charset="UTF-8"&amp;gt;
   &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
   &amp;lt;title&amp;gt;Responsive HTML demo&amp;lt;/title&amp;gt;
   &amp;lt;link rel="icon" type="image/x-icon" href="https://www.cloudinary.com/images/favicon.ico"&amp;gt;
   &amp;lt;style&amp;gt;
      img {
         max-width: 100%;
         min-height: 75px;
      }
   &amp;lt;/style&amp;gt;

&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
   &amp;lt;h1&amp;gt;Resize the window and hard-refresh to test responsive HTML&amp;lt;/h1&amp;gt;
   &amp;lt;div&amp;gt;
   &amp;lt;strong&amp;gt;Static 1080w&amp;lt;/strong&amp;gt;&amp;lt;br&amp;gt;
   &amp;lt;img width="1080" src="https://papish.cloudinary.us/w_1080/cmy.png" alt="Static 1080w"&amp;gt;
   &amp;lt;br&amp;gt;&amp;lt;strong&amp;gt;Static 2160w&amp;lt;/strong&amp;gt;&amp;lt;br&amp;gt;
   &amp;lt;img width="1080" src="https://papish.cloudinary.us/w_2160/cmy.png" alt="Static 2160w"&amp;gt;
   &amp;lt;br&amp;gt;&amp;lt;strong&amp;gt;Responsive W&amp;lt;/strong&amp;gt;&amp;lt;br&amp;gt;
   &amp;lt;img width="1080" src="https://papish.cloudinary.us/w_1080/cmy.png" alt="Responsive W"
srcset="https://papish.cloudinary.us/w_2160/cmy.png 2160w, https://papish.cloudinary.us/w_1620/cmy.png 1620w, https://papish.cloudinary.us/w_1080/cmy.png 1080w, https://papish.cloudinary.us/w_540/cmy.png 540w"
sizes="100vw"&amp;gt;
   &amp;lt;br&amp;gt;&amp;lt;strong&amp;gt;Responsive X&amp;lt;/strong&amp;gt;&amp;lt;br&amp;gt;
   &amp;lt;img width="1080" src="https://papish.cloudinary.us/w_1080/cmy.png" alt="Responsive x"
srcset="https://papish.cloudinary.us/w_2160/cmy.png 2x, https://papish.cloudinary.us/w_1620/cmy.png 1.5x, https://papish.cloudinary.us/w_1080/cmy.png 1x, https://papish.cloudinary.us/w_540/cmy.png 0.5x"&amp;gt;
   &amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Small viewport&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38aaruisbgchprec21he.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38aaruisbgchprec21he.png" alt="Small viewport" width="800" height="1258"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Large viewport&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flu1q5qv4lvcx81l1hzhh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flu1q5qv4lvcx81l1hzhh.png" alt="Large viewport" width="800" height="579"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Testing was still slow, as I was fooled again and again by the Google Chrome caching, so I added text overlays on the images, to be absolutely sure that I know which image is used. Then I added a cache busting on the resize event, and also added on-screen metrics. And then added SEO suffices, so it's easy to see in the network tab which image rendition is used. All of these are super-easy with Cloudinary URL-based syntax.&lt;/p&gt;

&lt;p&gt;Now that the demo page is bloated with JS, it allows you to resize the viewport, and see what happens both visually and numerically.&lt;/p&gt;

&lt;p&gt;Feel free to explore the impact of viewport resizing and observe the changes both visually and numerically. Visit the demo page: &lt;a href="https://codepen.io/Yuval-Papish/full/zYbjgWo" rel="noopener noreferrer"&gt;Interactive Responsive Images Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Medium viewport&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8nnwxoz15y8yj38nfvhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8nnwxoz15y8yj38nfvhg.png" alt="Medium viewport" width="800" height="936"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mobile emulated&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F341ekjrxvcnd02q71dhd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F341ekjrxvcnd02q71dhd.png" alt="Mobile viewport" width="650" height="948"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note the mobile, 1.5x is the 1620px which is way more than needed for a 320x480x1.5 screen. It means that the 'x' descriptor is useful only for a limited use case of a fixed CSS width design, because it ignores the viewport size. Using only 'w' would cover all bases, and don't hesitate to use a trivial &lt;em&gt;sizes&lt;/em&gt; attribute like a sizes="50vw".&lt;/p&gt;

&lt;p&gt;Only after I was able to play with the window and see the numeric results interactively, I understood that Chrome selected the 1620w when the image was rendered below 1840w. With Firefox the threshold was at 1590w. Safari had the threshold at 1588w (round with a bite :) . The HTML spec doesn't define how to choose the image candidates and Chrome would deliver lower-res images than other browsers (if I may over-conclude). Keep it in mind when you compare browsers' performance, and when looking for pixel-perfect results.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Responsive images with HTML can greatly benefit your webpage, as long as they are coherent with the webpage layout. You should evaluate each image slot with each possible layout of the page, and correlate the image responsiveness to the different layouts. Testing across multiple devices and viewports is paramount, while asserting that the browser’s cache doesn’t obfuscate the real user’s experience.&lt;/p&gt;

</description>
      <category>webperf</category>
      <category>html</category>
    </item>
  </channel>
</rss>
