CSS Display an Image Resized and Cropped

前端 未结 19 1584
时光取名叫无心
时光取名叫无心 2020-11-22 08:09

I want to show an image from an URL with a certain width and height even if it has a different size ratio. So I want to resize (maintaining the ratio) and then cut the imag

19条回答
  •  渐次进展
    2020-11-22 08:55

    Did you try to use this?

    .centered-and-cropped { object-fit: cover }
    

    I needed to resize image, center (both vertically and horizontally) and than crop it.

    I was happy to find, that it could be done in a single css-line. Check the example here: http://codepen.io/chrisnager/pen/azWWgr/?editors=110


    Here is the CSS and HTMLcode from that example:

    .centered-and-cropped { object-fit: cover }

    original

    Bear

    object-fit: cover

    Bear

提交回复
热议问题