Sizing to fit parent container, while respecting css "aspect-ratio"
I've been struggling with this for 2 hours so hopefully someone can help me out.
I have a div that I need a set aspect ratio, and am using the CSS "aspect-ratio" attribute for this. This div (white) needs to scale to fit the parent container (red), while maintaining aspect ratio.
I need my container to be scaled, not just what is shown in the viewport (i.e. no overflow-hidden). I also need this to be pure CSS, as this is in a NextJS server component.
current code:
Solution:Jump to solution
ok, solution (for minimal example) was
aspect-square h-[min(100vh,100vw)]
for my original problem (not shown here) i had to do:
aspect-square h-[min(50vw-24px,100vh-210px)]
(50vw since there are 2 side by side, and the arbitrary px values guessed/checked until it matched parent) ...3 Replies
need it to look like:
(and on parent container resize):
Here's a minimal example stackblitz:
https://stackblitz.com/edit/stackblitz-starters-zhkpve?file=index.html
Solution
ok, solution (for minimal example) was
aspect-square h-[min(100vh,100vw)]
for my original problem (not shown here) i had to do:
aspect-square h-[min(50vw-24px,100vh-210px)]
(50vw since there are 2 side by side, and the arbitrary px values guessed/checked until it matched parent)
if trying to do any aspect ratio other than a square, obviously need to adjust away from 100 and use your 1:decimal ratio