1

How to make that a column using flexbox has a height which automatically/dynamically matches its width?

#container { display: flex; width: 500px;   flex-direction: row; }
#a { background-color: red; width: 50%; }
#b { background-color: blue; width: 50%; height: 50%; }
<div id="container">
<div id="a">
Hello
</div>
<div id="b">
This div should be square, its height should automatically match its width, and text vertically centered
</div>
</div>

Note: I don't want to use the aspect-ratio property which is too young (not support on enough browsers for my project) nor the similar solutions of Maintain the aspect ratio of a div with CSS, but with flexbox, as mentioned on top. Also this linked question does not consider the vertical alignement either.

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 1
    You could use grid with aspect ratio – DreamTeK Feb 04 '22 at 10:11
  • @DreamTeK Thanks! Can you give an example code snippet in an answer? – Basj Feb 04 '22 at 10:16
  • Haha yes @DreamTeK. Could you paste a jsfiddle? Grid is ok but not https://caniuse.com/?search=aspect-ratio which is not supported enough for my project. Thanks! – Basj Feb 04 '22 at 10:22
  • IE11 letting you down? Would the suggestion in the linked post not be suitable? If not we can reopen yours. – DreamTeK Feb 04 '22 at 10:26
  • No it's not supported for Firefox or Chrome < 2020. This is too young for my project. – Basj Feb 04 '22 at 10:28
  • What about the post linked as duplicate? – DreamTeK Feb 04 '22 at 10:29
  • @DreamTeK There is no suitable solution with `flexbox`, all the rest of the website is already done with `display: flex;` so I need to keep this. – Basj Feb 04 '22 at 10:33
  • Without support for latest css I can only suggest falling back to Javascript. Flexbox is designed to be one dimensional array, it doesn't support two dimensions very well thats why `grid` was created. – DreamTeK Feb 04 '22 at 11:00

0 Answers0