Well, I found a unique hack, although this is only a half-answer (it may answer some cases). Maybe someone who has some more wit can expand it and find a complete solution. :-)
Based on the fact that <img> tags retain their proportion when scaling only one dimension, I put together a test into which a 1x1 spacer is inserted and scales it to fit the height.
It works well. Unfortunately, the disadvantage is that the image must be contained in an element that is related to the content generating the height, and the width of the actual element with the content does not change.
That way, if you can duplicate your content, this might work.
Here's a JSFiddle to demonstrate this. And, here is the commented code:
<style type="text/css"> #outer { position:relative; } #box { background-color:red; position:absolute; height:100%; display:block; z-index:-1; } img { height:100%; } #inner { position:absolute; top:0; bottom:0; left:0; right:0; } .centered { text-align:center; margin-top:50%; transform:translateY(-50%); } #box-text { color:white; } #height-box { display:inline-block; } </style> <div id="outer"> <div id="box"> <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"> <div id="inner"> <p id="box-text" class="centered">Box</p> </div> </div> <div id="height-box" contenteditable="true">is<br>this<br>actually<br>possible?</div> </div>
source share