I'm using the trick from https://stackoverflow.com/a/6615994 to get a div where the height will equal the percentage width. The structure looks like this :
#container {
display: block;
width: 200px;
height: 200px;
position: relative;
border: 2px dashed blue; /* just for demo */
}
#icon-container {
width: 25%;
position: relative;
display: block;
/* Removing the border property causes #icon-container to have 0 height */
border: 2px dashed red;
}
.icon-spacer {
/* margin-top calculated based on width of parent */
margin-top: 100%;
}
.icon {
/* My content will be displayed as a background-image in this element */
background-color: black;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<div id="container">
<div id="icon-container">
<div class="icon-spacer"></div>
<div class="icon"></div>
</div>
</div>
While I was testing this, I ran into the following behavior : if I remove the "border" CSS from #icon-container
, then it gets a height of 0. This can be seen in the snippet here and on the fiddle here : https://jsfiddle.net/s2b4xr1a/2/
Since my real app is not going to have that border, how can I get the div to show up and have the correct height based on its children ? Also, why does this happen ?