CSS: How to force background image to stretch/compress to fit div, without keeping aspect ratio

后端 未结 3 1472
梦毁少年i
梦毁少年i 2021-01-21 01:43

I\'m having a frustrating issue, and would love some help.

I\'ve written a script to allow me to resize a div on screen for an application I\'m building for a client, b

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-21 02:22

    Here's a re-sizeable example using background:

    div{
      resize:both;
      overflow:hidden;
      width:200px;
      height:300px;
      background:url(https://picsum.photos/200/300)no-repeat 0 0;
      background-size:100% 100%;
    }

    Here's another using img:

    div{
      resize:both;
      overflow:hidden;
      height:300px;
      width:200px;
    }
    img{
      width:100%;
      height:100%;
    }

提交回复
热议问题