Table of Contents for
Mastering Responsive Web Design

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Mastering Responsive Web Design by Ricardo Zea Published by Packt Publishing, 2015
  1. Cover
  2. Table of Contents
  3. Mastering Responsive Web Design
  4. Mastering Responsive Web Design
  5. Credits
  6. About the Author
  7. Acknowledgment
  8. About the Reviewers
  9. www.PacktPub.com
  10. Preface
  11. What you need for this book
  12. Who this book is for
  13. Conventions
  14. Reader feedback
  15. Customer support
  16. 1. Harness the Power of Sass for Responsive Web Design
  17. The basic concepts of Sass for RWD
  18. Summary
  19. 2. Marking Our Content with HTML5
  20. The
  21. The
    element
  22. The
  23. The
    element
  24. The
  25. The
  26. Using WAI-ARIA landmark roles to increase accessibility
  27. A full HTML5 example page with ARIA roles and meta tags
  28. Output screenshots for desktop and mobile
  29. Summary
  30. 3. Mobile-first or Desktop-first?
  31. Sass mixins for the mobile-first and desktop-first media queries
  32. Dealing with legacy browsers
  33. How to deal with high-density screens
  34. Sometimes RWD is not necessarily the right solution
  35. Retrofitting an old website with RWD
  36. Retrofitting with AWD
  37. Retrofitting with RWD
  38. Summary
  39. 4. CSS Grids, CSS Frameworks, UI Kits, and Flexbox for RWD
  40. CSS grids
  41. CSS frameworks
  42. UI kits
  43. The pros and cons of CSS frameworks for RWD
  44. Creating a custom CSS grid
  45. Building a sample page with the custom CSS grid
  46. Stop using CSS grids, use Flexbox!
  47. Summary
  48. 5. Designing Small UIs Driven by Large Finger
  49. The posture patterns and the touch zones
  50. The nav icon – basic guidelines to consider for RWD
  51. The navigation patterns for RWD
  52. Summary
  53. 6. Working with Images and Videos in Responsive Web Design
  54. Third-party image resizing services
  55. The element and the srcset and sizes attributes
  56. Replacing 1x images with 2x images on the fly with Retina.js
  57. Making videos responsive
  58. The Vector Formats
  59. Summary
  60. 7. Meaningful Typography for Responsive Web Design
  61. Calculating relative font sizes
  62. Creating a Modular Scale for a harmonious typography
  63. Using the Modular Scale for typography
  64. Web fonts and how they affect RWD
  65. Sass mixin for implementing web fonts
  66. Using FlowType.js for increased legibility
  67. Summary
  68. 8. Responsive E-mails
  69. Don't overlook your analytics
  70. Recommendations for building better responsive e-mails
  71. Responsive e-mail build
  72. Third-party services
  73. Summary
  74. Index

Making videos responsive

The videos we're going to talk about are the videos that come inside our good old friend, the <iframe> element, such as videos from YouTube, Vimeo, Dailymotion, and so on. There are several ways to make videos responsive, some more involving than others. Let's break it down.

Responsive videos with HTML and CSS

YouTube is an amazing video service that makes life easier for everyone—video authors, as well as web designers and developers. The fact that YouTube takes care of the hosting of the video, the streaming, and the technological conditions of browsers that don't support Flash (iOS), or browsers that don't support the <video> tag (legacy browsers), is just awesome.

The first thing we need to do is create a container that will hold the video. This container is the one we're going to manipulate to give the video the width we want while maintaining its aspect ratio:

<div class="video-container"></div>

Then, we create a container for the video we're going to embed:

<div class="video-container">
   <div class="embed-container"></div>
</div>

Then we embed the video, which is inside the <iframe> element:

<div class="video-container">
    <div class="embed-container">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/vpRsLPI400U" frameborder="0" allowfullscreen></iframe>
    </div>
</div>

Ok, that's it for our markup. Now, let's tackle the CSS from inside out.

Let's give the <iframe> element a few properties:

.embed-container iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

Then, let's give the .embed-container wrapper some context:

.embed-container {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 35px; /* This padding is only needed for YouTube videos */
    height: 0;
    overflow: hidden;
}

Now the <iframe> element will be positioned correctly and take up all the space of its parent container. The parent container will make sure the video is visible and anything sticking out of it will be hidden.

Tip

For videos with 16:9 aspect ratio, use padding-bottom: 56.25%;.

For videos with 4:3 aspect ratio, use padding-bottom: 75%;.

All we need to do now is define the width of the whole thing. We do that by adding a width to the outer container, the .video-container wrapper:

.video-container {
    width: 80%; /* This can be any width you want */
}

Responsive videos with jQuery

If you're a jQuery fan, this plugin is for you. It may also come in handy when you have to retrofit already published videos on your site, or if there are too many of them to update manually.

The plugin is called FitVids.js. It was developed by Chris Coyer and the guys at Paravel. Using FitVids.js is pretty straightforward. First, we need to download the FitVids JavaScript file from the following URL: https://github.com/davatron5000/FitVids.js/blob/master/jquery.fitvids.js

Then, we call jQuery and the FitVids.js files in the <head> of our document. Finally, we add a script at the bottom of our markup to call the fitVids function. That's pretty much all there is to it.

