YouTube Live Stream and Live Chat Embedded in Website (Responsive)

This tutorial shows how to embed a YouTube live stream iframe into an HMTL page with the live chat streaming as well. While the YouTube platform is nice, it can be nice to keep visitors on your domain while watching a live stream to show extra promotions, etc.

Embedding YouTube videos (and chat for that matter) can be tricky in that it is done via the iframe element. iframe‘s are not the most flexible HTML element. This is partially due to the nature of what is happening – blindly embedding the contents of one site into another. The parent site doesn’t know what the dimensions of the child site is, should it scroll or not, etc. While they can be frustrating to work with, it is possible to accomplish the desired user experience in this scenario – including be responsive, providing a favorable experience for mobile users.

YouTube Embed iframe Live Video and Chat Responsive

How to embed YouTube Live Video and Chat into a Website

Embedding a YouTube Live video and it’s accompanying Live Chat is done by implementing two iframe‘s in the website – one for the YouTube Live Video and the other for the accompanying Live Chat stream. By using two iframe‘s it allows for greater control over the layout and user experience. Also, it’s the way that YouTube designed to work – even how they do it on their own site – yes YouTube uses an iframe to add the chat to their own site.

With the two iframe elements on the page, some CSS does the styling magic. In the case of this tutorial, it will be a 70/30 split. The video will be 70% of the width of the screen and the live chat will be the remaining 30%. The CSS is responsive and so on a narrower screen (860px and less) the live chat goes below the live video iframe.

One critical note here is that this CSS was written to work for videos having a 16×9 aspect ratio. It is possible to adjust the CSS for other aspect ratios as needed. There are 2 aspect-ratio directives that would need to be adjusted. The rest of the CSS is based on percentages and thus should work without any adjustment.

Implementing Embedded YouTube Live Video and Chat into a Website

Below is a HTML and CSS example on how to embed YouTube Live video and chat in a website.

This example screenshot can be found here: Example of YouTube Live video and chat embed iframes (NOTE: the YouTube video embedded likely will change, but the HTML and CSS are still valid even if the video and chat don’t load)

YouTube Live Chat with Live Stream iframe example screenshot

YouTube Embed HTML

This HTML has the YouTube iframe elements wrapped in the container HTML structure that allows for styling of the elements.

There are two parts to this that need to be modified specific to the video being embedded.

YouTube ID

Replace the two entries of <youtube_id> with the YouTube ID of the video to be embedded. This is most easily obtained from the URL of the live stream YouTube page from the v query parameter. This needs to be set on both iframe‘s.

embed_domain Query Parameter

To get the Live Chat to stream correctly the embed_domain query parameter for the live chat must be set to the domain (TLD) that the iframe is being added to. So replace www.example.com with www.yourdomain.com accordingly when embedding this into the site. If this is not set correctly, the Live Chat will not work correctly.

YouTube Embed CSS

This CSS is pretty straightforward and will work for videos in the 16 x 9 aspect ratio as is.

Notes

The YouTube live chat documentation referenced below states that it will not work on mobile devices. In testing however it works as expected using Safari and Chrome on an iPhone.

References