Resize a div element to its background image size

前端 未结 6 606
忘了有多久
忘了有多久 2021-01-04 11:24

Is it possible to make a

adapt to its background image? I mean to maintain the proportions width and height.

Clarification:

6条回答
  •  礼貌的吻别
    2021-01-04 11:58

    Maybe using javascript, you can get the url to the image:

    var img = document.getElementById('image').style.backgroundImage; 
    var width = img.clientWidth;
    var height = img.clientHeight;
    document.getElemetById('image').style.width = width;
    document.getElemetById('image').style.height = height;
    

    I strongly suspect you cannot do this exclusively with stylesheets.

提交回复
热议问题