Fit div size to background image

后端 未结 3 970
你的背包
你的背包 2020-12-03 23:41

I\'m trying to set the size (both width and height) of a div to match it\'s background image size, but I can\'t get it working. The background image size has to be in percen

3条回答
  •  悲哀的现实
    2020-12-04 00:26

    tag cannot adapt to background-image size, you need to use an tag and choose between height: auto for the div or javascript

    // **** Problem ****
    
    // Wrong html :
    
    // with css : .my_class { width: 100%; height: 100%; background-image: url(/images/my-image.jpg); background-size: 100%; background-position: center; background-repeat: no-repeat; } //**** Solution **** // use css: .my_class { width: 100%; height: 100%; background-image: url(/images/my-image.jpg); background-size: contain; }

提交回复
热议问题