Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrapping div is not resized when inner image scales (a result of window resize)

I want my images to resize as the window height changes while keeping the containing div shrink wrapping the image. I tried using:

<div>
    <img src="http://akamaicovers.oreilly.com/images/9780596806767/cat.gif" alt="">
</div>

html, body {
    height: 100%;
    width: 100%;
}

div {
    height: 90%;
    background-color: black;
    display: inline-block;
}

img {
    height: 100%;
    width: auto;
}

But it doesn't seem to work as expected. The div doesn't shrink. It actually does once I play around with the css properties in debugger.

Here is the fiddle (try resizing the result panel)

Update:

Now this is strange. Since I first posted this question the browser behaviour changed. Originally (Chrome) when I resized the window the image would shrink proportionally as expected but the wrapping div would keep its original width. What happens now (Chrome update?) is that the image doesn't shrink horizontally, and the div also.

I tried it with the latest Safari and Firefox. Both shrink the image but keep original div width. So please be kind to check your solutions on other browsers as well.

Update #2:

The div has to stay of block type as I need to place other elements in the corners of the image.

like image 331
Xyand Avatar asked May 18 '13 06:05

Xyand


People also ask

How do I keep my div from moving when I resize windows?

first add a parent div/wrap and put those two divs of yours into it. Overall, whatever size you add to the min-width itll scale the parent div down to the size specified. once the window is sized down past your specified size, itll behave like any other window. this is my way of doing it.

How do I keep my div from resizing?

It is mostly used with textarea and div elements. To disable resizable property of an element use resize to none. It is the default value.

How do I resize a content to fit in a div?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.


2 Answers

I guess you'll have to resort to JavaScript:

$(window).on('resize', function (){ 
    $('div').width($('img').width());
});

JSFIDDLE

like image 133
Tomas Kirda Avatar answered Oct 18 '22 01:10

Tomas Kirda


You just have to keep your image max-height to be 100%. Thats it.

Here is the Working Solution

The HTML:

<div>
    <img src="http://akamaicovers.oreilly.com/images/9780596806767/cat.gif" alt="">
</div>

The CSS:

html, body {
    height: 100%;
    width: 100%;
}

div {
    height: 90%;
    background-color: black;
    display: inline;
}

img {
    max-height: 100%;
    max-width: 100%;
    height: 100%;
}

EDIT

Updated CSS for the img class to make the image fit the full div. Here is the working solution for the edit.

img {
    max-height: 100%;
    max-width: 100%;
    height: 100%;
    display:block;
}

Hope this Helps.

like image 36
Nitesh Avatar answered Oct 18 '22 00:10

Nitesh