{"__v":5,"_id":"567861b8e5da8d0d00591c7c","category":{"__v":8,"_id":"5666dac5d784a70d00397bcb","pages":["5666dda06819320d000c2e19","567856b9e5da8d0d00591c66","567861b8e5da8d0d00591c7c","568bdbf9e3e30f0d0021e07c","568be56983d2061900d86ce9","5697f7a60b09a41900b245d7","56abb8b49327b30d00f7c234","56d088977c2e100b000af8f0"],"project":"564de2dbfe07a81700b5c3a5","version":"564de2dbfe07a81700b5c3a8","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-12-08T13:27:33.662Z","from_sync":false,"order":6,"slug":"tutorials","title":"Tutorials"},"parentDoc":null,"project":"564de2dbfe07a81700b5c3a5","user":"564de2b4fe07a81700b5c3a4","version":{"__v":8,"_id":"564de2dbfe07a81700b5c3a8","project":"564de2dbfe07a81700b5c3a5","createdAt":"2015-11-19T14:55:23.838Z","releaseDate":"2015-11-19T14:55:23.837Z","categories":["564de2ddfe07a81700b5c3a9","564df317826645210097a890","564df3217c8f372b00b934df","564e5227c3553e0d003e53ba","5666dac5d784a70d00397bcb","56cd08ddd98d851d00c0c3bd","56e9a50946bfd60e008840a7","5718e37bf8f7de1900683fad"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"createdAt":"2015-12-21T20:31:52.139Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"When we look at responsive embedding we have to deal with how an element appears on a number of devices and multiple sizes. Lucky for us, most embeds take care of this for us, we just need to display them correctly and allow them to resize with our content.\n[block:embed]\n{\n  \"html\": \"<iframe class=\\\"embedly-embed\\\" src=\\\"//cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fjsfiddle.net%2Fmgb18yLt%2F%3Fheight%3D450%26tabs%3Dresult%2Cjs%2Chtml%2Ccss&src=https%3A%2F%2Fjsfiddle.net%2Fmgb18yLt%2Fembedded%2Fresult%2Cjs%2Chtml%2Ccss%2F&type=text%2Fhtml&key=02466f963b9b4bb8845a05b53d3235d7&schema=jsfiddle\\\" width=\\\"600\\\" height=\\\"450\\\" scrolling=\\\"no\\\" frameborder=\\\"0\\\" allowfullscreen></iframe>\",\n  \"url\": \"https://jsfiddle.net/mgb18yLt/?height=450&tabs=result,js,html,css\",\n  \"title\": \"Edit fiddle - JSFiddle\",\n  \"favicon\": \"https://jsfiddle.net/favicon.png\"\n}\n[/block]\nYou can prove it to yourself by resizing the window, but it looks like any other embed. We can accomplish this with just a bit of css and some javascript. The implementation below has both the code and the css.\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"How it Works\"\n}\n[/block]\nWe are going to build a wrapper around every embed like so:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<div class=\\\"responsive-object\\\">\\n  <iframe src=\\\"path/to/embed\\\" />\\n</div>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\nThis div will control the sizing of the underlying embed, so we want the embed to fill the entire space of the wrapper. We can accomplish this with a little bit of css.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".responsive-object iframe {\\n    position: absolute;\\n    top: 0;\\n    left: 0;\\n    width: 100%;\\n    height: 100%;\\n}\",\n      \"language\": \"css\"\n    }\n  ]\n}\n[/block]\nThe wrapper is a little more confusing as far as css, but it looks like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".responsive-object {\\n  position: relative;\\n  padding-bottom: 67.5%;\\n  height: 0;\\n  margin: 10px 0;\\n  overflow: hidden;\\n}\",\n      \"language\": \"css\"\n    }\n  ]\n}\n[/block]\nBy default a div will fill the entire width of the container, what we have to worry about is the height. `padding-bottom` defines the ratio between the width of the div and to the height of the embed. Because of the positioning of the iframe, the embed will fill the width and the height defined by the padding. It’s a little counter intuitive, but here’s a handy little diagram:\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://files.readme.io/O2VLc3HqSymQoNZbpxgV_tumblr_inline_mutqaosxIt1qa90wi.png\",\n        \"tumblr_inline_mutqaosxIt1qa90wi.png\",\n        \"500\",\n        \"314\",\n        \"#1052b6\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\nWe have set a default `padding-bottom` here, but in reality the ratio of the sizing of an embed changes per provider and even per embed. We need to calculate the `padding-bottom` and then add this as a style to the responsive wrapper. We can do this with just a little bit of javascript:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var ratio = ((obj.height/obj.width)*100).toPrecision(4) + '%';\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nTo put this all together, here is the code to convert links into responsive embeds.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$('a').embedly({\\n  display: function(obj){\\n    // Overwrite the default display.\\n    if (obj.type === 'video' || obj.type === 'rich'){\\n      // Figure out the percent ratio for the padding. This is (height/width) * 100\\n      var ratio = ((obj.height/obj.width)*100).toPrecision(4) + '%'\\n\\n      // Wrap the embed in a responsive object div. See the CSS here!\\n      var div = $('<div class=\\\"responsive-object\\\">').css({\\n        paddingBottom: ratio\\n      });\\n\\n      // Add the embed to the div.\\n      div.html(obj.html);\\n\\n      // Replace the element with the div.\\n      $(this).replaceWith(div);\\n    }\\n  }\\n});\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThere you have it, responsive embeds in 20 lines of code and a bit of css.","excerpt":"","slug":"responsive-embeds","type":"basic","title":"Responsive Embeds"}
When we look at responsive embedding we have to deal with how an element appears on a number of devices and multiple sizes. Lucky for us, most embeds take care of this for us, we just need to display them correctly and allow them to resize with our content. [block:embed] { "html": "<iframe class=\"embedly-embed\" src=\"//cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fjsfiddle.net%2Fmgb18yLt%2F%3Fheight%3D450%26tabs%3Dresult%2Cjs%2Chtml%2Ccss&src=https%3A%2F%2Fjsfiddle.net%2Fmgb18yLt%2Fembedded%2Fresult%2Cjs%2Chtml%2Ccss%2F&type=text%2Fhtml&key=02466f963b9b4bb8845a05b53d3235d7&schema=jsfiddle\" width=\"600\" height=\"450\" scrolling=\"no\" frameborder=\"0\" allowfullscreen></iframe>", "url": "https://jsfiddle.net/mgb18yLt/?height=450&tabs=result,js,html,css", "title": "Edit fiddle - JSFiddle", "favicon": "https://jsfiddle.net/favicon.png" } [/block] You can prove it to yourself by resizing the window, but it looks like any other embed. We can accomplish this with just a bit of css and some javascript. The implementation below has both the code and the css. [block:api-header] { "type": "basic", "title": "How it Works" } [/block] We are going to build a wrapper around every embed like so: [block:code] { "codes": [ { "code": "<div class=\"responsive-object\">\n <iframe src=\"path/to/embed\" />\n</div>", "language": "html" } ] } [/block] This div will control the sizing of the underlying embed, so we want the embed to fill the entire space of the wrapper. We can accomplish this with a little bit of css. [block:code] { "codes": [ { "code": ".responsive-object iframe {\n position: absolute;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n}", "language": "css" } ] } [/block] The wrapper is a little more confusing as far as css, but it looks like this: [block:code] { "codes": [ { "code": ".responsive-object {\n position: relative;\n padding-bottom: 67.5%;\n height: 0;\n margin: 10px 0;\n overflow: hidden;\n}", "language": "css" } ] } [/block] By default a div will fill the entire width of the container, what we have to worry about is the height. `padding-bottom` defines the ratio between the width of the div and to the height of the embed. Because of the positioning of the iframe, the embed will fill the width and the height defined by the padding. It’s a little counter intuitive, but here’s a handy little diagram: [block:image] { "images": [ { "image": [ "https://files.readme.io/O2VLc3HqSymQoNZbpxgV_tumblr_inline_mutqaosxIt1qa90wi.png", "tumblr_inline_mutqaosxIt1qa90wi.png", "500", "314", "#1052b6", "" ] } ] } [/block] We have set a default `padding-bottom` here, but in reality the ratio of the sizing of an embed changes per provider and even per embed. We need to calculate the `padding-bottom` and then add this as a style to the responsive wrapper. We can do this with just a little bit of javascript: [block:code] { "codes": [ { "code": "var ratio = ((obj.height/obj.width)*100).toPrecision(4) + '%';", "language": "javascript" } ] } [/block] To put this all together, here is the code to convert links into responsive embeds. [block:code] { "codes": [ { "code": "$('a').embedly({\n display: function(obj){\n // Overwrite the default display.\n if (obj.type === 'video' || obj.type === 'rich'){\n // Figure out the percent ratio for the padding. This is (height/width) * 100\n var ratio = ((obj.height/obj.width)*100).toPrecision(4) + '%'\n\n // Wrap the embed in a responsive object div. See the CSS here!\n var div = $('<div class=\"responsive-object\">').css({\n paddingBottom: ratio\n });\n\n // Add the embed to the div.\n div.html(obj.html);\n\n // Replace the element with the div.\n $(this).replaceWith(div);\n }\n }\n});", "language": "javascript" } ] } [/block] There you have it, responsive embeds in 20 lines of code and a bit of css.