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

后端 未结 3 1474
梦毁少年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:19

    You can use background-size: 100% 100% for this:

    div {
      margin-bottom: 1em;
      width: 300px;
      height: 200px;
      border: 3px solid black;
      background: url(https://unsplash.it/100x100);
      background-repeat: no-repeat;
      background-size: 100% 100%;
    }
    
    div:nth-of-type(2) {
      width: 100px;
      height: 400px;
    }
    
    div:nth-of-type(3) {
      width: 500px;
      height: 200px;
    }

提交回复
热议问题