How to Embed Responsive YouTube Video Using Pure HTML and CSS

To get the traffic on your page from all the devices your page must be responsive/friendly to every devices. While embed the youtube videos you see that's it is not responsive to every devices. So I have shared the Trick to make the embed youtube videos responsive.

HTML:
Wrap Your embed code with <div>.

<div class="videoWrapper">
<!-- Copy & Pasted from YouTube -->
<iframe allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="349" src="https://www.youtube.com/embed/code? width="560"></iframe>
</div>

CSS:
We are adding the CSS padding trick to maintain the video aspect ratio for all the devices.

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

As we know the most of youtube videos are in 16:9 aspect  ratio. So by dividing the Height by width(H/W) we can find the padding. Therefore "56.25%" is the perfect no. for making the video responsive.

Result:
Here is the result. Now Resize your browser window and check the responsiveness.

Post a Comment

Previous Post Next Post