I’m having a weird issue when trying to center an element that has aspect-ratio applied. I’d assume it would behave similarly to how centering an image works but I keep ending up with an element of 0px x 0px.
https://codepen.io/richardcool/pen/xxeKOwp
I have created a basic CodePen to show the issue. If I uncomment the align-items: center
line then the element becomes 0px x 0px. I have also tried centering using absolute positions and get the same issue.
Any thoughts?
2
Answers
For aspect ratio to work properly with alignment like flex, you need to specify an actual
width
orheight
on the element, otherwise there is no base value for CSS to calculate with, even if you specifymax-width
.Set the poster div to a width of 100%. Your max-width will override it, but then you’ll be able to vertically center it with
align-items: center
on the parent.As long as there is no content in your div that determines the size, you have to specify either a width or a height when using
aspect-ratio
andalign-items: center
. For example like this: