-
Hajipur, Bihar, 844101
Responsive images automatically adjust their size and resolution based on the user’s screen size or device resolution. This ensures that:
The image fits its container
It doesn’t overflow or cause horizontal scrolling
It loads appropriately for both mobile and desktop
Users on slower networks get faster load times
✅ Improves page speed
✅ Enhances mobile experience
✅ Prevents layout breaking
✅ Supports high-resolution (Retina) screens
✅ Saves bandwidth for mobile users
max-width: 100%
and height: auto
This is the simplest way to make images scale with their containers.
img {
max-width: 100%;
height: auto;
}
<div class="image-box">
<img src="banner.jpg" alt="Responsive Banner">
</div>
.image-box {
width: 90%;
margin: auto;
}
img {
max-width: 100%;
height: auto;
}
srcset
and sizes
This tells the browser to choose the most appropriate image based on screen width or pixel density.
<img
src="small.jpg"
srcset="medium.jpg 768w, large.jpg 1200w"
sizes="(max-width: 768px) 100vw, 50vw"
alt="Mountain View">
<picture>
ElementGives even more control, allowing different image formats (like WebP) or art direction.
<picture>
<source srcset="image.webp" type="image/webp">
<source srcset="image.jpg" type="image/jpeg">
<img src="image.jpg" alt="Sample Image">
</picture>
Make sure the container is also responsive using percentages or flex/grid.
.container {
width: 100%;
max-width: 800px;
margin: auto;
}
<img
src="logo.png"
srcset="logo.png 1x, logo@2x.png 2x"
alt="Company Logo">
This delivers sharper images on high-resolution (Retina) screens.
Use background-size: cover;
and background-position
.
.hero {
background-image: url('bg.jpg');
background-size: cover;
background-position: center;
height: 300px;
}
✅ Write HTML/CSS to handle these scenarios:
Q1. Add an image that scales with the screen width.
Q2. Use max-width: 100%
and explain its effect.
Q3. Add a srcset
with 2 image versions for different widths.
Q4. Use a <picture>
tag to load WebP on supported browsers.
Q5. Add a background image that adjusts to screen size.
Q6. Prevent image overflow in a fixed container.
Q7. Create an image that fills half the screen on desktop and full width on mobile.
Q8. Display a high-res image only on Retina screens.
Q9. Create a responsive image gallery using Flexbox.
Q10. Use media queries to hide/show images based on device size.
Q1: What does max-width: 100% do for images?
Q2: Which HTML attribute is used for responsive image selection?
Q3: Which HTML tag allows image format fallback (e.g., WebP to JPG)?
Q4: What does height: auto do in responsive images?
Q5: What is the benefit of using srcset?
Q6: Which method helps in serving images for Retina screens?
Q7: What does background-size: cover; do?
Q8: How to prevent image from overflowing its container?
Q9: Which attribute is NOT valid for responsive images?
Q10: Which CSS property is essential for responsive background images?