To auto-resize an image or a video to fit in a div container use object-fit property. It is used to specify how an image or video fits in the container.
object-fit property: This property is used to specify how an image or video resize and fit the container. It tells the content how to fit in a specific div container in various way such as preserve that aspect ratio or stretch up and take up as much space as possible.
- Example 1: This example describes the auto-resize image fit to div container. This example does not contain object-fit property.
<!DOCTYPE html><html><head><style>.geeks {width:60%;height:300px;}img {width:100%;height:100%;}</style></head><body><divclass="geeks"><imgsrc=alt="Geeks Image"/></div></body></html>chevron_rightfilter_noneOutput:

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.
<!DOCTYPE html><html><head><style>.geeks {width:60%;height:300px;}img {width:100%;height:100%;object-fit:cover;}</style></head><body><divclass="geeks"><imgsrc=alt="Geeks Image"/></div></body></html>chevron_rightfilter_noneOutput:

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 it’s aspect ratio and adjust or resize according to div container on resizing the browser window.
<!DOCTYPE html><html><head><style>body {text-align:center;}img {width:400px;height:200px;}</style></head><body><imgsrc=alt="Geeks Image"></body></html>chevron_rightfilter_noneOutput:

- Example 4: This example display 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 it’s aspect ratio and will not be resized according to div container.
<!DOCTYPE html><html><head><style>body {text-align:center;}img {width:400px;height:200px;object-fit: cover;}</style></head><body><imgsrc=alt="Geeks Image"></body></html>chevron_rightfilter_noneOutput:

Note: The property object-fit: cover; will cut the sides of the image, preserving the aspect ratio, and also filling in space.
Recommended Posts:
- Difference between Docker Image and Container
- How to Rotate Container Background Image using CSS ?
- Semantic-UI | Container
- How to position a div at the bottom of its container using CSS?
- Presentational vs container components
- Difference between ng-container and ng-template in AngularJS
- Font scaling based on width of container using CSS
- Docker compose tool to run multi container applications
- How to change font size depending on width of container?
- How to make container shrink-to-fit child elements as they wrap?
- Getting started with Scikit-image: image processing in Python
- Convert an image into grayscale image using HTML/CSS
- How to get the new image URL after refreshing the image using JavaScript ?
- How to hide “Image not found” icon when source image is not found?
- ES6 | Image Map
- Bulma | Image
- How to darken an Image using CSS ?
- What is Image Blurring
- Saving an Image from URL in PHP
- CSS | Image Sprites
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


