Sometimes, we want to auto-resize an image to fit a div container with CSS
In this article, we’ll look at how to auto-resize an image to fit a div container with CSS.
How to auto-resize an image to fit a div container with CSS?
To auto-resize an image to fit a div container with CSS, we set the max-width and max-height properties.
For instance, we write
img {
max-width: 100%;
max-height: 100%;
}
to set the max-width and max-height to fill the whole container’s dimension if it doesn’t change the aspect ratio
.
Conclusion
To auto-resize an image to fit a div container with CSS, we set the max-width and max-height properties.