
Shout out to Una where I first saw this and boy howdy did it strike interest in folks. Just dropping aspect-ratio on an element alone will calculate a height based on the auto width. Without setting a width, an element will still have a natural auto width. So the height can be calculated from the aspect ratio and the rendered width.el Demo If the content breaks out of the aspect ratio, the element will still expand. The aspect ratio becomes ignored in that situation, which is actually nice. That’s why the pseudo-element tactic for aspect ratios was popular, because it didn’t put us in dangerous data loss or awkward overlap territory when content got too much.īut if you want to constrain the height to the aspect ratio, you can by adding a min-height: 0 : Demo If the element has either a height or width, the other is calculated from the aspect ratio. So aspect-ratio is basically a way of setting the other direction when you only have one. Demo If the element has both a height and width, aspect-ratio is ignored. The combination of an explicit height and width is “stronger” than the aspect ratio. There is always a little tension between width, min-width, and max-width (or the height versions). One of them always “wins.” It’s generally pretty intuitive. If you set width: 100px and min-width: 200px then min-width will win. So, min-width is either ignored because you’re already over it, or wins. Same deal with max-width: if you set width: 100px and max-width: 50px then max-width will win. So, max-width is either ignored because you’re already under it, or wins. It looks like that general intuitiveness carries on here: the min-* and max-* properties will either win or are irrelevant. And if they win, they break the aspect-ratio.
