I have an div with the following code
HTML:
<div id='imgContainer'>
<img src='/img/logo.png' id='imglogo'></img>
</div>
CSS
div#imgContainer {
width: 250px;
height: 250px;
padding: 13px;
}
The problem is the users can edit the image size, colour and some other things, so I want the div to get bigger as the image get bigger. I want the width and height of the div to be 250px if the image is smaller than it, but to get bigger as the image gets bigger.
The solution can be in PHP, JavaScript, CSS or jQuery.
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.
Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.
Using inline-block property: Use display: inline-block property to set a div size according to its content.
Try this:
div#imgContainer {
min-width: 250px;
min-height: 250px;
padding: 13px;
display: inline-block;
}
Fiddle
Thanks to @ahren and @Mohsen
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With