{"id":1739,"date":"2016-03-09T11:45:16","date_gmt":"2016-03-09T10:45:16","guid":{"rendered":"https:\/\/sii.pl\/blog\/?p=1739"},"modified":"2023-01-17T17:53:08","modified_gmt":"2023-01-17T16:53:08","slug":"responsive-web-design-images","status":"publish","type":"post","link":"https:\/\/sii.pl\/blog\/en\/responsive-web-design-images\/","title":{"rendered":"Responsive Web Design Images"},"content":{"rendered":"\n<p>Responsive web design (RWD) is an approach to web design aimed at crafting sites to provide an optimal viewing and interaction experience, easy reading and navigation with a minimum of resizing, panning and scrolling (across a wide range of devices \u2013 from desktop computer monitors to mobile phones).<br><br>And how about images in RWD? In the past it was easy \u2013 just check what size of image you need, open Photoshop and save image \u201cfor web\u201d, that was it. Everything changed when six years ago, Apple announced iPhone 4 and Ethan Marcotte published an article about&nbsp;<a href=\"http:\/\/alistapart.com\/article\/responsive-web-design\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >Responsive Web Design<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using the width property<\/strong><\/h2>\n\n\n\n<p>The first idea, when everything on the website began to be \u201cfluid\u201d, was to specify in CSS image width to a size not larger than the screen, and for this\u00a0each image should use the CSS style, as shown below:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\nimg {\nwidth: 100%;\nheight: auto;\n}\n<\/pre><\/div>\n\n\n<p>The image will be responsive and scale up and down. If we don\u2019t want it to be scaled up to be larger than its original size, then we use:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\nimg {\nmax-width: 100%;\nheight: auto;\n}\n<\/pre><\/div>\n\n\n<p>OK, but still, on each device the same image will be loaded and this&nbsp;will have a huge impact on the website load speed, especially on mobile devices \u2013 as we can check on a sample bitmap image (75 dpi), comparing the file size with&nbsp;the resolution:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Image&nbsp;width<\/th><th>Devices<\/th><th>File size<\/th><\/tr><tr><td>320px<\/td><td>Older phones<\/td><td>40 KB<\/td><\/tr><tr><td>480px<\/td><td>Newer Phones<\/td><td>60 KB<\/td><\/tr><tr><td>768px<\/td><td>Tablets<\/td><td>110 KB<\/td><\/tr><tr><td>960px<\/td><td>Desktop computers or high resolution devices<\/td><td>160 KB<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Media queries<\/strong><\/h2>\n\n\n\n<p>To avoid loading one image for each device, web developers started to use media queries in CSS. By using \u201c@media\u201d and \u201cwidth\u201d condition tags, developers where able to tell \u00a0the browser, which image should be displayed on a specified viewport width\/device.<br>For Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: css; title: ; notranslate\" title=\"\">\n\/* For width smaller than 400px: *\/\nimg {\nbackground-image: url('ismall.jpg');\n}\n\/* For width 400px and larger: *\/\n@media only screen and (min-width: 400px) {\nimg {\nbackground-image: url('imedium.jpg');\n}\n}\n<\/pre><\/div>\n\n\n<p>But again, responsive designers have settled on varying the page layout based on one variable (viewport width). When dealing with responsive images, we are really concerned with three more variables:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>image size relative to the viewport<\/li><li>screen density<\/li><li>file dimensions<\/li><\/ol>\n\n\n\n<p>And thinking about this, when and what&nbsp;knows what?<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Variable<\/th><th>Web developer<\/th><th>Browser<\/th><\/tr><\/thead><tbody><tr><th>viewport width<\/th><td>no<\/td><td>yes<\/td><\/tr><tr><th>image size relative to the viewport<\/th><td>yes<\/td><td><strong>no<\/strong><\/td><\/tr><tr><th>screen density<\/th><td>no<\/td><td>yes<\/td><\/tr><tr><th>dimensions of source files<\/th><td>yes<\/td><td><strong>no<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>So wouldn\u2019t it be nice to pass all the information to the browser ?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u201cSRCSET\u201d&nbsp;and \u201cSIZES\u201d!<\/strong><\/h2>\n\n\n\n<p>There is an option to use, new attributes on the &lt;img> tag called srcset and sizes.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;img\nsrc=&quot;small.jpg&quot;\nsrcset=&quot;large.jpg 1024w, medium.jpg 640w, small.jpg 320w&quot;\nsizes=&quot;(min-width: 36em) 33.3vw, 100vw&quot;\nalt=&quot;rwd&quot; \/&gt;;\n<\/pre><\/div>\n\n\n<p><strong>Srcset&nbsp;<\/strong>gives a suggestion, extra information, to help the browser decide, which image should be used. We simply tell the browser the things that it didin\u2019t&nbsp;know. In this attribute we provide a list of images as valid, non-empty URL\u2019s and a width descriptor (non-negative integer greater than zero, representing the image width value and small letter \u201cw\u201d).<\/p>\n\n\n\n<p>srcset=\u201d[<em>image URL<\/em>] &nbsp;[<em>image width<\/em>]w, [<em>image URL<\/em>] &nbsp;[<em>image width<\/em>]w, \u2026 etc\u201d<\/p>\n\n\n\n<p>The second information we want to pass to the browser is how large the image will render within our layout. For this, we have&nbsp;<strong>sizes<\/strong>.<\/p>\n\n\n\n<p>sizes=\u201d[<em>media query<\/em>] [<em>length<\/em>], [<em>media query<\/em>] [<em>length<\/em>], \u2026 etc\u201d<\/p>\n\n\n\n<p>The browser goes over each media query until it finds a matching one&nbsp;and uses that query\u2019s paired length as the last piece of the source \u2013 picking puzzle: the image rendered width or relative to the viewport. In our example we pass information to a browser, so when&nbsp;the viewport width is greater than 36em, we use 1\/3 of it, otherwise use all of it. That is a typical example of images in 3 columns on a wide screen and in one column on devices with smaller resolution screens.<\/p>\n\n\n\n<p>So, going back to our table of what knows what, by those two attributes in an &lt;img&gt; tag we can pass on new information to a browser&nbsp;<strong>srcset<\/strong>&nbsp;=&nbsp;source files dimensions and&nbsp;<strong>sizes&nbsp;<\/strong>=&nbsp;image size relative to the viewport.<\/p>\n\n\n\n<p>The biggest advantage of using srcset and sizes compared to media queries is that, when you use media queries browsers&nbsp;<strong>must&nbsp;<\/strong>load the associated source and as I wrote before srcset and sizes are more a suggestion and the browser can decide and&nbsp;optionally load smaller sources when the bandwidth is slow or expensive.<\/p>\n\n\n\n<p><strong>Browser support<\/strong><br>The srcset attribute has been supported in Chrome since version 34. At the time of writing, it is supported by almost all browsers. Only Opera Mini and Internet Explorer are a problem, but there are also projects like\u00a0<a href=\"http:\/\/scottjehl.github.io\/picturefill\/\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >Picturefill<\/a>\u00a0that allow you to use these new attributes even with\u00a0Internet Explorer.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a href=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/01\/RWD-Images-srcset-Software-Development1.png\"><img decoding=\"async\" width=\"840\" height=\"436\" src=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/01\/RWD-Images-srcset-Software-Development1.png\" alt=\"\" class=\"wp-image-18440\" srcset=\"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/01\/RWD-Images-srcset-Software-Development1.png 840w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/01\/RWD-Images-srcset-Software-Development1-300x156.png 300w, https:\/\/sii.pl\/blog\/wp-content\/uploads\/2023\/01\/RWD-Images-srcset-Software-Development1-768x399.png 768w\" sizes=\"(max-width: 840px) 100vw, 840px\" \/><\/a><figcaption>Srcset attribute \u2013 source : http:\/\/caniuse.com\/#feat=srcset<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Responsive lazy load<\/strong><br>One more library worth mentioning is\u00a0<a href=\"https:\/\/afarkas.github.io\/lazysizes\/\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >lazysize<\/a>,\u00a0which lazy loads images (including responsive images (picture\/srcset)). By using it, the web developer can not only pass all the information to the browser, but also the image will be loaded when the user scrolls down the website to the point where it is placed.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;img\ndata-srcset=&quot;small.jpg 500w, medium.jpg 640w, big.jpg 1024w&quot;\nsizes=&quot;(min-width: 1000px) 930px, 90vw&quot;\ndata-src=&quot;medium.jpg&quot;\nclass=&quot;lazyload&quot;\nalt=&quot;rwd&quot; \/&gt;;\n<\/pre><\/div>\n\n\n<p><strong>Links<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/caniuse.com\/#feat=srcset\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >http:\/\/caniuse.com\/#feat=srcset<\/a><\/li><li><a href=\"https:\/\/html.spec.whatwg.org\/multipage\/embedded-content.html#attr-img-srcset\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/html.spec.whatwg.org\/multipage\/embedded-content.html#attr-img-srcset<\/a><\/li><li><a href=\"https:\/\/scottjehl.github.io\/picturefill\/\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/scottjehl.github.io\/picturefill\/<\/a><\/li><li><a href=\"https:\/\/responsiveimages.org\/\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/responsiveimages.org\/<\/a><\/li><li><a href=\"https:\/\/afarkas.github.io\/lazysizes\/\" target=\"_blank\" rel=\"noreferrer noopener\" rel=\"nofollow\" >https:\/\/afarkas.github.io\/lazysizes\/<\/a><\/li><\/ul>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1739&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;11&quot;,&quot;greet&quot;:&quot;&quot;,&quot;legend&quot;:&quot;0\\\/5 ( votes: 0)&quot;,&quot;size&quot;:&quot;18&quot;,&quot;title&quot;:&quot;Responsive Web Design Images&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} ( {votes}: {count})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 11px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 18px; height: 18px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 14.4px;\">\n            <span class=\"kksr-muted\"><\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Responsive web design (RWD) is an approach to web design aimed at crafting sites to provide an optimal viewing and &hellip; <a class=\"continued-btn\" href=\"https:\/\/sii.pl\/blog\/en\/responsive-web-design-images\/\">Continued<\/a><\/p>\n","protected":false},"author":74,"featured_media":2116,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_editorskit_title_hidden":false,"_editorskit_reading_time":3,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","inline_featured_image":false,"footnotes":""},"categories":[1320],"tags":[1420,1484,1417,1485,1419],"class_list":["post-1739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hard-development","tag-frontend-en","tag-html5-en","tag-javascript-frameworks-en","tag-responsive-web-design-en","tag-web-development-en"],"acf":[],"aioseo_notices":[],"republish_history":[],"featured_media_url":"https:\/\/sii.pl\/blog\/wp-content\/uploads\/2016\/01\/RWD-Images-Software-Development.jpg","category_names":["Hard development"],"_links":{"self":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/1739"}],"collection":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/users\/74"}],"replies":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/comments?post=1739"}],"version-history":[{"count":2,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/1739\/revisions"}],"predecessor-version":[{"id":18442,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/posts\/1739\/revisions\/18442"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media\/2116"}],"wp:attachment":[{"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/media?parent=1739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/categories?post=1739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sii.pl\/blog\/en\/wp-json\/wp\/v2\/tags?post=1739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}