DEV Community

Cover image for Optimized YouTube Embed Code Generator
Deano
Deano

Posted on • Originally published at resplace.com

Optimized YouTube Embed Code Generator

YouTube allows you to easily embed any YouTube video into a web page on your site, however this involves using iframes, and YouTube runs some heavy scripts and external resources are needed from YouTube directly. This causes slower page load which sometimes appears in speed test results as possible improvements that can be made to speed your site up.

We have developed a custom embed code, so instead of chucking in iframes that connect straight to YouTube and call in scripts and other unnecessary data, we instead embed just the preview image onto your web page, if the user wants to play the video and they click the “play” button, the small script you embed into your site will then replace the image with YouTubes iframe player as you would do originally with an auto-play. As far as the user knows, the experience is completely the same. But you save a tremendous amount of resources being included on page load.

All you need to do is embed our custom JavaScript into your HEAD tag and our CSS rules in your CSS file. Then use the tool on our website below to generate the HTML tags for each video and place that in your site.

YouTube Embed Code Generator

Setup Instructions

Please make sure you add the following CSS and JavaScript to your site on any pages that will be using the embed code generated above, else it will not work.

Add the following Javascript anywhere on your site:

//youtube video code - can be added to head or in the footer or in an external JS file.
document.addEventListener("DOMContentLoaded",
function() {
var div, n,
v = document.getElementsByClassName("youtube-player");
for (n = 0; n < v.length; n++) {
v[n].onclick = labnolIframe;
}
});

function labnolIframe() {
var iframe = document.createElement("iframe");
var embed = "https://www.youtube.com/embed/ID?autoplay=1";
iframe.setAttribute("src", embed.replace("ID", this.dataset.id) + this.dataset.ido);
iframe.setAttribute("frameborder", "0");
iframe.setAttribute("allowfullscreen", "1");
this.append(iframe);
}

Add the following CSS to your CSS file:

.iframeresponsive {
    font-size: 0;
}
.youtube-player {
    position: relative;
    overflow: hidden;
    max-width: 100%;
    background: #000;
    margin: 0;
    height: 100%;
}
.youtube-player img:hover {
    filter: brightness(75%);
}
.youtube-player img {
    bottom: 0;
    display: block;
    left: 0;
    margin: auto;
    max-width: 100%;
    width: 100%;
    position: absolute;
    right: 0;
    top: 0;
    border: none;
    height: auto;
    cursor: pointer;
    transition: .4s all;
}
.youtube-player iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100;
    background: transparent;
}
.youtube-player .play {
    height: 72px;
    width: 72px;
    left: 50%;
    top: 50%;
    margin-left: -36px;
    margin-top: -36px;
    position: absolute;
    background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAAAzCAYAAAA0CE5FAAAACXBIWXMAAAsTAAALEwEAmpwYAAAGtmlUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDIgNzkuMTYwOTI0LCAyMDE3LzA3LzEzLTAxOjA2OjM5ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtbG5zOmRjPSJodHRwOi8vcHVybC5vcmcvZGMvZWxlbWVudHMvMS4xLyIgeG1sbnM6cGhvdG9zaG9wPSJodHRwOi8vbnMuYWRvYmUuY29tL3Bob3Rvc2hvcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RFdnQ9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZUV2ZW50IyIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ0MgKFdpbmRvd3MpIiB4bXA6Q3JlYXRlRGF0ZT0iMjAxOC0wOC0wM1QxMjo1MDo1OCswMTowMCIgeG1wOk1vZGlmeURhdGU9IjIwMTgtMDgtMDNUMTI6NTk6MTMrMDE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kzKWA8ETGCO0J0WwbS/Pl0NHfujF0b74u9Dz/szZHDXl20mNqSJSgHym1miLGE20FJiO4rqn209u/Tnk0/jbe/+z2fHNqTqvMXUlu1GnIm2pmZxOBwstC+cAPRdeWly9j35G+8ObTb0tx51FathhxEzsxEBvuTRxxok3BEtykHpGTl5edhRYHamZlKArkNpUw6YAjRAxIUBR2SmNEMFBpKE5H3VdxAIGYdZUCG4TSQm3saRYUOY9ZRAuS2L+WieF1YC6mEMatDIIMW/o/UbOfDhv9dRXGJJGZBmFGKODhvorEjVRFTxgtNuMSZdVTAS83CIrVKCVdss7CbBBizTCD0u7FKiTQlQ9jzgmGHxeLsJsAUrRR5aylDIhlmYBF3tKL4eZI4m5kA565WKsZMkCqTRkfgWyj4WnZb4xLi7CSznZh/BzcQpMuGd9FhwL5q3xd2LFz4fAGfKSRMIONjzQQyphme45/NSvH5yWopPIuOlGhzVDlaE7RZk+AumW4JLJk4QrzLEGD8/xJgiKOEBInYWkRcP6U0Hi5AdCSOk93p6MtxRxT68bjrapddBVwILANKokOcMgHGiQQYp0+A8T7jXUKA0WGAEGD8JzFt0tCb4f5iqdV6JCleliU6TGDiiMRJyMDgXwYbTWxsO1RaeambrZwsisUmDYDNBdXAaqAaUh9mNbAyqAaUgCpQRpSAMlgClYASUCASkIACcCABBgQgIAMBZKANtIAMtIAmogU0DVoZGsCU4w1BA9QINOZ4Q4qGzMdMjBoxgrE/5XjH3YYnC8PFEeJE/wYzG8j73okoAQAAAABJRU5ErkJggg==) no-repeat;
    filter: grayscale(100%);
    cursor: pointer;
}
.youtube-player .play:hover {
    filter: grayscale(0%);
}

Top comments (0)