r/csshelp • • Jul 29 '24

Request Square aspect ratio within parent with implicit height

Here is the end result I am trying to achieve

A (the red div) and B (the blue div) are in a container div. The container's height is implicit and dictated by A's content. B needs to take up the height of the container AND be a square.

I came up with this solution with grid and aspect-ratio:

<style>
  .container {
    display: grid;
    grid-template-columns: 1fr auto;
    width: 100%;
  }
  .divA {
    background-color: lightcoral;
  }
  .divB {
    background-color: lightblue;
  }
  .square {
    height: 100%;
    aspect-ratio: 1 / 1;
  }
</style>
<div class="container">
  <div class="divA">
    A<br>A<br>A<br>A<br>A
  </div>
  <div class="square divB">
    B
  </div>
</div>

This works completely as expected in Chrome/ium (in fact, the screenshot above is exactly the result of this in Chrome and Arc).

HOWERVER, this does NOT work in Safari nor Firefox: B does take the height of the parent, but the aspect-ratio is not a square.

What can I do ? Many thanks in advance !

1 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/n0tKamui Jul 29 '24

now i understand the flex part, but i’m comfy about the need for the unconfigured grid