paint-brush
A Guide on How to Embed YouTube Videos Responsively by@chantastic
111 reads

A Guide on How to Embed YouTube Videos Responsively

by chantasticJuly 14th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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 drug this snippet around the internet with me for years. I adapted it to use [AVO] CSS, my data attributes specification of [BEM]
featured image - A Guide on How to Embed YouTube Videos Responsively
chantastic HackerNoon profile picture
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 drug this snippet around the internet with me for years.
/* youtube embed wrapper */
[data-responsive-youtube-container] {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%;
  margin: 1em 0;
}

[data-responsive-youtube-container] > iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

I adapted it to use AVO 🥑 CSS, my data attributes specification of . 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

  1. Copy a YouTube embed from the share sheet.


  2. Paste that embed in an HTML or markdown file.


  3. Wrap it in a block-element container with the data-responsive-youtube-container selector.


  4. Enjoy container-aware responsive videos!


Embed responsively!

Bonus

This works with layouts as well. Around , 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.

바카라사이트 바카라사이트 온라인바카라