01 / 12

How do you make images responsive? What are different attributes and elements which we can use?

Difficulty: 5/10
srcset attribute, picture element, image performance

Responsive images in HTML allow browsers to load the most appropriate image depending on the device screen size, resolution, or other conditions. Two main tools for this are the srcset attribute (used on <img>) and the <picture> element.

Key Concepts
  1. 1

    srcset on <img>: Lets you define multiple versions of the same image at different resolutions or sizes. The browser automatically chooses the best one based on screen size and pixel density.

  2. 2

    sizes attribute: Works with srcset to tell the browser how much space the image will take up in different layouts.

  3. 3

    <picture> element: Gives more control by letting you specify multiple image sources with <source> elements. Useful for art direction (e.g., showing a cropped image on mobile vs. full image on desktop).

  4. 4

    Fallback <img>: Inside <picture>, the <img> tag provides the default image if none of the <source> conditions match.

Example Usage

In short: Use srcset + sizes when you only need different image resolutions, and use <picture> when you need different images entirely depending on screen size or context.

Scenario Questions

Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.