How can I make an image fit a whole div (resizing the image if necessary)?
Asked
Active
Viewed 45 times
-3
-
Question is duplicated: https://stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit-a-div-container – Pepv Oct 09 '19 at 17:20
-
Possible duplicate of [How do I auto-resize an image to fit a 'div' container?](https://stackoverflow.com/questions/3029422/how-do-i-auto-resize-an-image-to-fit-a-div-container) – Gal Silberman Oct 09 '19 at 17:23
3 Answers
1
yes, You can use
width: auto;
height: 400px;
object-fit: cover;
Instead of cover you can use contain, fill, etc as per your choice.

j08691
- 204,283
- 31
- 260
- 272

Rutvik Gumasana
- 1,458
- 11
- 42
- 66
0
You cannot always display with the same dimensions Because your container may be other sizes But the normal way is to set the height to 100% and hide it overflow Or vice versa width: 100%
or use object-fit: hover same as:
img {
width:100%;
height:100%;
object-fit:cover;
}

Alireza Hosseinitabar
- 88
- 12
0
If you provide the height OR width of DIV in %, then the div will be expanded as per the size of the image but if you provide the width and height of the div in pixle "px" then the div will have fixed height and width.

Anurag
- 843
- 1
- 9
- 16