Responsive image srcset

Size
3,093 Kb
Views
10,120

How do I make an responsive image srcset?

Http://parashuto.com/rriver/responsive-web/picture-srcset-use-case/comment-page-1#two-markups. What is a responsive image srcset? How do you make a responsive image srcset? This script and codes were developed by Tomoyuki Kashiro on 19 January 2023, Thursday.

Responsive image srcset Previews

Responsive image srcset - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>responsive image srcset</title> <meta name="viewport" content="width=device-width"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel='stylesheet prefetch' href='https://yui.yahooapis.com/pure/0.5.0/pure-min.css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div class="window-width"></div>
<!--
sizes : set width by using media query or vw(viewport width)
srcset : set images path and condition when the image is shown using `w` unit
notes : if you want to show image fixing size you would use width as usual
-->
<h1>100vm with x syntax</h1>
<h2 class="image0-width"></h2>
<table class="pure-table"> <thead> <tr> <th>viewport size</th> <th>image</th> </tr> </thead> <tr> <td>not Retina</td> <td>320.jpg</td> </tr> <tr class="pure-table-odd"> <td>Retina</td> <td>640px.jpg</td> </tr> </table>
<img sizes="100vw" srcset="http://dummyimage.com/320x200/000/fff.jpg&text=320.jpg 1x, http://dummyimage.com/640x200/000/fff.jpg&text=640.jpg 2x," src="http://dummyimage.com/640x200/000/fff.jpg&text=640.jpg" alt="photo" class="image0"/>
<h1>100vm</h1>
<h2 class="image1-width"></h2>
<table class="pure-table"> <thead> <tr> <th>viewport size</th> <th>image</th> </tr> </thead> <tr> <td> - 400px</td> <td>400.jpg</td> </tr> <tr class="pure-table-odd"> <td>401px - 600px</td> <td>600px.jpg</td> </tr> <tr> <td>601px - 800px</td> <td>800.jpg</td> </tr> <tr class="pure-table-odd"> <td>801px - </td> <td>1000.jpg</td> </tr> </table>
<img sizes="100vw" srcset="http://dummyimage.com/400x200/000/fff.jpg&text=400.jpg 400w, http://dummyimage.com/600x200/000/fff.jpg&text=600.jpg 600w, http://dummyimage.com/800x200/000/fff.jpg&text=800.jpg 800w, http://dummyimage.com/1000x200/000/fff.jpg&text=1000.jpg 1000w" src="http://dummyimage.com/400x200/000/fff.jpg&text=400.jpg" alt="photo" class="image1"/>
<h1>query</h1>
<h2 class="image2-width"></h2>
<table class="pure-table"> <thead> <tr> <th>viewport size</th> <th>image</th> </tr> </thead> <tr> <td> - 400</td> <td>400.jpg</td> </tr> <tr class="pure-table-odd"> <td>401px - 600px</td> <td>600px.jpg</td> </tr> <tr> <td>601px - 639px</td> <td>800.jpg</td> </tr> <tr class="pure-table-odd"> <td>640px - 800px</td> <td>400.jpg</td> </tr> <tr> <td>801px - </td> <td>600.jpg</td> </tr> </table>
<div class="column"> <img sizes="(min-width: 1140px) 570px, (min-width: 640px) 50vw, 100vw" srcset="http://dummyimage.com/400x200/000/fff.jpg&text=400.jpg 400w, http://dummyimage.com/600x200/000/fff.jpg&text=600.jpg 600w, http://dummyimage.com/800x200/000/fff.jpg&text=800.jpg 800w, http://dummyimage.com/1000x200/000/fff.jpg&text=1000.jpg 1000w" src="http://dummyimage.com/400x200/000/fff.jpg&text=400.jpg" alt="photo" class="image2"/> <img sizes="(min-width: 1140px) 570px, (min-width: 640px) 50vw, 100vw" srcset="http://dummyimage.com/400x200/000/fff.jpg&text=400.jpg 400w, http://dummyimage.com/600x200/000/fff.jpg&text=600.jpg 600w, http://dummyimage.com/800x200/000/fff.jpg&text=800.jpg 800w, http://dummyimage.com/1000x200/000/fff.jpg&text=1000.jpg 1000w" src="http://dummyimage.com/400x200/000/fff.jpg&text=400.jpg" alt="photo"/>
</div> <script src="js/index.js"></script>
</body>
</html>

Responsive image srcset - Script Codes CSS Codes

html, body { width: 100%; height: 100%; position: relative;
}
.window-width { position: fixed; bottom: 30px; right: 0; color: red; font-size: 20px;
}
img { max-width: 100%; vertical-align: bottom;
}
.column img { float: left;
}

Responsive image srcset - Script Codes JS Codes

// //cdnjs.cloudflare.com/ajax/libs/picturefill/2.2.0/picturefill.min.js
// if we use that polifill the image in retina display will be change based on device pixel retio
// [ no polifill ] if you use no-retina display 400px imae will be used
// [ with polifill ] if you use no-retina display 800px imae will be used
(function(){ document.addEventListener('DOMContentLoaded', function(){ var image0 = document.body.querySelectorAll('.image0')[0], image0Width = document.body.querySelectorAll('.image0-width')[0], image1 = document.body.querySelectorAll('.image1')[0], image1Width = document.body.querySelectorAll('.image1-width')[0], image2 = document.body.querySelectorAll('.image2')[0], image2Width = document.body.querySelectorAll('.image2-width')[0], windowWidth = document.body.querySelectorAll('.window-width')[0]; window.addEventListener('resize', function(){ calc(); }); setTimeout(function(){ calc(); }, 1000); function calc(){ image0Width.innerText = 'image width : ' + image0.width + 'px'; image1Width.innerText = 'image width : ' + image1.width + 'px'; image2Width.innerText = 'image width : ' + image2.width + 'px'; windowWidth.innerText = 'window width : ' + window.innerWidth + 'px'; } }, true);
})();
Responsive image srcset - Script Codes
Responsive image srcset - Script Codes
Home Page Home
Developer Tomoyuki Kashiro
Username Tkashiro
Uploaded January 19, 2023
Rating 3
Size 3,093 Kb
Views 10,120
Do you need developer help for Responsive image srcset?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Tomoyuki Kashiro (Tkashiro) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!