Home › Forums › CSS › Is there a way to automatically re-size div according to the size of its background image? › Re: Is there a way to automatically re-size div according to the size of its background image?
June 12, 2013 at 4:10 pm
#138499
Jozsef K.
Participant
With javascript you can load the background image as
var bg=new Image;
bg.src='path/x.jpg'
$(bg).load(function() {
var bgW=bg.width;
var bgH =bg.height;
$('div.box').width(bgW).height(bgH);
})
Or you could just do another way around, using background size to fit the div’s dimensions (background-size:contain);