Responsive Images: Free HTML & CSS Tutorial
Master responsive images for faster loading websites
Two Responsive Image Techniques
Img Srcset
Same image across various screen sizes with optimal resolution. Mobile gets small files, desktop gets large sizes, and hi-res screens get appropriate images automatically.
Picture Element
Provides art direction control with different images, croppings, and aspect ratios. Vertical images for mobile, horizontal for desktop layouts.
Setup Instructions
Open Project Files
Navigate to Desktop > Class Files > Advanced HTML CSS Class > Responsive Images folder and open in your code editor
Open Source File
Open srcset.html from the Responsive Images folder in your code editor
Preview in Chrome
Open srcset.html in Chrome browser as we'll be using Chrome DevTools for testing
The src attribute serves as fallback for older browsers that don't understand srcset. Check caniuse.com for current browser support statistics.
Adding Srcset with Sizes
Comment Out Previous Code
Comment out the 1x vs 2x heading and img tag to work with the new sizes example
Add Image Sources
Define srcset with multiple image widths (320w, 640w, 1280w, 2560w) where 'w' indicates pixel width
Define Sizes Attribute
Set media conditions with viewport-relative sizes: (min-width: 800px) 50vw, (min-width: 640px) 75vw, 100vw
The browser must decide which file to download before rendering the page. This speeds up asset downloading and page rendering, which is why we provide size information upfront.
Picture Element vs Img Srcset
| Feature | Picture Element | Img Srcset |
|---|---|---|
| Browser Control | Developer dictates which source | Browser has discretion to choose |
| Image Variation | Different croppings/aspect ratios | Same image, different resolutions |
| Use Case | Art direction needed | Optimization only |
Picture Element Breakpoint Flow
Mobile (767px and smaller)
model-mobile-375.jpg or model-mobile-750.jpg displays at 100vw width
Tablet (768px to 1279px)
model-tablet-768.jpg or model-tablet-1536.jpg displays at 75vw width
Desktop (1280px and above)
model-desktop-1280.jpg or model-desktop-2560.jpg displays at 50vw width
Key Takeaways

and select Add device pixel ratio.