I have a fluid image inside a div, under the image there is an other div with text. Is it possible for the #text div to get same size like the image?
<div id="container">
<img src="http://dummyimage.com/300x200/c7c1c7/fff.jpg">
<div id="text">Several standard dimensions are included in dummyimage.com including ad sizes and screen resolution sizes.</div>
</div>
#container {
display: inline-block;
height: auto;
max-width: 100%;
position: relative;
}
img {
max-width:100%; max-height:100%;
}
FIDDLE
How to fit image without stretching and maintain aspect ratio? Answer: If you want to use the image as a CSS background, there is an elegant solution. Simply use cover or contain in the background-size CSS3 property. contain will give you a scaled-down image.
Seeing as the #container
is relatively positioned, you could position the #text
absolutely, to prevent it from stretching the #container
:
#text {
/* remove from flow, prevents from stretching container */
position: absolute;
}
http://jsfiddle.net/HUsRm/5/
This will, of course, also prevent vertical stretching, and not push following content down. This might be unwanted?
You can make this work by having only this css:
#container
{
display:table-caption;
}
Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With