Contain an image within a div?

后端 未结 7 805
北荒
北荒 2020-12-13 00:08

I want to contain an image into a 250 x 250 div, forcing the image to resize but not stretch. How can I do this? By the way, the image will usually be bigger than the div it

相关标签:
7条回答
  • 2020-12-13 00:49

    Since you don't want stretching (all of the other answers ignore that) you can simply set max-width and max-height like in my jsFiddle edit.

    #container img {
        max-height: 250px;
        max-width: 250px;
    } 
    

    See my example with an image that isn't a square, it doesn't stretch

    0 讨论(0)
提交回复
热议问题