Embed YouTube videos responsively
🌱 This post is in the growth phase. It may still be useful as it grows up.
YouTube makes it easy to copy-paste a video embed. LOVE THAT!
But, the default embed isn’t responsive so it is guaranteed to look shit wherever you embed it.
I’ve dragged this snippet around the internet with me for years.
I adapted it to use AVO 🥑 CSS, my data attributes specification of BEM.
But I didn’t write the styles.
I hate magic numbers like 56.25%
, and I would have tried to avoid it until giving up 😆
Usage
- Copy a YouTube embed from the share sheet.
- Paste that embed in an HTML or markdown file.
- Wrap it in a block-element container with the
data-responsive-youtube-container
selector. - Enjoy container-aware responsive videos!
It’ll look like this:
Embed responsively!
Bonus
This works with layouts as well. Around chan.dev, you’ll see this paired with a flex and grid layout.
Help me attribute this
If you know where I originally swiped this from, let me know so I can give the author credit here.
I’m @chantastic
on twitter and discord.
tweet:
Embed responsively 🍻
How I embed YouTube videos with range…
alt:
embed responsively! css snippet from chan.dev showing how to embed YouTube videos responsively