Responsive images help your website show pictures that look good on all screen sizes. They save data and load faster by using the right image size for each device.
Responsive images in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
<img src="small.jpg" srcset="small.jpg 500w, medium.jpg 1000w, large.jpg 1500w" sizes="(max-width: 600px) 500px, (max-width: 1200px) 1000px, 1500px" alt="Description">
srcset lists image files with their widths.
sizes tells the browser which image size to pick based on screen width.
<img src="cat-small.jpg" srcset="cat-small.jpg 400w, cat-medium.jpg 800w, cat-large.jpg 1200w" sizes="(max-width: 600px) 400px, (max-width: 900px) 800px, 1200px" alt="Cute cat">
<img src="flower.jpg" srcset="flower-1x.jpg 600w, flower-2x.jpg 1200w" sizes="100vw" alt="Beautiful flower">
<picture> <source media="(max-width: 600px)" srcset="small.jpg"> <source media="(max-width: 1200px)" srcset="medium.jpg"> <img src="large.jpg" alt="Scenery"> </picture>
This page shows one image that changes size and file based on the browser width. The border and rounded corners make it look neat. The text explains what to do.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Responsive Images Example</title> <style> body { font-family: Arial, sans-serif; padding: 1rem; max-width: 600px; margin: auto; } img { width: 100%; height: auto; border: 2px solid #333; border-radius: 0.5rem; } </style> </head> <body> <h1>Responsive Images Demo</h1> <p>Resize the browser window to see the image change size and load the best version.</p> <img src="https://via.placeholder.com/400x300.png?text=Small+Image" srcset="https://via.placeholder.com/400x300.png?text=Small+Image 400w, https://via.placeholder.com/800x600.png?text=Medium+Image 800w, https://via.placeholder.com/1200x900.png?text=Large+Image 1200w" sizes="(max-width: 600px) 400px, (max-width: 900px) 800px, 1200px" alt="Placeholder image showing responsive sizes"> </body> </html>
Always include alt text for accessibility so screen readers can describe the image.
Use width: 100% and height: auto in CSS to make images scale nicely.
Test your responsive images by resizing the browser or using browser developer tools device simulator.
Responsive images help your site load faster and look good on all devices.
Use srcset and sizes attributes on <img> to provide multiple image options.
The browser picks the best image based on screen size and resolution automatically.
Practice
srcset and sizes attributes in an <img> tag?Solution
Step 1: Understand the role of
Thesrcsetandsizessrcsetattribute lists multiple image files with different sizes or resolutions. Thesizesattribute tells the browser how large the image will appear on the screen.Step 2: How the browser uses these attributes
The browser uses this information to pick the best image to load based on the device's screen size and resolution, improving loading speed and image quality.Final Answer:
To provide different image files for different screen sizes and resolutions -> Option DQuick Check:
Responsive images = srcset + sizes [OK]
- Thinking srcset changes image color
- Confusing sizes with image captions
- Using srcset without sizes attribute
srcset and sizes?Solution
Step 1: Check the
The correct format uses image file names followed by width descriptors with 'w' (e.g., 500w, 1000w). <img srcset="small.jpg 500w, large.jpg 1000w" sizes="(max-width: 600px) 500px, 1000px" src="large.jpg" alt="Example"> uses this correctly.srcsetattribute formatStep 2: Verify the
The sizes attribute uses media conditions likesizesattribute logic(max-width: 600px)to specify image display size. <img srcset="small.jpg 500w, large.jpg 1000w" sizes="(max-width: 600px) 500px, 1000px" src="large.jpg" alt="Example"> correctly uses this to tell the browser when to use which image size.Final Answer:
<img srcset="small.jpg 500w, large.jpg 1000w" sizes="(max-width: 600px) 500px, 1000px" src="large.jpg" alt="Example"> -> Option AQuick Check:
Width descriptors use 'w' and sizes use media queries [OK]
- Using 'h' instead of 'w' for width descriptors
- Mixing min-width and max-width incorrectly
- Omitting width descriptors in srcset
<img srcset="small.jpg 400w, medium.jpg 800w, large.jpg 1200w" sizes="(max-width: 600px) 400px, 800px" src="large.jpg" alt="Sample Image">
Solution
Step 1: Analyze the sizes attribute for 400px screen width
The sizes attribute says if the screen is at most 600px wide, use 400px image size. Since 400px is less than 600px, the browser expects the image to display at 400px width.Step 2: Match the srcset image closest to 400px width
From srcset, the image with 400w descriptor is small.jpg, which matches the needed size best. The browser picks this to save bandwidth and load faster.Final Answer:
small.jpg -> Option CQuick Check:
Screen width 400px uses small.jpg [OK]
- Choosing large.jpg because it's default src
- Ignoring sizes attribute
- Assuming medium.jpg for all screens
<img srcset="image-400.jpg 400w, image-800.jpg 800w" sizes="(max-width: 600px) 800px, 400px" src="image-800.jpg" alt="Error Example">
Solution
Step 1: Review the sizes attribute syntax
The sizes attribute lists conditions and sizes in order. It should say: if max-width is 600px, use 400px, else use 800px. Here, the sizes attribute reverses these values, causing the browser to pick wrong image sizes.Step 2: Confirm other attributes are correct
The srcset uses correct 'w' descriptors, src attribute is allowed as fallback, and alt attribute is present. So no errors there.Final Answer:
The sizes attribute has incorrect order of conditions and values -> Option AQuick Check:
Sizes conditions must match correct image widths [OK]
- Swapping sizes values causing wrong image choice
- Using 'h' instead of 'w' in srcset
- Removing src fallback attribute
srcset. Which of the following is the best way to write this for an image named photo.jpg?Solution
Step 1: Understand resolution descriptors in srcset
To serve images for retina (high pixel density) screens, use resolution descriptors like '1x' and '2x' to indicate normal and double resolution images.Step 2: Check the options for correct syntax
<img src="photo.jpg" srcset="photo.jpg 1x, photo@2x.jpg 2x" alt="Photo"> uses '1x' and '2x' correctly with a fallback src attribute. <img src="photo.jpg" srcset="photo.jpg 400w, photo@2x.jpg 800w" sizes="100vw" alt="Photo"> uses width descriptors ('w'), which are better suited for different viewport sizes rather than DPR. <img srcset="photo.jpg 1x, photo@2x.jpg 2x" alt="Photo"> misses src fallback. <img src="photo.jpg" srcset="photo.jpg 1h, photo@2x.jpg 2h" alt="Photo"> uses invalid 'h' descriptors.Final Answer:
<img src="photo.jpg" srcset="photo.jpg 1x, photo@2x.jpg 2x" alt="Photo"> -> Option BQuick Check:
Use '1x' and '2x' for retina images [OK]
- Using width descriptors instead of resolution for retina
- Omitting fallback src attribute
- Using invalid 'h' descriptors
