Responsive video iframes (keeping aspect ratio) with only CSS? - html

I usually use a similar solution to this one. Something like:
.wrapper {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%;
}
.wrapper iframe {
position:absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
}
But this time I have no access to the HTML or JavaScript code so I can't use a wrapper to prevent the height:0.
Is there a way to make an iframe responsive (and to keep the ratio) with only CSS?
Tried this (works with the iframe but not with its content):
iframe {
width: 100%;
background: red;
height: 0;
padding-bottom: 33%;
}
fiddle
Any thoughts? No need to support old browsers so even a CSS3 solution would be great.

Here is a Fiddle for a solution, that is based on a CSS2 secret: https://jsfiddle.net/59f9uc5e/2/
<div class="aspect-ratio">
<iframe src="" width="550" height="275" frameborder="0"></iframe>
</div>
<style>
/* This element defines the size the iframe will take.
In this example we want to have a ratio of 25:14 */
.aspect-ratio {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* The height of the item will now be 56.25% of the width. */
}
/* Adjust the iframe so it's rendered in the outer-width and outer-height of it's parent */
.aspect-ratio iframe {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
</style>
It is explained by how percentage-values for padding are handled:
The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'.
https://www.w3.org/TR/CSS2/box.html#padding-properties

Use the new CSS viewport units vw and vh (viewport width / viewport height)
FIDDLE
iframe {
width: 100vw;
height: 56.25vw; /* 100/56.25 = 560/315 = 1.778 */
background:red;
}
Browser support is also good: IE9+ (caniuse)

Calc function makes it much more readable:
.iframe-video {
width: 755px;
height: 424px;
max-width: 100%;
max-height: calc((100vw - 40px) / (16/9));
}
width and height is size for desktop and also fallback to ancient browsers
40px is margin (20 px between iframe border and viewport border on both sides)
16/9 is ratio of video (if you have edge-to-edge player)

With css aspect-ratio it's easy.
iframe {
height: auto;
width: 100%;
aspect-ratio: 16 / 9;
}

<div>
<div style="position:relative;padding-top:56.25%;">
<iframe src="https://www.youtube.com/embed/nckseQJ1Nlg" frameborder="0" allowfullscreen
style="position:absolute;top:0;left:0;width:100%;height:100%;"></iframe>
</div>
</div>
Please add this styling to the web url content you are trying to load. It will keep the 16:9 aspect ratio.

This is kind of hackish however you can use images to preserve the aspect ratio of a video. For example I went to paint and saved an image of 1280 x 720 resolution to use for a 16:9 aspect ratio (in here I will just grab a blank image off the internet somewhere).
This works because if you change the width of an image while leaving height auto, vise-versa the image will automatically scale - keeping proportions.
img {
display: block;
height: auto;
width: 100%;
}
iframe {
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
.wrapper {
float: left;
position: relative;
}
<div class="wrapper">
<img src="http://www.solidbackgrounds.com/images/1280x720/1280x720-ghost-white-solid-color-background.jpg" alt=""/>
<iframe width="560" height="315" src="https://www.youtube.com/embed/HkMNOlYcpHg" frameborder="0" allowfullscreen></iframe>
</div>

I had the same issue and this worked for me:
.wrapper iframe{
width: 100%; /* This Forces video to 100% of parent's width */
height:unset; /* This Overwrites the height attribute of Youtube's embed code */
aspect-ratio: 16 / 9; /* This adjusts video height to keep the desired aspect ratio*/
}

Related

Set height as a ratio of width with only css

I want to achieve the following for an <img> element in HTML, using only CSS:
width: calc(100% - 20px)
height: calc(width * 0.5625) /*16:9 aspect ratio*/
There are similair examples all over the internet regarding <div> elements and their background. But in the case of <img> elements, changing the padding does not work
Similair example: Maintain the aspect ratio of a div with CSS
Edit, using jQuery one can achieve the above with:
$(".myImage/s").outerHeight($(".myImage/s").outerWidth() * 0.5625);
CSS has a built-in property called aspect-ratio just assign it to the element after height or width has been defined. CSS-tricks has an example and I made a code snippet below.
div{
width:50vw;
border: 2px solid black;
border-radius: 15px;
margin:5px;
aspect-ratio: 16/9;
}
<div>
</div>
Use viewport-width (vw) for defining width in the height property:
width: calc(100% - 20px)
height: calc((100vw - 20px) * 0.5625) /*16:9 aspect ratio*/
The viewport is the visible area of the web page.
Its full size is 100vw * 100vh, where vw and wh are the viewports size units.
Thus one "vw" is equal to 1% of the web page's currently visible width.
More can be found at: Viewport units: vw, vh, vmin, vmax
the proposed solutions so far use vw which only work if you want the image to fill the entire page.
but there is a much cleaner solution that keep the image aspect ratio 9/16 in all size containers.
HTML:
...
<div class="image image-9-16"> <!-- replace image and image-9-16 with any name you like -->
<img src="..." />
</div>
...
CSS:
.image {
position: relative;
display: block;
width: calc(100% - 20px);
max-width: calc(100% - 20px);
height: auto;
max-height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
.image::before {
display: block;
content: "";
}
.image, .image img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
.image-9-16::before {
padding-top: 56.25%;
}
and that'll work no matter the width of the container holding the image with no need to place the image as a background-image... and now you can even add more aspect ratios if you want...
.image-1-1::before {
padding-top: 100%;
}
.image-3-4::before {
padding-top: 75%;
}
.image-9-21::before {
padding-top: 42.857143%;
}
...
you can use vw (viewport width) to do that:
width: calc(100vw - 20px);
height: calc((100vw - 20px) * 0.5625); /*16:9 aspect ratio*/
You can also use the padding-bottom method if you place the image as a
background for the div.
https://jsfiddle.net/m11L9kjb/1/
This worked for me using the actual ratio… then I needed a max dimension, so make sure you set them before setting the height:
position: relative;
margin: 0 auto;
width: 100vw;
max-width: 1080px;
max-height: 1920px;
height: calc(100vw * (16/9));

Maintaining a videos aspect ratio in a responsive layout

I'm trying to maintain a videos aspect ratio in a responsive layout to prevent black edges when the layout changes size. So far, I've set up some media queries, but while re-sizing there are still some points that the video has black edges.
You can see the layout and video here http://smedia.lv/ (the SHOWREEL video).
The video is embedded from Vimeo with an iframe and it has a width and height of 100%. The video container width depends on the screen size and is also defined in %, the height is a fixed value.
How can I keep the aspect ratio of the video, so it doesn't have the black edges?
What you want is fluid width video.
Adding just a few styles to your container (.video) and the iframe will accomplish this.
.video {
height: 410px;
width: 964.71px;
margin: 0 auto;
}
iframe {
width: 100%;
height: 100%;
}
/* Adjust the max-width depending on the other styles on your site. */
#media(max-width: 1046px) {
.video {
position: relative;
/* 40:17 aspect ratio */
padding-bottom: 42.5%;
height: 0;
width: auto;
}
iframe {
position: absolute;
top: 0;
left: 0;
}
}
Checkout the example below.
Note:
You can use a media query to setup a breakpoint at which point the video will grow no bigger than 964.71x410.
You will need to update the padding-bottom, .video width, and media query to reflect the correct aspect ratio of your video if it changes.
.video {
height: 410px;
width: 964.71px;
margin: 0 auto;
}
iframe {
width: 100%;
height: 100%;
}
#media(max-width: 1046px) {
.video {
position: relative;
/* 40:17 aspect ratio */
padding-bottom: 42.5%;
height: 0;
width: auto;
}
iframe {
position: absolute;
top: 0;
left: 0;
}
}
<div class="video">
<iframe src="https://player.vimeo.com/video/120261170" width="500" height="213" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
</div>

How can I achieve fluid width video?

I tried using the example for fluid width video and it works for the video but other typ of content (plain text comment) should not have the padding and now they get they padding resulting in a large gap of nothing with each comment that is not a video. Can you tell me how I can fix the layout so that all elements are fluid and responsive?
.abstract{
}
.abstract-inner {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.abstract-inner iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
HTML
<div class="abstract-container">
<div class="abstract">
<div class="abstract-inner">
<iframe width="512" height="288" src="https://www.youtube.com/embed/r9yH-EmnGX4?feature=oembed" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
</div>
</div>
</div>
The above is just a fragment, but I made a fiddle if it helps. Maybe you know what I should do to make the video have a fluid responsiveness and the text properly placed?
If I use only this CSS, then everything is fins except the height of the video that is too small:
.abstract{
}
.abstract-inner {
}
.abstract-inner iframe{
min-width: 100% !important;
max-width: 100% !important;
min-height: 100% !important;
max-height:100% !important;
}
If I use CSS viewport units then the page looks right in the fiddle but combined with my other code then the video get blown up larger than the page.
In this fiddle the problem is reproduced using CSS viewports when the page is resized to fit also the menu to the left.
I would use CSS viewport units in this case.
The code also becomes a lot simpler. (You may also find that some of the wrapper elements aren't necessary anymore)
iframe {
width: 100%;
height: 56.25vw; /* 16:9 ... 56.25vw means 56.25% of the viewport width*/
}
That's it.
Updated fiddle (Resize to verify that the iframe keeps aspect ratio)
NB:
If you only want the iframe to take up a certain % of the viewport width (say 80% ) - this can still be done while still maintaining the correct aspect ratio on the iframe .
FIDDLE (Resize the viewport width)
iframe {
width: 100%;
height: 56.25vw;
/* 16:9 ... 56.25vw means 56.25% of the viewport width*/
}
aside {
width: 20vw;
height: 100vh;
background: aqua;
display: none;
}
#media screen and (min-width: 900px) {
aside {
display: table-cell;
}
iframe {
width: 80vw;
height: 45vw;
/* 9/16 * 80 = 45 */
}
.content {
display: table-cell;
vertical-align: top;
}
.wpr {
display: table;
}
}
<div class="wpr">
<aside>blabla</aside>
<div class="content">
<iframe src="https://www.youtube.com/embed/r9yH-EmnGX4?feature=oembed" frameborder="0" allowfullscreen="allowfullscreen"></iframe>
<p>content here</p>
<p>content here</p>
<p>content here</p>
</div>
</div>
I think you can do similar thing like this
window.onresize = function(event) {
updateiframeSize()
};
function updateiframeSize() {
$(".abstract-inner").each(function(){
$this = jQuery(this);
var ratio_cont = $this.width()/$this.height();
var $iframe = $this.find("iframe");
var ratio_iframe= $iframe.width()/$iframe.height();
if (ratio_cont > ratio_iframe)
{
$iframe.css({"width": "100%", "height": "auto"});
}
else if (ratio_cont < ratio_img)
{
$iframe.css({"width": "auto", "height": "100%"});
}
});
};
if you want in css try
iframe{
width: 100% !important;
height: auto !important;
}
if you want to keep aspect ratio then put width:100% and height:auto
if you want to cover whole parent element then height:100% and width:100%

Video doesn't scale correctly in width

I have a slight issue. I have a video which I would like to adjust to the browser, it shouldn't stretch, but neither should there be any with space visible
HTML:
<video src="Wereldbol.mp4" onclick="this.play();" id='wereldbol' preload="auto" ></video>
CSS:
#wereldbol {
position: absolute;
bottom: 0px;
right: 0px;
width: 100%;
height: 100%;
z-index: -1;
overflow: hidden;
background-size:cover;
}
background-size: cover; is causing the issue that the video would be off-center, is there any alternative way to cover the browser's full width and height? At the moment width: 100% and height: 100% don't quite fix the issue because it would leave the image to have a white bar on the left and right, eventhough the video scales correctly. Is there any way to fix this issue?
If I understand you correctly, what you are trying to achieve is keep the video’s aspect ratio, but make it adapt to its surroundings without stretching the video out of proportion. Here’s how I do it:
Wrap the video element in a div, like this:
<div class="video-wrapper">
<video src="Wereldbol.mp4" onclick="this.play();" id='wereldbol' preload="auto" ></video>
</div>
Then use the following CSS:
.video-wrapper {
position: relative;
width: 100%;
max-width: 100%;
height: 0;
padding: 56.25% 0 0 0; /* 100%/16*9 = 56.25% = Aspect ratio 16:9 */
overflow: hidden;
border: 0;
}
.video-wrapper video {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
}
EDIT: Here’s a plunker: Adjust the viewport-width to see how it works.
You need to either adjust the CSS to fit the exact aspect-ratio using the "aspect ratio trick" or use something like FitVids.js -- see http://fitvidsjs.com/

Make flash file scale to div width and maintain height according to its aspect ratio

We're having a problem to make a flash file scale 100% according to the parent div width, while still maintaining the height according to its aspect ratio.
Please check out the code on jsFiddle (http://jsfiddle.net/LgegF) or paste the below code into an empty html-page:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>
<script type="text/javascript">
swfobject.embedSWF("http://www.diescon.net/yellowfestival/400x400.swf", "flash-content", "100%", "100%", "9.0.0", '', null, {wmode: 'transparent'});
</script>
<style type="text/css">
body {
background-color: #ccc;
margin: 0;
}
#container {
width: 400px; /* Fixed width according to page template */
height: auto; /* Container height should adapt to the content, we do not want to set a fixed height here */
background-color: pink; /* Background of container */
position: absolute;
top: 0;
left: 0;
}
#flash-content {
width: 400px; /* Fill the width of the parent, could be fixed but ideally 100% */
height: 100%; /* We want the height of the flash object/flash content to scale according to the aspect ratio of the swf, we do not want to set a fixed height here */
}
/* This is what we really want */
#what-we {
width: 400px;
height: auto;
background-color: pink;
position: absolute;
top: 0;
right: 0;
}
#really-want {
width: 100%;
height: 400px; /* This is what we really want, but without setting this height fixed */
background-color: yellow;
}
</style>
</head>
<div id="container">
<div id="flash-content"></div>
</div>
<div id="what-we">
<div id="really-want">
<span>This is what we really want</span>
</div>
</div>
</body>
</html>
I have a similar problem and I find this page that was very useful:
http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
Based on what I've learn on this article, I propose to change your HTML code to:
<div id="container">
<div class="videoWrapper">
<div id="flash-content"></div>
</div>
</div>
And to change your CSS code by adding:
.videoWrapper {
position: relative;
padding-bottom: 100%; /* 1:1 */
padding-top: 0px;
height: 0;
}
.videoWrapper object, .videoWrapper embed, {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
You can see the result here:
http://jsfiddle.net/LgegF/56/
Hope this help.
PatBriPerso
It appears what you are looking for is
width: 100%;
height: auto;
Let me know how that works out.
-Brian
Ommit the css height declaration for the flash container alltogether. Swf's usually do scale (or you can set their scale behaviour in your embed/swfobject options), so if you just restrict the width, the height of the swf itself scales accordingly.
[edit:]
Working fiddle (with very rudimentary swf embed code): http://jsfiddle.net/LgegF/1/
You notice how the swf scales when scaling the browser. The white on top and bottom of the swf is due to the object inclusion, swfobject or the tags give you controll over how the swf itself scales (scale mode, align, valign, etc). What you notice though, is how the swf uses the container's width to determine its size, i.e. fitting the width and scaling the height accordingly.