How to make an image responsive in HTML?

Responsive images automatically adjust to fit different screen sizes and device dimensions. This ensures that images scale appropriately on desktops, tablets, and mobile devices without breaking the layout or becoming too large or small to view properly.

To make an image responsive in HTML, we use the <img> tag combined with CSS properties that control the image's width and height behavior. The key is setting the width to scale with the container while maintaining the image's aspect ratio.

Syntax

Following are the main CSS properties used to make images responsive −

width: 100%;
height: auto;

For better control over maximum size −

max-width: 100%;
height: auto;

Complete HTML syntax −

<img src="image.jpg" alt="description" style="width: 100%; height: auto;">

Using Inline Styles

The simplest approach is to add responsive styles directly to the image using the style attribute −

<!DOCTYPE html>
<html>
<head>
   <title>Responsive Image - Inline Style</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>Responsive Image with Inline CSS</h3>
   <img src="https://www.tutorialspoint.com/coffeescript/images/coffeescript-mini-logo.jpg" 
        alt="CoffeeScript Logo" 
        style="width: 100%; height: auto; border: 2px solid #ccc;">
   <p>Resize the browser window to see how the image scales.</p>
</body>
</html>

The image will automatically resize based on the available width while maintaining its original proportions −

Responsive Image with Inline CSS
[CoffeeScript Logo - scales with container width]
Resize the browser window to see how the image scales.

Using Internal CSS

For better organization, use internal CSS with a class to make images responsive −

<!DOCTYPE html>
<html>
<head>
   <title>Responsive Image - Internal CSS</title>
   <style>
      .responsive-image {
         width: 100%;
         height: auto;
         display: block;
         margin: 0 auto;
      }
      .container {
         max-width: 800px;
         margin: 0 auto;
         padding: 20px;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif;">
   <div class="container">
      <h3>Responsive Image with CSS Class</h3>
      <img src="https://www.tutorialspoint.com/coffeescript/images/coffeescript-mini-logo.jpg" 
           alt="CoffeeScript Logo" 
           class="responsive-image">
      <p>This image uses a CSS class for responsive behavior.</p>
   </div>
</body>
</html>

The image scales within the container and is centered using margin: 0 auto

Responsive Image with CSS Class
[CoffeeScript Logo - centered and responsive]
This image uses a CSS class for responsive behavior.

Using max-width for Better Control

The max-width property prevents images from growing larger than their original size, which is often preferable −

<!DOCTYPE html>
<html>
<head>
   <title>Responsive Image - Max Width</title>
   <style>
      .max-responsive {
         max-width: 100%;
         height: auto;
         display: block;
         border: 1px solid #ddd;
         padding: 10px;
         margin: 20px 0;
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h3>Responsive Image with max-width</h3>
   <img src="https://www.tutorialspoint.com/coffeescript/images/coffeescript-mini-logo.jpg" 
        alt="CoffeeScript Logo" 
        class="max-responsive">
   <p>This image will never exceed its original dimensions, but scales down on smaller screens.</p>
</body>
</html>

The image maintains its quality by not upscaling beyond original size −

Responsive Image with max-width
[CoffeeScript Logo - with border and padding, limited to original size]
This image will never exceed its original dimensions, but scales down on smaller screens.
Responsive Image Behavior Large Screen Full Width Image Small Screen Scaled Image Image automatically adjusts to screen size while maintaining aspect ratio

Comparison of Responsive Techniques

Property Behavior Best Use Case
width: 100% Image scales to full container width, may upscale beyond original size When you want images to always fill the container
max-width: 100% Image scales down but never exceeds original dimensions Recommended for most responsive images
height: auto Maintains aspect ratio when width is adjusted Essential for both approaches to prevent distortion

Responsive Image with Container Constraints

For more control, combine responsive images with container max-width limits −

<!DOCTYPE html>
<html>
<head>
   <title>Responsive Image with Container</title>
   <style>
      .image-container {
         max-width: 500px;
         margin: 0 auto;
         padding: 20px;
         text-align: center;
      }
      .constrained-responsive {
         max-width: 100%;
         height: auto;
         border-radius: 8px;
         box-shadow: 0 4px 8px rgba(0,0,0,0.1);
      }
   </style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f5f5f5;">
   <div class="image-container">
      <h3>Responsive Image in Constrained Container</h3>
      <img src="https://www.tutorialspoint.com/coffeescript/images/coffeescript-mini-logo.jpg" 
           alt="CoffeeScript Logo" 
           class="constrained-responsive">
      <p>Container limits maximum width while image remains responsive.</p>
   </div>
</body>
</html>

This approach gives you the best of both worlds: responsive behavior with size constraints −

Responsive Image in Constrained Container
[CoffeeScript Logo - centered with shadow and rounded corners]
Container limits maximum width while image remains responsive.

Key Points

  • Always use height: auto to maintain the image's aspect ratio and prevent distortion.

  • Prefer max-width: 100% over width: 100% to prevent unwanted upscaling.

  • Use CSS classes instead of inline styles for better maintainability across multiple images.

  • Test responsiveness by resizing the browser window or using developer tools to simulate different devices.

  • Consider container constraints to prevent images from becoming too large on wide screens.

Conclusion

Making images responsive in HTML requires setting max-width: 100% and height: auto to ensure images scale properly across different screen sizes. Using CSS classes provides better organization than inline styles, and combining responsive images with container constraints gives optimal control over layout and presentation.

Updated on: 2026-03-16T21:38:53+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements