How To Auto-resize An Image To Fit A Div Container Using CSS?

Skip to content geeksforgeeks
  • Tutorials
    • Python Tutorial
      • Python Data Types
      • Python Loops and Control Flow
      • Python Data Structures
      • Python Exercises
    • Java
      • Java Programming Language
        • OOPs Concepts
      • Java Collections
      • Java Programs
      • Java Interview Questions
      • Java Quiz
      • Advance Java
    • Programming Languages
    • System Design
      • System Design Tutorial
    • Interview Corner
    • Computer Science Subjects
    • DevOps
    • Linux
    • Software Testing
    • Databases
    • Android
    • Excel
    • Mathematics
  • DSA
    • Data Structures
      • Linked List
      • Tree
    • Algorithms
      • Analysis of Algorithms
      • Searching Algorithms
      • Sorting Algorithms
    • Practice
      • Company Wise Coding Practice
      • Practice Problems Difficulty Wise
      • Language Wise Coding Practice
      • Curated DSA Lists
    • Company Wise SDE Sheets
    • DSA Cheat Sheets
    • Puzzles
  • Data Science
    • Data Science Packages
    • Data Visualization
    • Data Analysis
  • Web Tech
    • Web Development Using Python
      • Django
      • Flask
    • Cheat Sheets
  • Courses
    • DSA Courses
    • Programming Languages
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
  • Space between two rows in a table using CSS?
  • What is the difference between display: inline and display: inline-block in CSS?
  • How to write a:hover in inline CSS?
  • How to place two div side-by-side of the same height using CSS?
  • What is a clearfix?
  • Making a div vertically scrollable using CSS
  • How to give a div tag 100% height of the browser window using CSS
  • Wildcard Selectors (*, ^ and $) in CSS for classes
  • How to style a dropdown using CSS?
  • Remove border from IFrame using CSS
  • Hide scroll bar, but while still being able to scroll using CSS
  • How to apply !important in CSS?
  • How to use a not:first-child selector in CSS?
  • How to auto-resize an image to fit a div container using CSS?
  • How to disable resizable property of textarea using CSS?
  • Create an unordered list without any bullets using CSS
  • Set cellpadding and cellspacing in CSS
  • How to overlay one div over another div using CSS
  • How to disable a link using only CSS?
  • What does the “+” (plus sign) CSS selector mean?
  • How to Vertically Center Text with CSS ?
  • How to horizontally center a div using CSS ?
  • Change an HTML5 input placeholder color with CSS
  • How to disable text selection highlighting using CSS?
  • How to align content of a div to the bottom using CSS ?
How to auto-resize an image to fit a div container using CSS? Last Updated : 25 Jan, 2024 Improve Improve Like Article Like Save Share Report

To resize an image or video to fit inside a div container, you can use the object-fit property. This property is used to specify how the content should fit inside the container. With object-fit, you can set the content to maintain its aspect ratio or stretch to fill the entire container. This allows you to control how the content is displayed within a specific div container.

Example 1: This example describes the auto-resize image fit to the div container. This example does not contain object-fit property. 

html

