Automatically Resize A YouTube Embed To Fit In Another Element
October 2024
This is how I'm using the basic YouTube embed iframe and wrapping it in a that makes it responsive.
HTML
<div class="example-wrapper">
<iframe
width="560"
height="315"
src="https://www.youtube.com/embed/REPPgPcw4hk"
title="YouTube video player"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen></iframe>
</div>
Output
CSS
.example-wrapper {
position: relative;
padding-bottom: 56.25%;
height: 0;
}
.example-wrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}