Scale image inside div up AND/OR down to fit largest side of image with CSS

独自空忆成欢 提交于 2019-12-06 13:46:05

Here's one way to do it.

Apply the image as an css background using background: url(path/to/image) and then using the background-size attribute to set the scaling to contain, background-size: contain (see fiddle, https://jsfiddle.net/mghgsk5e/).

For all modern browsers except Edge, and all versions of Internet Explorer, you can use the CSS property object-fit.

Example: https://jsfiddle.net/09cpe9e0/1/

I would be inclined to implement a javascript solution for Internet Explorer and Edge that can be removed at a later date, leaving only the CSS version when it is safe to do so.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!