Advertisement
salmancreation

iframe Fluid Width Video responsive tips+tut

Dec 15th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 13.72 KB | None | 0 0
  1. https://css-tricks.com/NetMag/FluidWidthVideo/Article-FluidWidthVideo.php
  2.  
  3. http://stackoverflow.com/questions/17838607/making-an-iframe-responsive
  4.  
  5. <h1>Responsive iframe</h1>
  6.  
  7. <div class="videoWrapper">
  8.     <iframe src="//www.youtube.com/embed/9fSde2DD8YQ" allowfullscreen></iframe>
  9. </div>
  10. --------\\\\\\\\\\
  11. .videoWrapper {
  12.     position: relative;
  13.     padding-bottom: 56.25%; /* 16:9 */
  14.     padding-top: 25px;
  15.     height: 0;
  16. }
  17. .videoWrapper iframe {
  18.     position: absolute;
  19.     top: 0;
  20.     left: 0;
  21.     width: 100%;
  22.     height: 100%;
  23. }
  24.  
  25.  
  26. ---------------/
  27. Making Embedded Content Work In Responsive Design
  28. By Rachel McCollin
  29. February 27th, 2014 Content StrategyResponsive Web Design 94 Comments
  30. The Sketch Handbook Meet the new Sketch Handbook, our brand new Smashing book that will help you master all the tricky, advanced facets of Sketch. Filled with practical examples and tutorials in 12 chapters, the book will help you become more proficient in your work. Get the book now →
  31.  
  32. A few HTML elements don’t play nice with responsive layouts. One of these is the good ol’ iframe, which you may need to use when embedding content from external sources such as YouTube.
  33. In this article, we’ll show you how to make embedded content responsive using CSS, so that content such as video and calendars resize with the browser’s viewport. For those occasions when non-coders will be embedding video on your website and you don’t want to rely on them adding extra markup, we’ll also look at a solution that uses JavaScript instead of CSS.
  34. (You can see a demo of these techniques on my blog.)
  35. Note: This technique was originally detailed in Thierry Koblenz’s excellent tutorial ‘Creating Intrinsic Ratios for Video‘. I’ve used techniques I learned from his tutorial and expanded on them here for additional content types such as calendars.
  36. The Markup For Embedded Content Link
  37. Services such as YouTube provide code that you can copy and paste into your own website to embed content. I tend to recommend to my clients that they host video with YouTube because it will save them server space and, regardless of the user’s browser or device, YouTube will display the video correctly. The two main ways to embed video on a website are the HTML5 video element, which doesn’t work in legacy versions of Internet Explorer, and Flash, which doesn’t work on iOS devices and isn’t standards-compliant.
  38. When you embed content from an external source, the code will include an iframe:
  39. <iframe src="http://www.youtube.com/embed/4aQwT3n2c1Q" height="315" width="560" allowfullscreen="" frameborder="0"></iframe>
  40. This iframe enables external content to be displayed on your website, because it includes a URL that points to the source of the streamed content.
  41. However, you’ll notice that our iframe includes width and height attributes. Remove these and the iframe will disappear because it would have no dimensions. And you can’t fix this in your style sheet, unfortunately.
  42. The width attribute means that, on a screen narrower than 560 pixels, the embedded content will protrude outside of its containing element, breaking the layout. In the example below, I’ve added the code above to a page of my blog. The screenshot is taken from an iPhone in portrait mode (320 pixels wide), and the rest of the page has been shrunk so that the embedded content fits the screen. Far from ideal!
  43. Video breaks the layout in a responsive website on iPhone
  44. Screenshot taken from an iPhone in portrait mode (320px wide). (Larger view)
  45. Fortunately, there is a way around this using CSS. First, I’ll show you how to do this with embedded video, and then with calendars.
  46. Responsive Video Link
  47. THE MARKUP LINK
  48. To make embedded content responsive, you need to add a containing wrapper around the iframe. Your markup would be as follows:
  49. <div>
  50.     <iframe src="http://www.youtube.com/embed/4aQwT3n2c1Q" height="315" width="560" allowfullscreen="" frameborder="0">
  51.     </iframe>
  52. </div>
  53. The next step is to add styling to this new wrapper and the iframe within it.
  54. THE CSS LINK
  55. First, we style the containing wrapper with the .video-container class. As proposed by Thierry Koblentz in his ALA article “Creating Intrinsic Ratios For Video”, we can use the following snippet in our style sheet:
  56. .video-container {
  57.     position: relative;
  58.     padding-bottom: 56.25%;
  59.     padding-top: 35px;
  60.     height: 0;
  61.     overflow: hidden;
  62. }
  63. This does a few things:
  64. Setting the position to relative lets us use absolute positioning for the iframe itself, which we’ll get to shortly.
  65. The padding-bottom value is calculated out of the aspect ratio of the video. In this case, the aspect ratio is 16:9, which means that the height will be 56.25% of the width. For a video with a 4:3 aspect ratio, we set padding-bottom to 75%.
  66. The padding-top value is set to 30 pixels to allow space for the chrome — this is specific to YouTube videos.
  67. The height is set to 0 because padding-bottom gives the element the height it needs. We do not set the width because it will automatically resize with the responsive element that contains this div.
  68. Setting overflow to hidden ensures that any content protruding outside of this element will be hidden from view.
  69. We also need to style the iframe itself. Add the following to your style sheet:
  70. .video-container iframe {
  71.     position: absolute;
  72.     top:0;
  73.     left: 0;
  74.     width: 100%;
  75.     height: 100%;
  76. }
  77. This targets iframes inside containers with the .video-container class. Let’s work through the code:
  78. Absolute positioning is used because the containing element has a height of 0. If the iframe were positioned normally, we would have given it a height of 0 as well.
  79. The top and left properties position the iframe correctly in the containing element.
  80. The width and height properties ensure that the video takes up 100% of the space used by the containing element (which is actually set with padding).
  81. Having done this, the video will now resize with the screen’s width. Here’s how it will look on a desktop:
  82. Responsive video as seen on the desktop (in the flow of the content, fitting nicely)
  83. Desktop screenshot of the video resizing the screen’s width. (Larger view)
  84. And here’s how it will look on a screen that is 320 pixels wide:
  85. Responsive video fits into the layout on a 320px wide screen
  86. The video on a 320 pixels wide screen. (Larger view)
  87. Let’s move on to other sources of embedded content — specifically, Google calendars.
  88. The Sketch Handbook Meet the new Sketch Handbook, our brand new Smashing book that will help you master all the tricky, advanced facets of Sketch. Filled with practical examples and tutorials in 12 chapters, the book will help you become more proficient in your work. Get the book now →
  89.  
  90. Responsive Calendar Link
  91. THE MARKUP LINK
  92. The CSS to make any form of embedded content responsive is essentially the same, but different content will have different aspect ratios, which means you’ll need to set the padding-bottom value accordingly.
  93. Below is a screenshot of a website that I manage for a primary school, a website that embeds a Google calendar. As you can see, the calendar breaks the layout on a small screen. In this case, the website is displayed at the correct width, but the calendar goes beyond the screen’s width.
  94. A celandar as seen on a responsive website on an iPhone - not all of the calendar is visible
  95. The calendar breaks the layout on a small screen. (Larger view)
  96. The markup for the embedded calendar is as follows:
  97. <iframe src="https://www.google.com/calendar/embed?height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=60aqhusbghf7v0qvvbfu1ml22k%40group.calendar.google.com&amp;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no"></iframe>
  98. To make a calendar responsive, add a div with a class of .calendar-container to contain the iframe:
  99. <div>
  100.     <iframe src="https://www.google.com/calendar/embed?height=600&amp;wkst=1&amp;bgcolor=%23FFFFFF&amp;src=60aqhusbghf7v0qvvbfu1ml22k%40group.calendar.google.com&amp;color=%232952A3&amp;ctz=Europe%2FLondon" style=" border-width:0 " width="800" height="600" frameborder="0" scrolling="no">
  101.     </iframe>
  102. </div>
  103. The next step is to style this div.
  104. THE CSS LINK
  105. The CSS for a calendar is almost identical to the CSS for a video, with two exceptions: The aspect ratio will be different, and padding-top isn’t needed.
  106. Add the following to your style sheet:
  107. .calendar-container {
  108.     position: relative;
  109.     padding-bottom: 75%;
  110.     height: 0;
  111.     overflow: hidden;
  112. }
  113. In this case, the iframe is 800 pixels wide and 600 pixels high, which gives us an aspect ratio of 4:3. So, set padding-bottom to be 75%.
  114. Having done this, we need to apply the same styling to the iframe element in this new container:
  115. .calendar-container iframe {
  116.     position: absolute;
  117.     top:0;
  118.     left: 0;
  119.     width: 100%;
  120.     height: 100%;
  121. }
  122. This is exactly the same styling that we applied to the video.
  123. Now, the calendar will resize with the browser window, as shown here in Opera Mobile on an Android phone:
  124. A responsive calendar fits into the width of a 320px wide screen
  125. As long as you remember to wrap your embedded calendars and videos with the appropriate containing element, then this CSS will work for any new videos and calendars that you add to your website.
  126. The problem, however, is that although you can fit the whole calendar on a page, it’s still almost unusable because click targets are so small and no information is visible. If you absolutely have to display Google Calendar, you can, but if you can use more usable responsive calendar solutions like simple CSS (setting display: block on table rows, for example), or w3widgets Responsive calendar or Calendario for your own calendars, your users might appreciate it.
  127. Responsive Video With CSS or JavaScript Link
  128. If you’re developing a responsive website using a content management system, then one or more of the website’s editors will probably have to embed video at some point. You can point your editors to EmbedResponsively.com which generates responsive <embed> codes for embedding rich third-party media with one click, with CSS alone. Alternatively, you could use a JavaScript solution, to relieve nervous editors from having to add extra CSS and markup. However, as long as you can avoid this path, the better, of course.
  129. Until recently, most solutions were plugins, which are OK to an extent but can have performance issues. A popular plugin is FitVids.js, developed by Chris Coyier and Paravel.
  130. A more current solution is to use just a script — such as FluidVids.js, developed by Todd Motto. FluidVids.js is simple to use:
  131. Download the script (ZIP) from GitHub and upload it to your server with the same folder structure that the downloaded files come in. This will place the script itself in a folder named dist.
  132. Call the script in each page’s <head> section with the following code:
  133. <script src="dist/fluidvids.js></script>
  134. That’s all you need to do to make videos resize on all devices that support JavaScript. It works not only for YouTube, but for Vimeo, too. The problem, however, is that if you users don’t have JavaScript support or the JavaScript hasn’t loaded yet or JavaScript hasn’t loaded correctly, the only fallback you can use is to add the following to your style sheet:
  135. iframe {
  136.     max-width: 100%;
  137. }
  138. This will ensure that video resizes to the width of the browser’s window. But it won’t resize the video’s height; unfortunately, iframe just doesn’t work this way. So, the video won’t break your layout, but it won’t look very good either. This is not really a good option, so if you can avoid using JavaScript for videos, it’s a good idea to do so.
  139. Responsive Google Maps Link
  140. Apart from videos and calendars, another common issue is the embedding of Google maps, responsively. Basically, we again use the same intrinsic ratio technique, and when setting padding-bottom for the wrapper, we just divide the height by width and add the aspect ratio in CSS.
  141. Usually, the code generated by Google Maps would look like this:
  142. <iframe src="https://www.google.com/maps/embed?pb=!1m14!1m8!1m3!1d3022.260812859955!2d-73.990184!3d40.756288!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0xb134c693ac14a04c!2sThe+TimesCenter!5e0!3m2!1sen!2suk!4v1393485725496" width="500" height="450" frameborder="0" style="border:0"></iframe>
  143. We just wrap a div around the iframe and apply the familiar CSS styling to it:
  144. .google-maps {
  145.     position: relative;
  146.     padding-bottom: 90%; // (450 ÷ 500 = 0.9 = 90%)
  147.     height: 0;
  148.     overflow: hidden;
  149. }
  150. .google-maps iframe {
  151.     position: absolute;
  152.     top: 0;
  153.     left: 0;
  154.     width: 100%;
  155.     height: 100%;
  156. }
  157. And this is what the markup will look like:
  158. <div class="google-maps">
  159. <iframe src="https://www.google.com/maps/embed?pb=!1m14!1m12!1m3!1d7098.94326104394!2d78.0430654485247!3d27.172909818538997!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!5e0!3m2!1sen!2s!4v1385710909804" width="500" height="450" frameborder="0" style="border:0"></iframe>
  160. </div>
  161. Voilá! Again, we can just use EmbedResponsively to generate the copy-paste-code with one click
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. ------------------------------////////////////////////
  169.  
  170. // Find all iframes
  171. var $iframes = $( "iframe" );
  172.  
  173. // Find &#x26; save the aspect ratio for all iframes
  174. $iframes.each(function () {
  175.   $( this ).data( "ratio", this.height / this.width )
  176.     // Remove the hardcoded width &#x26; height attributes
  177.     .removeAttr( "width" )
  178.     .removeAttr( "height" );
  179. });
  180.  
  181. // Resize the iframes when the window is resized
  182. $( window ).resize( function () {
  183.   $iframes.each( function() {
  184.     // Get the parent container&#x27;s width
  185.     var width = $( this ).parent().width();
  186.     $( this ).width( width )
  187.       .height( width * $( this ).data( "ratio" ) );
  188.   });
  189. // Resize to fix all iframes on page load.
  190. }).resize();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement