- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘CSS’ is closed to new topics and replies.
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?
I have a div with a background image that is changed with Jquery. I want the div to resize itself when according to the size of its background image. If the background image is small than it shrinks to that size. Is this possible in any way?
If you can define the bg image sizes as variables in the JS then I suppose so but it’s not something that would be detected automatically.
Certainly not by CSS.
Or making the background image one specific width but not height so it wont stretch. will work too.
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);
Nevermind with this question. I found an alternative. Thank you guys anyways.