Why is aspect-ratio CSS property inside flexbox sometimes ignored? [duplicate]
Asked Answered
A

1

11

I want to keep a 5/3 aspect ratio inside a flexbox. I want the box to be 33% of the screen height, and i want to stack two of them on top of each other with a flexbox with flex-direction set to column. This seemingly breaks the aspect ratio. If i set width instead it works again. Is there a way to do this without calculating the width with JavaScript? Should I do something else instead?

Here is a codepen demonstrating the issue. https://codepen.io/voxlz/pen/WNjPoqY

html:

<div class='parent'>
  <div class='child'></div>  
  <div class='child'></div>  
  <div class='child'></div>
</div>

css:

.parent {
  display: flex;
  gap: 20px;
  flex-direction: column;
}

.child {
  flex-grow: 0;
  
/*   this works v, aspect perserved */
/*   width: 200px; */
  
/*   this does not v */
  height: 200px; 
  
  aspect-ratio: 5/3;
  background-color: blue;
}
Arietta answered 11/8, 2021 at 10:8 Comment(1)
keep in mind that aspect-ratio is a very weak property : css-tricks.com/weak-declarationFictile
K
15

You can give align-items property to the parent then the aspect-ratio will be considered.

Keeley answered 11/8, 2021 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.