With the aspect-ratio
property, as you resize the card, the green visual block maintains this 16 x 9 aspect ratio. We are respecting the aspect ratio with aspect-ratio: 16 / 9
.
.video {
aspect-ratio: 16 / 9;
}
To maintain a 16 x 9 aspect ratio without this property, you'd need to use a padding-top
hack and give it a padding of 56.25%
to set a top-to-width ratio. We will soon have a property for this to avoid the hack and the need to calculate the percentage. You can make a square with 1 / 1
ratio, a 2 to 1 ratio with 2 / 1
, and really just anything you need for this image to scale with a set size ratio.
.square {
aspect-ratio: 1 / 1;
}
<div class="parent">
<div class="card">
<h1>Video Title</h1>
<div class="visual"></div>
<p>Descriptive Text goes here</p>
</div>
</div>
.parent {
display: grid;
place-items: center;
}
.visual {
aspect-ratio: 16 / 9;
}
.card {
width: 50%;
display: flex;
flex-direction: column;
padding: 1rem;
}