TL;DR
The square
class should be a square in all testcases.
The following html includes three testcases.
All testcases contain a flex-box which holds three divs. The div in the middle has a square
class and (as the name suggests) should be a square in all testcases. The square
should fill up as much space of the container as possible, but shouldn't exceed it of course.
I already found a solution here, but it fails on the second and third testcase.
The solution shouldn't include javascript if possible and shouldn't use aspect-ratio
if possible, because it has bad browser support currently.
Any help is gratefully appreciated.
.container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
background-color: red;
}
.simple {
width: 100%;
background-color: green;
}
.square {
background-color: yellow;
}
<!-- 1. Testcase -->
<div class="container" style="width: 200px; height: 400px">
<div class="simple">Hello</div>
<div class="square">World</div>
<div class="simple">Hello</div>
</div>
<!-- 2. Testcase -->
<div class="container" style="width: 400px; height: 200px">
<div class="simple">Hello</div>
<div class="square">World</div>
<div class="simple">Hello</div>
</div>
<!-- 3. Testcase -->
<div class="container" style="width: 200px; height: 200px">
<div class="simple">Hello</div>
<div class="square">World</div>
<div class="simple">Hello</div>
</div>