The Wayback Machine - https://web.archive.org/web/20250214161456/https://www.geeksforgeeks.org/how-to-auto-resize-an-image-to-fit-a-div-container-using-css/
Open In App

How to Auto-Resize an Image to Fit a Div Container using CSS?

Last Updated : 13 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

To resize an image or video to fit inside a div container, use the object-fit property for precise control over how the content fits.

  • object-fit maintains the aspect ratio or stretches the content to fill the container.
  • Common values include cover (fills the container, may crop) and contain (fits within the container without cropping).

Different Ways to Auto-Resize an Image to Fit a Div Container Using CSS

1. Using object-fit: cover;

This method ensures the image covers the entire container while maintaining its aspect ratio, potentially cropping parts of the image.

html
<html>
<head>
    <style>
        .container {
            width: 300px;
            height: 200px;
            border: 1px solid #000;
        }
        img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    </style>
</head>
<body>
    <div class="container">
        <img src="https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png"
             alt="Sample Image">
    </div>
</body>
</html>
  • The object-fit: cover; property ensures the image covers the entire container, potentially cropping parts of the image to maintain its aspect ratio.

2. Using object-fit: contain;

HTML
<html>
<head>
    <style>
        .container {
            width: 300px;
            height: 200px;
            border: 1px solid #000;
        }
        img {
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
    </style>
</head>
<body>
    <div class="container">
        <img src="https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" 
             alt="Sample Image">
    </div>
</body>
</html>
  • The object-fit: contain; property scales the image to fit within the container without cropping, preserving the entire image but potentially leaving empty space if the aspect ratios differ.

3. Using max-width: 100%; and height: auto;

html
<html>
<head>
    <style>
        .container {
            width: 300px;
            border: 1px solid #000;
        }
        img {
            max-width: 100%;
            height: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <img src="https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" 
             alt="Sample Image">
    </div>
</body>
</html>
  • Setting max-width: 100%; and height: auto; ensures the image scales down to fit the container’s width while maintaining its aspect ratio, preventing distortion.

How to Auto-Resize an Image to Fit a Div Container using CSS?- FAQs

How can I ensure an image maintains its aspect ratio while fitting within a div?

Use the object-fit: contain; property to scale the image to fit within the container without cropping, preserving its aspect ratio.

What does object-fit: cover; do to an image within a div?

The object-fit: cover; property scales the image to fill the entire container, potentially cropping parts of the image to maintain its aspect ratio.

Is there a way to prevent an image from exceeding its container’s size?

Yes, applying max-width: 100%; and height: auto; to the image ensures it scales down appropriately without exceeding the container’s dimensions.

How can I handle images of varying sizes to fit uniformly within divs?

Utilize the object-fit property with values like cover or contain to ensure images of different sizes fit uniformly within their containers.

Are there any considerations for older browsers when using object-fit?

Yes, the object-fit property may not be supported in older browsers. In such cases, alternative methods like setting max-width and max-height can be used to achieve similar results.




Next Article

Similar Reads

three90RightbarBannerImg