Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a div the same size as an image inside it

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.

like image 303
LightningBoltϟ Avatar asked Nov 27 '13 12:11

LightningBoltϟ


People also ask

How do I make a div fit an image?

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.

How do I fit an image IMG inside a div and keep the aspect ratio?

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.

How do I fit content inside a div?

Using inline-block property: Use display: inline-block property to set a div size according to its content.


1 Answers

Try this:

div#imgContainer {
    min-width: 250px;
    min-height: 250px;
    padding: 13px;
    display: inline-block;
}

Fiddle

Thanks to @ahren and @Mohsen

like image 109
karaxuna Avatar answered Oct 05 '22 13:10

karaxuna