How to maintain the aspect ratio of a div with CSS?
.ratio-4-3 {
padding-bottom: 75%;
width: 100%;
background-color: #ff0000;
}
<div class="ratio-4-3"></div>
September 8th, 2019
The padding bottom trick is helpful if you want to maintain the size of a <div>
containing a video or image with a specific aspect ratio. The math behind it is simple: height / width = padding-bottom value
aspect ratio | padding-bottom value |
---|---|
4:3 | 75% |
16:9 | 56.25% |
3:2 | 66.6% |
8:5 | 62.5 |