Tip

The actual file name of FitVids.js is jquery.fitvids.js. This is the file name we're going to see in the example.

Here's an HTML snippet with two videos within <iframe>, one from YouTube and another one from Vimeo:

<!DOCTYPE html>
<!--[if IE 8]> <html class="no-js ie8" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="no-js ie9" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
<script src="js/jquery.min.js"></script>
<script src="js/jquery.fitvids.js"></script>
    <title>Responsive Videos with: jQuery Using FitVids.js</title>
</head>
<body>
    <h1>Responsive Videos with: jQuery Using FitVids.js</h1>
    <main class="main-container" role="main">
        <h2>YouTube</h2>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/vpRsLPI400U" frameborder="0" allowfullscreen></iframe>
        <h2>Vimeo</h2>
        <iframe width="560" height="315" src="https://player.vimeo.com/video/101875373" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
    </main>
    <script>
        $(function(){
        //Look for all the videos inside this element and make them responsive
         $(".main-container").fitVids();
      });
   </script>
</body>
</html>

If you're curious about how FitVids.js modifies the DOM to make the videos responsive, here's the markup:

<div class="fluid-width-video-wrapper" style="padding-top: 56.25%;">
    <iframe src="https://www.youtube.com/embed/vpRsLPI400U" frameborder="0" allowfullscreen="" id="fitvid0"></iframe>
</div>

Tip

Document Object Model (DOM): When you read, or hear, someone say modify the DOM, it basically means modify the generated HTML.

Here's a demo I created for this in CodePen: http://codepen.io/ricardozea/pen/9e994c213c0eeb64ccd627e132778a42.

Responsive videos with plain JavaScript

If you are not using jQuery or don't want any framework dependencies, but still need a simple JavaScript solution, the best option is to use a script developed by Todd Motto: Fluidvids.js.

Using it is very simple as well. First, we need to download the Fluidvids JavaScript file: https://github.com/toddmotto/fluidvids/blob/master/dist/fluidvids.min.js

Then, we need to call the fluidvis.js file in the <head> element of our document. Once we have that in place, we add a small script snippet at the bottom of our markup. That's it. The script will read through the markup, modify the DOM, and make any videos it finds responsive.

Tip

Make sure to always give a width and height value to the <iframe> element. Otherwise you'll see a blank space on the page.

Here's the HTML snippet you'll need for this to work:

<!DOCTYPE html>
<!--[if IE 8]> <html class="no-js ie8" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="no-js ie9" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <script src="js/fluidvids.min.js"></script>
    <title>Responsive Videos with: Plain JavaScript - FluidVids.js</title>
</head>
<body>
    <h1>Responsive Videos with: Plain JavaScript - FluidVids.js</h1>
    <main class="main-container" role="main">
        <h2>YouTube</h2>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/vpRsLPI400U" frameborder="0" allowfullscreen></iframe>
        <h2>Vimeo</h2>
        <iframe width="560" height="315" src="https://player.vimeo.com/video/101875373" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
    </main>
    <script>
        fluidvids.init({
            selector: ['iframe'],
            players: ['www.youtube.com', 'player.vimeo.com']
        });
    </script>
</body>
</html>

Here's the modified DOM:

<div class="fluidvids" style="padding-top: 56.2%;">
    <iframe src="https://www.youtube.com/embed/vpRsLPI400U" width="560" height="315" frameborder="0" allowfullscreen class="fluidvids-item" data-fluidvids="loaded"></iframe>
</div>

Here's a demo I created for this in CodePen: http://codepen.io/ricardozea/pen/fda7c2c459392c934130f28cc092dbbe

Third-party services to embed video

What can I say? All you need to do is point your browser to http://embedresponsively.com/ and select the tab of the video service you want to use. Let's choose Vimeo. Input the URL of the video you want to make responsive, press the Embed button, and voilà—the HTML and CSS that you need to use appears right below the example video.

Here are the HTML and CSS snippets produced by embedresponsively.com for a video with the well-known Dan Mall about RWD (it has been formatted for easier reading):

The HTML is as follows:

<div class='embed-container'>
    <iframe src='https://player.vimeo.com/video/101875373' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
</div>

The CSS is as follows:

.embed-container {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    overflow: hidden;
    max-width: 100%;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

However, with the following snippets, the container of the video looks much higher than it should be. In order to make the preceding snippets work properly, we need to wrap the embed-container inside an outer container. Here are the amended markup and CSS.

The HTML is as follows:

<div class="video-container">
    <div class='embed-container'>
        <iframe src='https://player.vimeo.com/video/101875373' frameborder='0' webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
   </div>
</div>

The CSS is as follows:

.video-container {
    width: 100%;
}
.embed-container {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    overflow: hidden;
    background: red;
}
.embed-container iframe,
.embed-container object,
.embed-container embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
     height: 100%;
}

The .video-container wrapper is what we manipulate in order to define any width we want while maintaining the aspect ratio of the video. Now, all we need to do is place the markup in our HTML document and the CSS snippet in our SCSS file.

Here's a demo I created for this in CodePen: http://codepen.io/ricardozea/pen/10262216eeb01fc9d3b3bedb9f27c908