Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Responsive Images with CSS
📖 Scenario: You are building a simple webpage that shows a photo. You want the photo to look good on all screen sizes, from phones to large desktop monitors.
🎯 Goal: Create a responsive image that adjusts its size smoothly to fit different screen widths without distortion or overflow.
📋 What You'll Learn
Use an HTML <img> tag with a fixed image source.
Write CSS to make the image scale responsively within its container.
Ensure the image keeps its aspect ratio and does not overflow the page width.
Use modern CSS properties only (no deprecated or fixed pixel widths).
💡 Why This Matters
🌍 Real World
Responsive images are essential for websites to look good on phones, tablets, and desktops without loading huge images or breaking layouts.
💼 Career
Web developers must know how to make images responsive to improve user experience and website performance across devices.
Progress0 / 4 steps
1
Create the HTML image element
Write an HTML <img> tag with src="photo.jpg" and alt="A beautiful scenery" inside a <div> with class image-container.
CSS
Hint
Use the <img> tag with the exact src and alt attributes inside a <div> with class image-container.
2
Add CSS container width
Create a CSS rule for .image-container that sets its maximum width to 600px and width to 90% so it shrinks on smaller screens.
CSS
Hint
Use max-width: 600px; and width: 90%; inside the .image-container CSS rule.
3
Make the image responsive
Add a CSS rule for .image-container img that sets width: 100% and height: auto to make the image scale with the container while keeping its aspect ratio.
CSS
Hint
Set width: 100% and height: auto for the image inside .image-container img CSS rule.
4
Add responsive behavior for small screens
Add a CSS media query for screens with max-width 400px that sets .image-container width to 100% so the image uses full width on very small devices.
CSS
Hint
Use @media (max-width: 400px) and inside it set .image-container { width: 100%; }.
Practice
(1/5)
1. What is the main purpose of using srcset and sizes attributes in an <img> tag?
easy
A. To add captions to images
B. To make images clickable links
C. To change the image color dynamically
D. To provide different image files for different screen sizes and resolutions
Solution
Step 1: Understand the role of srcset and sizes
The srcset attribute lists multiple image files with different sizes or resolutions. The sizes attribute 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 D
Quick Check:
Responsive images = srcset + sizes [OK]
Hint: Remember: srcset and sizes help pick best image [OK]
Common Mistakes:
Thinking srcset changes image color
Confusing sizes with image captions
Using srcset without sizes attribute
2. Which of the following is the correct syntax to provide two image sources for different screen widths using srcset and sizes?
easy
A.
B.
C.
D.
Solution
Step 1: Check the srcset attribute format
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.
Step 2: Verify the sizes attribute logic
The sizes attribute uses media conditions like (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.
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 C
Quick Check:
Screen width 400px uses small.jpg [OK]
Hint: Match sizes width to closest srcset width [OK]
Common Mistakes:
Choosing large.jpg because it's default src
Ignoring sizes attribute
Assuming medium.jpg for all screens
4. Identify the error in this responsive image code:
A. The sizes attribute has incorrect order of conditions and values
B. The srcset descriptors should use 'h' instead of 'w'
C. The src attribute should be omitted when using srcset
D. The alt attribute is missing
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 A
Quick Check:
Sizes conditions must match correct image widths [OK]
Hint: Sizes order: condition then matching size [OK]
Common Mistakes:
Swapping sizes values causing wrong image choice
Using 'h' instead of 'w' in srcset
Removing src fallback attribute
5. You want to serve different image resolutions for retina and non-retina screens using srcset. Which of the following is the best way to write this for an image named photo.jpg?
hard
A.
B.
C.
D.
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 B
Quick Check:
Use '1x' and '2x' for retina images [OK]
Hint: Use '1x' and '2x' for retina images in srcset [OK]
Common Mistakes:
Using width descriptors instead of resolution for retina