<!DOCTYPE html> <html> <head> <title>To auto-resize an image</title> <style> body { display: flex; justify-content: center; } .geeks { width: 60%; height: 300px; border: 1px solid #ccc; } img { width: 100%; height: 100%; } </style> </head> <body> <div class="geeks"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="Geeks Image" /> </div> </body> </html>

Output:

In the above example as the object-fit property is not used, the image is being squeezed to fit the container, and its original aspect ratio is destroyed.

Example 2: This example is used to display the part of image when use resize the div container. 

HTML

<!DOCTYPE html> <html> <head> <title>To auto-resize an image</title> <style> body { display: flex; justify-content: center; } .geeks { width: 60%; height: 300px; border: 1px solid #ccc; } img { width:100%; height:100%; object-fit:cover; } </style> </head> <body> <div class="geeks"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="Geeks Image" /> </div> </body> </html>

Output:

Screenshot-2024-01-16-131125

Note: Using object-fit: cover; will cut off the sides of the image, preserving the aspect ratio, and also filling in space.

Example 3: This example displays an image without using object-fit property. In this example, the size of the image is set manually and the image will not be able to maintain its aspect ratio and adjust or resize according to div container on resizing the browser window. 

html

<!DOCTYPE html> <html lang="en"> <head> <title>Auto-resize of an image</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { display: flex; justify-content: center; } .container { width: 400px; height: 250px; border: 1px solid #ccc; overflow: hidden; } .container img { width: 100%; height: auto; display: block; } </style> </head> <body> <div class="container"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="GeeksforGeeks Image"> </div> </body> </html>

Output:

Screenshot-2024-01-16-130350

Example 4: This example displays the part of image or image using object-fit property. In this example, the size of the image is set manually and the object-fit property is also used. In this case, on resizing the browser the image will maintain its aspect ratio and will not be resized according to div container. 

html

<!DOCTYPE html> <html> <head> <title>To auto-resize an image</title> <style> body { text-align: center; } img { width: 400px; height: 200px; object-fit: cover; } </style> </head> <body> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="Geeks Image"> </body> </html>

 Output:                                                            Note: The property object-fit: cover; will cut the sides of the image, preserving the aspect ratio, and also filling in space.

CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.

S

Sabya_Samadder Follow Improve Previous Article How to use a not:first-child selector in CSS? Next Article How to disable resizable property of textarea using CSS?

Please Login to comment...

Similar Reads

How to stretch div to fit the container ? A child div inside a container can be made to take the complete width and height of the parent div. There are two methods to stretch the div to fit the container using CSS that are discussed below: Method 1: The first method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. E 2 min read Auto-Fit vs Auto-Fill Property in CSS Grid One of the most important features in CSS Grid is that we can create a responsive layout without using a media query. We don't need to write a media query for each viewport rather it can be adjusted by using some of the properties of CSS-Grid. It can be adjusted by simply using grid-template-columns property, repeat() function along with auto-fit a 4 min read How to Fit Background Image to Div using CSS ? In this article, we will see how to fit background images into HTML div. The main problem that is being solved here is that the dimensions of the background image of a div are not exactly the same as the dimensions of the actual div, so there arises a mismatch. Due to this mismatch, the background image doesn't fit properly in the div. Now, there a 3 min read How to fit text container width dynamically according to screen size using CSS ? In this article, we have given a text paragraph and the task is to fit the text width dynamically according to screen size using HTML and CSS. Some of the important properties used in the code are as follows- display-grid: It helps us to display the content on the page in the grid structure.grid-gap: This property sets the minimum amount of gap bet 7 min read How to Set Div Width to Fit Content using CSS? The div can be managed to set the whole screen width or the width according to the need. In this article, we are going to discuss how can we set a div width to fit content using CSS. we will discuss different approaches and code examples. Below are the approaches to set div width to fit content using CSS: Table of Content Using Default CSSUsing inl 3 min read How to make CSS Font Size Fit Container Effectively ? Making CSS font size fit a container effectively involves ensuring that the text scales appropriately based on the dimensions of the container. This is crucial for maintaining readability and a consistent user experience regardless of the device being used. Table of Content Using ViewportUsing Media QueriesUsing CSS ClampUsing ViewportWe can use vi 5 min read How to position a div at the bottom of its container using CSS? To position a div at the bottom of its container in CSS, you can use various approaches. Using absolute positioning involves setting `position: absolute;` and `bottom: 0;`, aligning the div at the container's bottom. Flexbox achieves this by employing `display: flex;` and `margin-top: auto;`, while Grid utilizes `display: grid;` with a defined temp 2 min read How to resize SVG to fill its container in ReactJS? We can resize SVG to fill its container in ReactJS using the type props. We can add the following style to the SVG CSS Syntax: svg { width: inherit; height: inherit; } React Syntax: &lt;svg style = {{ width: "inherit", height:"inherit"}} // Child elements &lt;/svg&gt;Creating React Application: Step 1: Create a React application using the following 2 min read How to overlay one div over another div using CSS Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS 2 min read How to Make a Child Div Element Wider than Parent Div using CSS ? When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to 2 min read View More Articles Article Tags :
  • CSS-Questions
  • Technical Scripter 2018
  • CSS
  • Technical Scripter
  • Web Technologies
+2 More Like three90RightbarBannerImg Explore More We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy Got It ! Lightbox Improvement Please go through our recently updated Improvement Guidelines before submitting any improvements. This article is being improved by another user right now. You can suggest the changes for now and it will be under the article's discussion tab. You will be notified via email once the article is available for improvement. Thank you for your valuable feedback! Suggest changes Please go through our recently updated Improvement Guidelines before submitting any improvements. Suggest Changes Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal. geeksforgeeks-suggest-icon Create Improvement Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all. geeksforgeeks-improvement-icon Suggest Changes Suggestion[CharLimit:2000] Create Improvement

What kind of Experience do you want to share?

Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences Can't choose a topic to write? click here for suggested topics Write and publish your own Article

Từ khóa » Html Scale Background Image To Fit Div