Something is tormenting me for a long time now. I just can't find any way to make images fit it's parent's height.
I went trough a lot of questions here, but none gave me the answer I need.
Some of these questions are:
CSS same height as Parent
Fit image to parent div's size
How can I resize an image dynamically with CSS as the browser width/height changes?
CSS: How can I set image size relative to parent height?
CSS: 100% width or height while keeping aspect ratio?
Make an image to fit it's parent dimensions
CSS: How can I set image size relative to parent height?
Now, the problem is that whenever I insert an image to a parent element, the parent just doensn't strech in height.
An image:
http://es.tinypic.com/view.php?pic=2s1u1ec&s=9#.WLnbWvKE1jk
I'm trying to build an image slider, and the img must be absolutely positioned.
Red border should wrap the image, as well as the parent should wrap the entire image plus the padding.
Some code:
HTML:
<section>
Gray: parent
<div class="level0">
<div class="container">
<img src="1.jpg" alt="image">
</div>
</div>
</section>
CSS:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
html, body {
width: 100%;
height: auto;
background-color: #565656;
}
section {
position: relative;
width: 100%;
max-width: 1300px;
height: 100%;
margin: 0 auto;
padding: 1%;
background-color: #a1a4a8;
}
.level0 {
position: relative;
width: 100%;
height: 100%;
}
.container {
position: relative;
width: 100%;
height: 100%;
border: 4px solid red;
}
.level0 img {
position: absolute;
width: 100%;
}
I tried overflow: hidden, max-height, %, etc. The only thing that actually works is setting a fixed height to the parent element, but that's not good for me. I want the parent to automatically strech it's height deppending on the image's width.
Oh, one more thing. I read several JQuery, JS, or similar answers, but I would appreciate pure CSS answers. Obviously trere must be something I'm missing.
Please let me know if there is anything confusing. Thanks for the help!