This is the strangest thing, I have used this method many times before but it seems to be breaking for me now.
Here is a duplicate question that provides this method as an answer:
Maintain the aspect ratio of a div with CSS
But for some unknown reason it's breaking for me in Firefox and Chrome. From what I can gather it's calculating the padding according to the parent of the element I'm applying the style to...
In this case it's not looking at .test but instead is looking at .parent to calculate the padding:
.parent {
width: 200px;
}
.test {
width: 50px;
background: red;
padding-top: 100%;
}
<div class='parent'>
<div class='test'></div>
</div>
That is actually the correct behaviour. You are meant to make the child 100% width and control the sizing with the parent.
Example:
Here is a nice method proposed on CSS-Tricks that helps you get the correct padding you need for the desired ratio:
Aspect Ratio - 2:1
Aspect Ratio - 3:1
Aspect Ratio - 16:9
You can find the full article here: https://css-tricks.com/aspect-ratio-boxes/