Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

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?

#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);