Fixing padding issues when using iframe css responsiveness trick - css

Fixing padding issues when using iframe css responsiveness trick.
.videoWrap {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
height: 0;
overflow: hidden;
}
.videoWrap iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
People use this css trick in order to insure the iframe stays responsive while the video preserves its aspect ratio, but the problem is when you use a different value than 100%.
<div class="videoWrap">
<iframe width="560" height="315" src="//www.youtube.com/embed/xxx" frameborder="0" allowfullscreen></iframe>
</div>
I have an example below. And the issue is that depending on the device the margin between the video and the text becomes bigger, I want to keep the margin to a minimum, but it's hard to put a maximum value to the margin below of the video. Is there an easy way to do this? The only solution would be to use a lot of media query, but I only use 5 breakpoints on my website, I would have to use 9 or more to prevent the bottom margin from becoming too large.
Is there a better way around this issue?
https://codepen.io/codepen_user_123/pen/KKNmLad

Related

Scaling full width video background rows with viewport

I am not a developer, which I will make painfully clear here:
There are 3 full screen video background elements towards the top of my company website’s homepage (papertown.tv — see the rows titled, 'how it should be,’ ‘your value with replay value,’ and ‘simply, lasting results’), which I cannot for the life of me seem to coerce into scaling down with the viewport. In other words, instead of maintaining their full fields-of-view as the viewport shrinks, they get cropped by their respective rows. It’s to the point where it’s nearly impossible to tell what’s happening in the videos on mobile.
I tried this #media query but it just made the videos float around awkwardly inside the rows:
#media (max-width: 798px) {
#this-is-how-mobile .fl-bg-video,
#your-value-with-mobile .fl-bg-video,
#simply-lasting-results-mobile .fl-bg-video
{
height: 1920px;
width: 1080px;
-webkit-transform: scale(.45);
transform-origin: 0% -27%;
z-index:-1!important;
The site was built on WordPress with Beaver Builder (sorry). But this should be a simple fix, no?
Happy to send some Doge or Ether to whomever helps me solve this.
Many thanks in advance.
You can wrap the iframe with the following html/css and it will scale to fit the size of the container.
CSS
.video-wrap {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
height: 0;
margin-bottom: 30px;
}
.video-wrap iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
HTML
<div class="video-wrap">
<!-- Copy & Pasted from YouTube -->
<iframe width="560" height="349" src="https://player.vimeo.com/video/572629737?title=0&byline=0&muted=1&autoplay=1&loop=1&background=1&app_id=122963" frameborder="0" allowfullscreen></iframe>
</div>

Stretching iframes to fit a width while maintaining height-width aspect ratio (tumblr theme)

I'm running into a problem with tumblr cutom theme creator. When adding, for example, a video post, they put it into an iframe with a set width and height. I can reset with width of the iframe and the video will stretch accordingly, but the height stay the same (as expected).
But when I set the height to auto, it squishes into a small rectangle, instead of assuming its normal width-height ratio (as I thought it would). Is there a way to make the iframe keep the ratio set for it in the iframe, but scale it up/down with CSS? JQuery is okay too, but CSS is preferred.
HTML
<div class="video">
{VideoEmbed} // tumblr replaces this with an iframe containing the video
</div>
CSS (not working)
.video iframe {
height: auto // not working (as in, not producing desired effect)
width: 700px // working
}
You can maintain the ratio by using a simple CSS padding trick. You'll need to wrap the iframe in a container element, and set styles like this:
/*span*/.iframe-wrapper {
width: 50%;
height: 0;
padding: 40% 0 0;
display: block;
position: relative;
border: 5px solid blue;
}
/*span*/.iframe-wrapper iframe {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
max-width: 100%;
max-height: 100%;
}
<span class='iframe-wrapper'>
<iframe width="560" height="315" src="https://www.youtube.com/embed/XdlmoLAbbiQ" frameborder="0" allowfullscreen></iframe>
</span>
Basically, you set the width you want the <iframe> to be, then add padding to the container to maintain the aspect ratio.

responsive iframe in pure css

I'm attempting to create a class for embedded youtube videos that will display them at 100% width in the div, and will adjust the height according to the video's aspect ratio.
I have found the following suggestion here:
.fluidMedia {
position: relative;
padding-bottom: 56.25%; /* proportion value to aspect ratio 16:9 (9 / 16 = 0.5625 or 56.25%) */
padding-top: 30px;
height: 0;
overflow: hidden;
}
.fluidMedia iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="fluidMedia">
<iframe src="" frameborder="0"> </iframe>
</div>
but this relies on every video having the same aspect ratio to work. I'd prefer to write one class that just works, regardless of the video embedded into it. Can this be done without javascript?
The only CSS solution is the one you used, where you need to set the ratio in your CSS file – therefore there is no generic frontend solution without JavaScript.
You could have Drupal extract the ratio for you, of course – and write it into an inline CSS style property like
<div class="fluidMedia" style="padding-bottom: <?=$imgWidth/$imgHeight?>">
How to get the Video ratio would be a Drupal or PHP question.
For the JavaScript Solutionsee AndyM's link: http://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php

Removing black borders on a vimeo iframe embed using CSS?

I am trying to find a way to hide the black strips across the top and bottom of a vimeo video. I thought there might be a way to cover them up with CSS.
I basically wanted to achieve what this person wanted to achieve with an image in the link below except I want to do it with an embedded video whilst keeping it repsonsive.
Removing black borders 4:3 on youtube thumbnails
Many thanks.
HTML
<section class="d5-d13 c5-c13 b5-b13 a5-a13 video">
<div class='embed-container'>
<iframe src='http://player.vimeo.com/video/69252713' frameborder='0'
webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>
</section>
CSS
.embed-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
max-width: 100%;
height: auto;
}
.embed-container iframe, .embed-container object, .embed-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
For your use case, I don't think you'll be able to use just css.
Usually we add letterboxing or pillar boxing around video iframes to keep the height and width at a certain ratio for presentation. But in that case, the black borders would just be as simple as a css background.
To keep things responsive, you would set the height to something like zero (like you have) and use the padding hack to keep the aspect ratio of the video (in this case a 16:9 video; 9/16 * 100 = 56.25%). That number would be either your padding-top or padding bottom value. Since the padding is measured with percent, this scales the padding in relation to the width keeping the correct ratio no matter what width you size the video to.
In your case, this video actually has the letterboxing in the actual video which you can see from the source of the video tag within the iframe. I'm not sure why you have the padding-top:30 but that makes the black borders even bigger. You'll need to hack your situation even more though because of the built in letterboxing. I put together a jsfiddle demo here which includes a few comments but it uses JS to achieve what you're looking for.
The concept for the code is as follows:
You want the outer container to crop off the bottom and top of the
video. Assuming you wanted the video to be responsive, and be cropped, you need to always have the actual video be larger than the outer container which masks it.
The video should be moved up in relation to how wide the video is vs the thickness of the top border
You'll want to shrink the height of the outer container a bit to compensate for the negative top margin yet still hide the bottom portion of the video
Personally I don't like doing expensive DOM operations on resize which maybe is the reason you asked for solely css but FWIW, you have the demo.
Ideally your best option would be to get the video re-recorded without the letterboxing so all you would need is the padding hack.
Cut the 1px off all edges with CSS:
.embed-container {
position: relative;
padding-bottom: 43%; /* Aspect ratio of the video */
height: 0;
overflow: hidden;
max-width: 100%;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
position: absolute;
top: -1px;
left: -1px;
width: calc(100% + 2px);
height: calc(100% + 2px);
}
HTML:
<div class="js-video [vimeo, widescreen]">
[video html goes here]
</div>
CSS:
.js-video {
height: 0;
padding-top: 25px;
padding-bottom: 67.5%;
margin-bottom: 10px;
position: relative;
overflow: hidden;
}
.js-video.widescreen {
padding-bottom: 57.25%;
}
.js-video.vimeo {
padding-top: 0;
}
.js-video embed, .js-video iframe, .js-video object, .js-video video {
top: 0;
left: 0;
width: 100%;
height: 100%;
position: absolute;
}
You will find more details here
I had this same issue and the problem was simple to solve. My videos were embedded in Wordpress pages and posts using oEmbed. Wordpress was wrapping my embedded videos in <p> tags, the <p> tags had some margin which was causing black borders on the top and bottom of my videos. I used the following bit of jQuery to remove the <p> tags from my embedded videos:
$('.embed-container iframe').unwrap();
I solved this problem by removing padding-top in .embed-container
padding-bottom: 56.25%; will set screen ratio to 16:9 and remove the black bar in top and bottom.
padding top here will add extra black bar area back.
I created a solution for this exact problem using a portion of this github post. Removing Black Bars. It doesn't change vimeo's background color but merely hides it from the viewport.
https://github.com/davatron5000/FitVids.js/issues/130
#myid {
height: 112.6%;
}
However, If you add a width using CSS "vw"(viewport width) it will size consistently on any monitor/device without showing the black background. I added a margin so that the iframe will stay centered in the div once the width is shorter.
#myvimeoiframeID {
height: 112%;
width: 80vw;
margin: 0 15% auto;
}
In my parent container that holds the video, I added:
.embed-container {
padding-bottom: 40.25%;
}
This seems to make sure the video shows in the div. When I removed this section the video disappears but you can still here it play. So there is something that is pretty awesome with the padding-bottom: 40.25%;
I changed the vimeo embedded iframe code to have a height="100%".
So you can add a height to the iframe or you can do it in css. In order to control the height by css, I kept the base height in the iframe at 100% and any adjustments to that base height is through the css.
Simply put frameborder="0" as one of your attributes.
I just solved this -
The video container was built with a video-captions-container DIV which was a black transparent bar.

Youtube: Embedded video squeezed

I’m embedding a youtube video via the iFrame API.
The video itself is square–shaped and it’s width and height depend on the container’s size (a square as well, with fluid dimensions) — I’m using CSS to achieve that.
Everything works more or less okay, but in the beginning and the end of the video (before starting and after stopping), the content is displayed with reduced width, revealing black side bars:
Any idea why this happens or what to do about it? Has someone made a video with irregular shape work in context of a responsive layout?
Update
It seems that setting the width and height to 101% helps. Not sure yet how consistently, though.
<div class="video_iframe_wr">
<iframe/>
</div>
.video_iframe_wr {
height: 0;
padding-bottom: 100%; /* that makes height = width */
position: relative;
}
.video_iframe_wr > iframe {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width:100%;
height:100%;
}

Resources