Accessibility Tools

Skip to main content
© Romankoff Development
Account
adaptive YouTube video on a computer, tablet and smartphone

In today's world, video has become an integral part of websites. Internet users consume video content more and more often, so developers have an important task to make videos on the site as convenient and accessible as possible. One of the most popular services for downloading and playing videos is YouTube. In this article, we'll look at how to make a responsive YouTube video on your site using CSS.

Using responsive video on your site is important because it allows your visitors to view your content on any device, from computers to smartphones. Responsiveness of the video means that it will automatically adapt to the size of the screen, ensuring optimal viewing regardless of the device.

Embed YouTube videos

First of all, you need to embed a YouTube video on your website. To do this, copy the link to the video from YouTube and paste it into your web page. This can be done using the embed code that YouTube provides for each video. However, add a class for the block div, namely for youtube.

As a result, your video code should look something like this:

<div class="youtube">
<iframe width="560" height="315" src="https://www.youtube.com/embed/9bZkp7q19f0?si=OnWY9eOF2HHSgMNl" frameborder="0" allowfullscreen></iframe>
</div>

Making video responsive with CSS

One of the easiest ways to make your video responsive is to use CSS styles. You can create an appropriate CSS class for the video container and set it with responsive properties.

Add this CSS code to your site's style files:

.youtube {
	position: relative;
	padding-bottom: 56.25%;
	height: 0;
	overflow: hidden;
  }
  .youtube iframe {
	position: absolute;
	top: 0;
	left: 0;
	width: 100% !important;
	height: 100% !important;
	border-width: 0;
	outline-width: 0;
  }

In this example, we're setting the dimensions of the video container using percentages. What some properties are responsible for is shown in this table:

Table of CSS properties and their value descriptions for responsive video
PropertyValue description
padding-bottom Sets the proportional indentation to ensure that the aspect ratio of the video is maintained
position: relative Allows us to position nested elements relatively
position: absolute Place the video exactly in the positions we need

As a result, we get a responsive video container that changes depending on the screen size. By the way, I think you already guessed that this way you can embed any video, not just YouTube 😉

Conclusion

Responsive YouTube video on a website is an important feature that allows your visitors to conveniently view video content regardless of the device. Thanks to the use of CSS styles, you can create a responsive video container that changes according to the screen size. This will ensure that your video is viewed optimally on any device, which will increase the usability of your website as a whole. Understanding and using video responsiveness on your website is an important step in improving your web design and user experience.

Comments: 0

Only logged in users can comment