csshtmlbackgroundheight

How to get div height to auto-adjust to background size?


How do I get a div to automatically adjust to the size of the background I set for it without setting a specific height (or min-height) for it?


Solution

  • Another, perhaps inefficient, solution would be to include the image under an img element set to visibility: hidden;. Then make the background-image of the surrounding div the same as the image.

    This will set the surrounding div to the size of the image in the img element but display it as a background.

    <div style="background-image: url(http://your-image.jpg);">
     <img src="http://your-image.jpg" style="visibility: hidden;" />
    </div>