Reduce initial-load page weight by only loading embedded video resources on-demand.
Embedded videos from 3rd-party platforms like Vimeo and YouTube are a great way to include videos directly on a webpage, but can add more than 500KB of page weight even when they're not played. This technique replaces a placeholder image and link to the video with the embedded video when clicked. The image and link doubles as a perfect no-JavaScript fallback.
An on-demand embedded video in action:
The HTML
Start with a link and thumbnail image, wrapped in a container with the video ID stored as a data attribute.
The CSS
Style the placeholder image link as desired, including a custom play button. This setup also uses a technique for responsive embedded videos that scale with a custom aspect ratio.
The JavaScript
Replace the image link with a normal embedded video <iframe> when the placeholder is clicked.
Without JavaScript
This sans-JavaScript alternative is more limited in features and browser support, but similarly avoids loading the full embedded video resources until the user clicks the play button. The srcdoc attribute supersedes the regular src attribute in browsers that support it.
Responsive Embedded Videos
This technique works great when combined with mobile friendly, automatically-scaling embedded videos: