The skinny: Is there a way to make max-height on the image obey the fixed height of an ancestor that is not its direct parent?
I understand that max-height
needs a fixed height to calculate from, so this is not a duplicate of questions asking why max-height doesn't magically work.
I have an element (.rightbox
) that has 100% height and is absolutely positioned inside an auto-height container. This element conforms to the expected height.
That element then has a child with a max-height of 100%. This element also conforms to the expected height, since the .rightbox
parent has a set height (% + absolute positioning)
But now... I have an image inside that container... and it doesn't conform to the max-height. I would think that having an ancestor with a calculable height would allow that element to conform to the max-height.
Max-width works, and I believe it's using that same ancestor to calculate width.
Setting a px height on .rightbox
has no effect.
Removing the div between the .rightbox
and the img
causes the img to conform to the desired max-height.
*{
box-sizing:border-box;
margin:0;
padding:0;
}
.item{
width: 100%;
position:relative;
background:#0FF;
}
.rightbox::after{
content:"";
display:inline-block;
height:100%;
width:0;
vertical-align:middle;
}
.rightbox>div{
display:inline-block;
vertical-align:middle;
}
.rightbox{
position:absolute;
height:100%;
width:50%;
top:0;
right:0;
}
.rightbox>div,
.rightbox img{
max-height:100%;
max-width:100%;
}
.rightbox img{
opacity:0.7;
}
<div class="item">
<div>
<p>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
Some text to make this bigger.
</p>
</div>
<div class="rightbox">
<div>
<img src="http://via.placeholder.com/550x900" />
</div>
</div>
</div>