Responsive Web Design Images


🔹 What are Responsive Images?

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


🔸 Why Use Responsive Images?

✅ Improves page speed
✅ Enhances mobile experience
✅ Prevents layout breaking
✅ Supports high-resolution (Retina) screens
✅ Saves bandwidth for mobile users


🔸 Methods to Make Images Responsive

1. Using CSS: max-width: 100% and height: auto

This is the simplest way to make images scale with their containers.

img {
  max-width: 100%;
  height: auto;
}
✅ Example
<div class="image-box">
  <img src="banner.jpg" alt="Responsive Banner">
</div>
.image-box {
  width: 90%;
  margin: auto;
}

img {
  max-width: 100%;
  height: auto;
}

2. Using HTML 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">

3. Using the <picture> Element

Gives 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>

🔸 Image Responsiveness with Container

Make sure the container is also responsive using percentages or flex/grid.

.container {
  width: 100%;
  max-width: 800px;
  margin: auto;
}

🔸 Retina-Ready (High DPI) Images

<img 
  src="logo.png" 
  srcset="logo.png 1x, logo@2x.png 2x" 
  alt="Company Logo">

This delivers sharper images on high-resolution (Retina) screens.


🔸 Background Images (Responsive)

Use background-size: cover; and background-position.

.hero {
  background-image: url('bg.jpg');
  background-size: cover;
  background-position: center;
  height: 300px;
}

Practice Questions

✅ 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.


Responsive Web Design Images Quiz

Q1: What does max-width: 100% do for images?

A. Fixes width to 100px
B. Makes the image scale within its container
C. Adds border
D. Reduces quality

Q2: Which HTML attribute is used for responsive image selection?

A. media-query
B. srcset
C. altset
D. img-responsive

Q3: Which HTML tag allows image format fallback (e.g., WebP to JPG)?

A. <img>
B. <picture>
C. <source>
D. <responsive>

Q4: What does height: auto do in responsive images?

A. Maintains aspect ratio
B. Makes image full-screen
C. Adds padding
D. Centers the image

Q5: What is the benefit of using srcset?

A. Loads best-fit image for the screen
B. Fixes image size
C. Adds animation
D. Embeds CSS

Q6: Which method helps in serving images for Retina screens?

A. srcset with 2x
B. max-width
C. min-height
D. float: left

Q7: What does background-size: cover; do?

A. Scales background to fill the container
B. Crops the image
C. Disables background
D. Centers text

Q8: How to prevent image from overflowing its container?

A. overflow: auto
B. max-width: 100%
C. position: fixed
D. z-index: 0

Q9: Which attribute is NOT valid for responsive images?

A. srcset
B. img-fit
C. sizes
D. alt

Q10: Which CSS property is essential for responsive background images?

A. background-size: cover
B. object-fit
C. font-size
D. grid-area

Go Back Top