I am trying to implement a layout similar to this:
.
The requirement I have is to create two adjacent boxes: one that contains multiline, dynamic, text and the other that must be a square whose side is determined by its sibling’s height.
The problem is that i am not able to set the dimensions of the square based on the implicit height of the parent / sibling.
I tried to put the two div
s (square+text) in a flex
container and to set the aspect-ratio
of the square div
to 1, but even if the align-items
property on the parent is set to stretch
the height is not taken in to account when calculating the ratio.
* {
box-sizing: border-box;
}
body {
font-family: system-ui;
margin-left: 2rem;
}
.wrapper {
display: flex;
border: 1px solid black;
width: fit-content;
}
.square {
width: 100px;
aspect-ratio: 1;
background-color: #ffec99;
border: 1px solid black;
}
.text {
padding: 0 1rem;
}
.text :last-child {
margin-bottom: 2rem;
}
<div class="wrapper">
<div class="square"></div>
<div class="text">
<h1>Some text of different sizes</h1>
<p>Some other text that may be present</p>
</div>
</div>
Here is a link of a codepen with the setup.
2
Answers
Use
.wrapper { display: grid; }
and.square { height: 0; min-height: 100%; }
trick:It auto adjusts to screen size and expands depending on the
amount of text.