How to create responsive images in HTML ? (from A to Z)


A responsive image is when a different physical image (in width or resolution) is served for the same logical image depending on the characteristic of the device property (width, device pixel ratio)

HTML definition

In HTML, the following elements can define a serie of image (for instance, with different dimension for each screen size)


To define a set of responsive images, you can use the img with the srcset attribute and sizes attributes. See Responsive HTML image with the srcset attribute


<img src="image.png" data-srcset="image.png 1000w, image-2x.png 2000w">


To define a set of responsive images, you can also use the picture element

  <source media="(max-width: 799px)" srcset="480w.jpg" width="600" height="279">
  <source media="(min-width: 800px)" srcset="800w.jpg" width="1500" height="300">
  <img src="800w.jpg" alt="Default if the media condition are not met" width="1500" height="300">


lazy loading

You can lazy load them also.


<img src="image.png" srcset="image.png 1000w, image-2x.png 2000w" loading="lazy">
<img class="lozad" data-src="image.png" data-srcset="image.png 1000w, image-2x.png 2000w">


You can preload the responsive image

<link rel="preload" href="bg-image-narrow.png" as="image" media="(max-width: 600px)">
<link rel="preload" href="bg-image-wide.png" as="image" media="(min-width: 601px)">
  imagesrcset="wolf_400px.jpg 400w, wolf_800px.jpg 800w, wolf_1600px.jpg 1600w"


To resize the image properly on width and height, you should apply the following CSS rules.

.responsive_img {
    max-width: 100%; 
    height: auto;

where height:auto will apply:

  • if the image was downloaded, the real intrinsic ratio of the image
  • if the image was not downloaded, the ratio given by the width and height off the img attribute.


  • Without image in src, the height:auto is using the width and height attribute.
<div style="width:150px">
        style="height:auto; max-width:100%;background: rgb(203, 241, 234);">
  • Win an inline 1×1 pixel, the result is a perfect square of 150×150 and not a rectangle following the aspect ratio of 500×150
<div style="width:150px">
        style="height:auto; max-width:100%;background: rgb(203, 241, 234);">


height: auto not working until the image is loaded

if you use a placeholder in src that is a image, it should have the same width/height ratio than the image.

For instance, with a svg where the width and height are specified in the viewbox

<div style="width:150px">
        src="data:image/svg+xml,%3Csvg xmlns='' viewBox='0 0 500 150'%3E%3C/svg%3E" 
        style="height:auto; max-width:100%;background: rgb(203, 241, 234);">

Discover More
HTML - Image (Img tag)

img is an fetch element that represents an image. An image in HTML can also be represented with a picture element that defines logically the same image but may have different physical image (different...
How to preload HTML resource (Javascript, StyleSheet)

This article shows you how to preload HTML resources to use them at a later time.
How to represent an image in HTML ?

An image in HTML can be represented by: the img element (from raster to svg image) the svg element the picture element imgpictureresponsive image
Page Loading Key Moment
Web - Timeline of a page load (Page Speed|Page Latency)

Page load is the latency performance metrics that cumulates: TCP latency (ie how fast, the network will receive the HTTP request and send back the HTTP response ) HTTP latency (ie how fast the web...

Share this page:
Follow us:
Task Runner