To make an image inside a <div> stretch to fit the dimensions of the <div>, you can use CSS properties. One common method is to use or in combination with setting the image's width and height to 100%. Here are a few examples:
object-fit: cover;
object-fit: contain;
Cover the Entire Div
If you want the image to cover the entire area of the <div> while maintaining its aspect ratio (the image may be cropped):
.image-container {
width: 100%;
height: 300px; /* Set your desired height */
overflow: hidden; /* Hide any overflow */
}
.image-container img {
width: 100%;
height: 100%;
object-fit: cover; /* Make the image cover the container */
}
Contain Within the Div
If you want the image to fit inside the <div> without cropping (the entire image will be visible, but there may be some empty space within the container):
Another method is to use the image as a background image of the <div>. This provides greater control over how the image is displayed:
<div class="image-container"></div>
.image-container {
width: 100%;
height: 300px; /* Set your desired height */
background-image: url('path_to_your_image.jpg');
background-size: cover; /* Cover the entire container */
background-position: center; /* Center the image */
}
Choose the method that best suits your needs based on whether you want to maintain the image's aspect ratio or not, and whether you want to potentially crop the image.