{"id":255404,"date":"2017-06-08T08:47:49","date_gmt":"2017-06-08T15:47:49","guid":{"rendered":"http:\/\/css-tricks.com\/?p=255404"},"modified":"2021-03-09T15:47:17","modified_gmt":"2021-03-09T23:47:17","slug":"aspect-ratio-boxes","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/aspect-ratio-boxes\/","title":{"rendered":"Aspect Ratio Boxes"},"content":{"rendered":"\n
I had a little situation the other day where I needed to make one of those aspect-ratio friendly boxes<\/strong>. This isn’t particularly new stuff. I think the original credit goes as far back as 2009 and Thierry Koblentz’s Intrinsic Ratios<\/a> and maintained popularity even for other kinds of content with articles like Uncle Dave’s Ol’ Padded Box<\/a>.<\/p>\n\n\n\n Let’s go on a little journey through this concept, as there is plenty to talk about.<\/p>\n\n\n\n\n\n\n Even when that is a little unintuitive, like for vertical padding. This isn’t a hack, but it is weird: Isn’t that a perfect square, 500px \u00d7 500px? Yes, it is! An aspect ratio!<\/p>\n\n\n\n If we force the height of the element to zero ( Now imagine instead of 100% top padding, we used 56.25%. That happens to be a perfect 16:9 ratio! (9 \/ 16 = 0.5625).<\/p>\n\n\n\nThe Core Concept: padding in percentages is based on width<\/h3>\n\n\n
padding-top<\/code> and
padding-bottom<\/code> is based on the parent element’s
width<\/code>. So if you had an element that is 500px wide, and
padding-top<\/code> of 100%, the
padding-top<\/code> would be 500px.<\/p>\n\n\n\n
height: 0;<\/code>) and don’t have any borders. Then padding will be the only part of the box model affecting the height, and we’ll have our square.<\/p>\n\n\n\n