I have determined how to set a div height based on its width (using percentage width and padding-bottom). But I also need to be able to set a div width based on its height. I have tried using vw/vm/vh units, but those are relative to the viewport and not the container.
So, in the following example
<body>
<div class="rectangle">
<div class="square">
</div>
</div>
</body>
Here's some non-working CSS that only works in portrait:
.rectangle {
background-color:red;
position:absolute;
top:0;
left:0;
bottom:0;
right:0;
}
.square {
background-color:blue;
width:100%;
padding-bottom:100%;
}
I would like .rectangle to fill its container, meaning as the window size changes, .rectangle may go from a landscape layout to a portrait layout. When this happens, I want the .square div to fill as far as it can without losing its square shape.
So, if the browser window is wider than it is tall (landscape), the .rectangle should fill the window, and the .square should be as tall as the rectangle is tall, and as wide as the rectangle is tall.
And if the browser window is taller than it is wide (portrait), the .rectangle should fill the window, and the .square should be as wide as the rectangle is wide, and as tall as the rectangle is wide.
So, I can use media queries to swap out styles between landscape and portrait, and I have a solution for the portrait case, but I haven't found a solution to the landscape case. How do I get the square to fill its parent's height, and limit its width to maintain aspect ratio of a square?