I have a CSS problem that I don't know how to solve. The situation is as follows. I have a <div> which is a flex-item, and which stretches vertically so that it fills the available height of its flex-container. Depending on the size of the browser window this <div> is a rectangle, sometimes taller than wide, sometimes wider than tall.
Now I want to position another <div> inside of this rectangle, which shall be a square. It should be as large as possible with respect to the surrounding rectangle, but it must always be visible entirely.
The typical approach to create a square is to set the padding-bottom to a percent value (which is the same percent value as for width). This actually creates a square, but since in this approach the height always follows the width, it stretches the out rectangle if it's wider than tall.
How can I solve this issue, so that the square is always contained within the boundaries of the size of the rectangle that was calculated by flexbox, ideally without using JavaScript?
Update: Meanwhile, I solved it using JavaScript, but I'd still be interested in a pure CSS solution, if there is any.