How to embed responsive YouTube videos with CSS

Below is the method that I use to embed responsive or fluid YouTube videos. This is based on an article by Thierry Koblentz with some modifications. This method also works for other video sources, but this article and the changes I made are specifically for YouTube videos. (Featured video: "Middle" by DJ Snake ft. Bipolar Sunshine.)

To use this method insert the following pieces of CSS code in your stylesheet. Here I use "video-frame" as the class name but you can use "videoWrapper", "video-container", "myVideo" or any other name as long as it is unique in your stylesheet.

Your HTML will then look like this:

The original article uses a "padding-bottom" value of 56.25%, which is based on the 16:9 aspect ratio. But through trial and error I've found that a value of 58% produces a better and more consistent look at different screen sizes. I also added "border: 0" to remove annoying borders that appear on the top and left sides of the video. I have verified that this method works on the current versions of Firefox, Chrome, Internet Explorer, and Edge.

Labels: ,

« Older PostBack to TopNewer Post »