Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image size to fit in fixed size div

Tags:

html

css

I have div of fixed size height:45px and width:60px and no other css to div. This div used to show the image uploaded by customer. I want to give the dimensions of image so that the uploaded image will be fit in given div perfectly. I need to give the optimized size so that image will look good in div. First logic I tried to give the image with 45 by 60, 90 by 120 like. What is the correct way to solve this.Please guide.Thanks in advance.

like image 545
user3738016 Avatar asked Dec 16 '25 14:12

user3738016


2 Answers

div {
  width: 160px;
  height: 145px;
  overflow: hidden;
  border: 1px solid black;
}

div img {
    width: 100%;
    min-height: 100%;
}
<div>
<img src="https://i.sstatic.net/aFYTl.jpg?s=328&g=1"/>
</div>
like image 90
Pedram Avatar answered Dec 19 '25 05:12

Pedram


Best thing is the following:

#div-to-contain-image img {
    display: block;
    height: auto;
    min-width: 100%;
    width: 100%;
}

This will render the image the best as possible. If you need to cover the containing div entirely, you could do the following:

#div-to-contain-image img {
    display: block;
    height: 100%;
    width: 100%;
}
like image 20
Jeroen Bellemans Avatar answered Dec 19 '25 05:12

Jeroen Bellemans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!