

We can use the contain value so that the larger image can shrink to the given dimension of the container.įor example, insert an image of 600px height and width using the img tag as in the first example. The property takes the values like fill, contain, cover, none, and scale-down. We can specify the way how the image fits using the object-fit property. The property lets us fit the image or videos according to the size of the container. A container may be larger or smaller in size than the image. We can use the object-fit property in CSS to resize the image to fit its container. Use the object-fit Property to Resize the Image in CSS As we used the max-height and max-width properties, the larger images shrink to the size of the container. But, the container cat has a height and width of 200px. In the example below, we have inserted a random image from LoremFlickr, which has 300px of width and height. Then, select the cat class and give height and width of 200px and 200px. In CSS, select the img tag and assign the proprties max-width and max-height to 100%. Inside the div insert an image using the tag. We can set the max-height and max-width properties to an element, and thus, the elements inside the container will adjust their size.įor example, create a div with class cat in HTML. But, if their sizes are small, no effect takes place. If the content in the element has more width and height than the max-width and max-height properties, their sizes will be adjusted with the value of these properties. These properties set the maximum height and width of an element. To get rid of this problem, we can use the max-width and max-height CSS properties to auto-resize the image according to the size of the container. As a result, the webpage won’t follow our design, and it will be unattractive. The image will cover more space on the screen it will take areas from other elements. If the image is inside a specific container, sometimes the size of the image can be greater than the container’s size. Whenever we insert an image in HTML, the image occupies the total pixels of its size. Use the max-width and max-height Properties to Resize the Image in CSS This article introduces methods to resize an image in CSS to fit them in a div proportionally, maintaining its height and width.

Use the auto Value for Width and the max-height Property to Resize the Image in CSS.Use the object-fit Property to Resize the Image in CSS.Use the max-width and max-height Properties to Resize the Image in CSS.
