Make image fill div completely without stretching

后端 未结 13 1721
情话喂你
情话喂你 2020-11-30 04:22

I have large images of varying dimensions that need to completely fill 240px by 300px containers in both dimensions. Here is what I got right now, which only works for one d

13条回答
  •  抹茶落季
    2020-11-30 04:45

    For anyone looking to do this that doesn't have dynamic images, here's an all-CSS solution using background-image.

    The "background-size: cover" makes it so that the image scales to cover all of the div while maintaining the aspect ratio. The CSS could also be moved to a CSS file. Although if it's dynamically generated, the background-image property will have to stay in the style attribute.

提交回复
热议问题