Automatic height when embedding a YouTube video?

If you go for the entire viewport you can use following code:

iframe{
  width: 100vw;
  height: calc(100vw/1.77);
}

This article contains a good answer, copied below.

CSS:

.video-container {
  position: relative;
  padding-bottom: 56.25%; /* 16:9 */
  height: 0;
}
.video-container iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

Example Html

<div class="video-container">
    <iframe width="560" height="315" src="https://www.youtube.com/embed/_TyJeKKQh-s?controls=0" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

How this works: The container element is given a zero height and a percentage bottom padding. The percentage bottom padding is a percentage of the container width, so that gives it a fixed aspect ratio. But in order to get the iframe to show up inside the zero-height container, you need to make the container relative and the iframe absolute, positioned inside the div.

See a live example.


EDIT: If you need to use percentage width other than 100%, multiply the ratio by the width multiplying factor. See the example below:

.video-container {
  position: relative;
  padding-bottom: calc(56.25% * 0.75); /* 16:9 */
  width: 75%;
  height: 0;
}