Expanding upon the padding top/bottom technique, it is possible to use a pseudo element to set the height of the element. Use float and negative margins to remove the pseudo element from the flow and view.
This allows you to place content inside the box without using an extra div and/or CSS positioning.
.fixed-ar::before {
content: "";
float: left;
width: 1px;
margin-left: -1px;
}
.fixed-ar::after {
content: "";
display: table;
clear: both;
}
/* proportions */
.fixed-ar-1-1::before {
padding-top: 100%;
}
.fixed-ar-4-3::before {
padding-top: 75%;
}
.fixed-ar-16-9::before {
padding-top: 56.25%;
}
/* demo */
.fixed-ar {
margin: 1em 0;
max-width: 400px;
background: #EEE url(https://lorempixel.com/800/450/food/5/) center no-repeat;
background-size: contain;
}
1:1 Aspect Ratio
4:3 Aspect Ratio
16:9 Aspect Ratio