freecoding.school100% FREE · NO SIGNUP
Neon DistrictISSUE #19 of 50

aspect-ratio · holding shape

PrismaVSJank the Janitor
Prisma saysaspect-ratio reserves a width:height proportion so boxes (and media) hold their shape.

aspect-ratio: 16 / 9 sizes an element to a proportion: set the width and the height follows. It prevents layout shift for images/iframes before they load and keeps cards and video embeds proportioned without padding-hack tricks.

Power-ups you unlock

Jank the Janitor attacks — common mistakes

Boss battleMake a responsive 16:9 video wrapper with aspect-ratio instead of the padding hack.

Example code

<!doctype html><html><head><meta charset="utf-8">
<style>.v{aspect-ratio:16/9;width:100%;max-width:360px;background:#140a28;display:grid;place-items:center;color:#e6e0ff}</style></head><body style="background:#06040d;padding:20px"><div class="v">16:9, no padding hack</div></body></html>
▶ Open the interactive comic issue
‹ Object-Position · FramingPseudo-Classes · :Hover · :Focus · :Active ›