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?

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #45485
    Anonymous
    Inactive

    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?

    #138496
    Paulie_D
    Member

    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.

    #138498
    Anonymous
    Inactive

    Or making the background image one specific width but not height so it wont stretch. will work too.

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

    #138501
    Anonymous
    Inactive

    Nevermind with this question. I found an alternative. Thank you guys anyways.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘CSS’ is closed to new topics and replies.