TL;DR: I need a scaled iFrame to be centered on larger screen sizes. I know this is a problem due to absolute positioning in the method.
I'm trying to embed an iFrame (YouTube video specifically) to a website. As it's 2017, I want the site to be responsive, and naturally, I want the embedded video to be responsive as well. So I found some code online that does this, it appears to be the common method for responsively sizing iFrames. I understand what's going on here.
Relevant HTML:
<div class="videoWrapper">
<iframe width="530" height="315" src="https://www.youtube.com/embed/4OJWC1tn_t4" frameborder="0" allowfullscreen>
</iframe>
</div>
Relevant CSS:
.videoWrapper {
position: relative;
padding-bottom: 56.25%;
height: 0;
padding-top: 20px;
overflow: hidden;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
This is where the above code gets us to.
This works fairly well, but the video takes up 100% of the container space. It should though, it's coded that way. But what if I don't want it to do that? It looks really bad on larger screen sizes to have a video take up the whole window. I couldn't find an answer to making this not happen, so I tried putting my "videoWrapper" within another called "videoSizer" and scaling that down to width: 60% with height: auto when the screen size is greater than 480px.
Relevant HTML:
<div class="videoSizer">
<div class="videoWrapper">
<iframe width="530" height="315" src="https://www.youtube.com/embed/4OJWC1tn_t4" frameborder="0" allowfullscreen>
</iframe>
</div>
</div>
Relevant CSS:
#media only screen and (min-width: 480px) {
.videoSizer {
width: 60%;
height: auto;
}
}
This is where the above code gets us to.
So the size is about where I want it, and it looks nice on the page, but it's stuck in the top left corner of its container. That's how it should be, it's coded to that absolute position in the container. It's doing what I told it to. However, what I don't understand, and herein lies the problem, when I change the position coding in any way that I've thought of so far, the container size stays the same, but the iFrame element disappears.
So I would like to get the video centered on the site without it taking up 100% of the window, but am out of ideas and knowledge of how to have that happen. If anybody had any suggestions, I'd be really appreciative! I hope I've written this in a way that's descriptive enough and has enough resources attached to adequately explain the problem.
Answer (credit to LKG for helping me find it):
Remove #media query mentioned above. Not necessary, and creates problems.
Code as in LKG's answer, seen below.
To properly calculate bottom padding based on this:
- Add margin and element widths together
- Divide element width by margin width (we'll call this p1)
- Divide iFrame element height by width (call this p2)
- Multiply p1 by p2
- Answer is your new padding-bottom value
Just change css to get center
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
right:0;
margin:auto;
}
html,
body {
margin: 0;
padding: 0;
}
.videoWrapper {
position: relative;
padding-bottom: 56.25%;
height: 0;
padding-top: 20px;
overflow: hidden;
}
.videoWrapper iframe {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: auto;
}
<div class="videoWrapper">
<iframe width="530" height="315" src="https://www.youtube.com/embed/4OJWC1tn_t4" frameborder="0" allowfullscreen>
</iframe>
</div>
CSS:
.vertical-center {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
HTML:
<div class="vertical-center">
<iframe width="560" height="315" src="../assets/video.mp4" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>
<p>Error loading video</p>
</iframe>
</div>
Related
So, I'm having quite a bit of a problem in making my iframes work in mobile devices. It's only covering about a half or more of the screen. I suspect it is because of some lines of css I put in my iframes.
.Wrapper2 {
position: relative;
padding-bottom: 56.25%; /* 16:9 */
padding-top: 25px;
height: 0;
}
.Wrapper2 iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
I would also like to add that for my text, I am using viewport sizing(vw, vh, vmin, vmax) if that would be of any help as I am also getting problems with long blocks of texts producing scrollbars. I also think (???) that it may have to do something with my iframe css and/or text sizing. What would be the necessary change that I need to implement? Thank you in advance!
Please post your html to make it easier for us to understand what the problem is.
Try this -- If you look into your iframe in html then you'll find :
<iframe width="560" height="315" src="https://www.youtube.com/embed/HDI9inno86U" frameborder="0" allowfullscreen></iframe>
So, there's width & height already defined inside the iframe element. All you have to do i change it to width="100%";
I have added slideshare iframe with presentation to my page
Code: <iframe src="//www.slideshare.net/slideshow/embed_code/key/HQoiz6GR1oLe1n" width="860" height="600" frameborder="600" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen> </iframe>
But when I open page at mobile phone there appear black area around presentation:
screnshot
I know that it happens because of style height="600", but when I remove it presentation becames small. How to remove this black areas around presentation?
With some CSS it’s very easy to make slideshare embeds that scale the height in relation to the actual width.
You need to add an outer , with 0 height, and 100% width, and a padding-bottom as percentage of the aspect ratio (eg 9 / 16 = 56.25%) with an extra padding of 38px for the slide navigation.
<div class="iframe-slideshare-16x9">
<iframe src="//www.slideshare.net/slideshow/embed_code/key/h1Fw1vmfv5uVlM"
frameborder="0"
marginwidth="0"
marginheight="0"
scrolling="no"
allowfullscreen>
</iframe>
</div>
The CSS looks like this:
.iframe-slideshare-16x9 {
padding-bottom: calc(56.25% + 38px);
position: relative;
width: 100%;
height: 0;
}
.iframe-slideshare-16x9 iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
border:1px solid #CCC;
}
More on this, at my blog: https://nathan.gs/2018/01/07/responsive-slideshare-iframe/
this is not issue with iframe as your iframe is working properly and it is responsive
the problem is images/slides in slideshare iframe are bit small in height and I believe you cant do anything about it
hope it helps
Add the following media query to your css:
#media (max-width: 667px) {
iframe {
width: 280px;
}
}
Feel free to tweak values as desired.
I am trying to embed a video from YouTube into my site. I am wondering if there is a way to relate the width and height based on the window size. For example, on an iPhone it looks something like THIS. Can I make the video shrink in width to smaller than the display size? Can I tie in my responsive site somehow, to get that?
It works great on computers or other larger screened devices, but not mobile.
Source:
<center><iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/zRDi1DT3AFA?rel=0" frameborder="0" allowfullscreen></iframe></center>
Full page source
Thanks for the help!
[edit: source]
It is possible to embed Youtube videos responsive by overriding the Youtube style (CSS).
Add the following CSS:
.video-con {
position: relative;
padding-bottom: 56.25%;
padding-top: 30px;
height: 0;
overflow: hidden;
}
.video-con iframe,
.video-con object,
.video-con embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
And then wrap the iframe with div:
<div class="video-con">
<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/zRDi1DT3AFA?rel=0" frameborder="0" allowfullscreen></iframe>
</div>
My first time embedding a youtube video anywhere, and I'm trying to optimize the video's size for both desktop and mobile.
I want to embed the video with a MAX width of 940 px and MAX height of 630 px, and otherwise the video should be as large as the screen allows. Meaning, mobile devices should have a width of 100% and desktop browsers should have 100% up to the max limits.
Is this possible?
Here's my code so far:
<div style="text-align:center;">
<iframe src="https://www.youtube.com/embed/xxx?rel=0"
frameborder="0" allowfullscreen></iframe>
</div>
I've tried various combinations of width, height, max-width, and max-height, but haven't got it to work.
Put it in a container, and use the good old padding-bottom trick. This way you can make it retain the aspect ratio and control the size of the container instead of the actual embedded thing. The container can be handled like any other normal div.
Here's an example: https://jsfiddle.net/foykobm5/1/
HTML
<div class="main-container">
<div class="video-container">
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>
</div>
</div>
CSS
.main-container {
max-width: 1000px;
margin: 0 auto;
background: #eee;
}
.video-container {
position: relative;
padding-bottom: 56.25%;
max-width: 940px;
margin: 0 auto;
}
iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
I set up a iframe like below
<iframe src="http://www.bbc.com/" frameborder="0" width="100%"></iframe>
CSS
iframe { overflow:hidden;
min-height:600px}
2 horizontal scrolling bars appear, 1 is at the iframe and the other is at the browser.
I want to get rid of the bar at iframe, I try
<iframe src="yahoo.com" scrolling="no" frameborder="0" width="100%"></iframe>
Then the scrolling bar at the iframe disappeared, and the problem is I cannot see anything at the bottom of BBC website!
I could increase min-height to greater number to cover entire length of BBC's page, but sometimes the page is very short and I'll get a huge white space at the bottom! So that increase the min-height is not a good practice.
How can I remove the scrolling bar at the iframe and use the browser's scrolling bar to navigate the page up and down?
Thank you!
There are different ways of doing this. There's jQuery solutions too. This will help you get started:
DEMO: http://jsbin.com/voqubo/1
CSS
body,html {padding:0;margin:0;height:100%;}
html {overflow-y:auto}
.embed-container {
position: relative;
height: 100%;
max-width: 100%;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
}
HTML
<div class='embed-container'><iframe src='http://www.bbc.com/' style='border:0'></iframe></div>
<iframe
src="http://maroof.sa/Business/GetStamp?bid=203783"
style=" width: auto; height: 250px; overflow-y:hidden; "
frameborder="0"
seamless='seamless'
scrollable="no">
</iframe>