{"id":304927,"date":"2020-03-11T07:10:16","date_gmt":"2020-03-11T14:10:16","guid":{"rendered":"https:\/\/css-tricks.com\/?p=304927"},"modified":"2020-03-11T07:10:18","modified_gmt":"2020-03-11T14:10:18","slug":"fluid-width-video","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/fluid-width-video\/","title":{"rendered":"Fluid Width Video"},"content":{"rendered":"\n

IN A WORLD of responsive and fluid layouts on the web, ONE MEDIA TYPE stands in the way of perfect harmony: video. There are lots of ways in which video can be displayed on your site. You might be self-hosting the video and presenting it via the HTML5 <video><\/code> tag. You might be using\u00a0YouTube,\u00a0Vimeo, or some other video provider\u00a0that provides <iframe><\/code> code to display videos. Let’s cover how to make them all fluid width while maintaining an appropriate height based on their aspect ratio.<\/p>\n\n\n\n\n\n\n\n

In each of these video-embedding scenarios, it is very common for a static width<\/code> and height<\/code> to be declared.<\/p>\n\n\n\n

<video width=\"400\" height=\"300\" controls ... ><\/video>\n\n<iframe width=\"400\" height=\"300\" ... ><\/iframe>\n\n<!-- maybe even super old school -->\n<object width=\"400\" height=\"300\" ... \/>\n<embed width=\"400\" height=\"300\" ... \/><\/code><\/pre>\n\n\n\n

Guess what? Declaring static widths isn’t a good idea in fluid width environments. What if the parent container for that video shrinks narrower than the declared 400px<\/code>? It will bust out and probably look ridiculous and embarrassing.<\/p>\n\n\n\n

\"breakout\"
Simple and contrived, but still ridiculous and embarassing.<\/figcaption><\/figure>\n\n\n\n

So can’t we just do this?<\/p>\n\n\n\n

<video width=\"100%\" ... ><\/video><\/code><\/pre>\n\n\n\n

Well, yep, you can! If you are using standard HTML5 video, that will make the video fit the width of the container. It’s important that you remove the height<\/code> declaration when you do this so that the aspect ratio of the video is maintained as it grows and shrinks, lest you get awkward “bars” to fill the empty space (unlike images, the actual video maintains it’s aspect ratio regardless of the size of the element). <\/p>\n\n\n\n

You can get there via CSS (and not worry about what’s declared in the HTML) like this:<\/p>\n\n\n\n

video {\n  \/* override other styles to make responsive *\/\n  width: 100%    !important;\n  height: auto   !important;\n}<\/code><\/pre>\n\n\n\n