Skip to content

Responsive YouTube embed in Tailwind CSS with aspect-video

Published Updated 3 min read

On this page

Embedding YouTube videos into a responsive website can be a hassle. YouTube embeds are iframes, and an iframe has a fixed width and height. Give it w-full and the width follows the screen, but the height doesn’t, so the video ends up squashed or surrounded by black bars.

Easily embed Responsive YouTube Video with Tailwind CSS

YouTube still doesn’t scale its embeds for you. Tailwind CSS fixes it with one class: aspect-video.

Updated for Tailwind CSS v4. When I wrote this post in 2021 you needed the @tailwindcss/aspect-ratio plugin and its aspect-w-16 aspect-h-9 classes on a wrapper div. Since Tailwind CSS v3.0 the aspect-* utilities are part of the framework and use the native CSS aspect-ratio property, so you no longer need the plugin or the wrapper. The plugin’s own README says the same. The examples below are rewritten for v4.

The aspect ratio utilities#

Tailwind CSS v4 ships these aspect ratio classes:

ClassCSS
aspect-videoaspect-ratio: 16 / 9
aspect-squareaspect-ratio: 1 / 1
aspect-autoaspect-ratio: auto
aspect-<ratio>, e.g. aspect-9/16aspect-ratio: 9 / 16
aspect-[<value>]any value you write

Put the class on the iframe itself, together with w-full and h-auto. The width follows its container and the browser works out the height from the ratio.

Common use cases and aspect ratios#

YouTube embed with 16:9 ratio

This is probably why you are reading this article: a responsive YouTube embed. The code for the video above:

<iframe
  class="aspect-video h-auto w-full"
  src="https://www.youtube.com/embed/r9jwGansp1E"
  title="Mandolin Orange - Wildfire"
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
  allowfullscreen
  loading="lazy"></iframe>

Resize your browser window and the video keeps its shape. The title attribute tells screen reader users what the frame holds, and loading="lazy" keeps the player from loading until it gets near the screen.

Vimeo embed with 16:9 ratio

The same class works for Vimeo embeds.

<iframe
  class="aspect-video h-auto w-full"
  src="https://player.vimeo.com/video/146022717?title=0&byline=0&portrait=0"
  title="The Clock of the Long Now"
  allow="autoplay; fullscreen; picture-in-picture"
  allowfullscreen
  loading="lazy"></iframe>

YouTube embed with 9:16 ratio (vertical video)

Vertical video, the format of TikTok and YouTube Shorts, uses a 9:16 ratio. There is no named class for it, but v4 takes any ratio as a fraction: aspect-9/16. Cap the width, or a vertical video fills a desktop screen from top to bottom.

<iframe
  class="aspect-9/16 h-auto w-full max-w-xs"
  src="https://www.youtube.com/embed/UDIQwGb-4YQ"
  title="Vertical video example"
  allowfullscreen
  loading="lazy"></iframe>

YouTube embed with 1:1 ratio (square video)

Less common than the others, but square video works the same way with aspect-square.

<iframe
  class="aspect-square h-auto w-full max-w-md"
  src="https://www.youtube.com/embed/stNnNjM-Rhk"
  title="Square video example"
  allowfullscreen
  loading="lazy"></iframe>

A ratio of your own

For a ratio you use often, such as 4:3 for older footage, add it to your theme and you get a named class:

@import "tailwindcss";

@theme {
  --aspect-retro: 4 / 3;
}

Now aspect-retro works like aspect-video. For a one-off, aspect-[4/3] does the same without touching the theme.

Conclusion#

A responsive YouTube embed in Tailwind CSS is one class on the iframe: aspect-video, or aspect-9/16 and aspect-square for other formats. No plugin and no wrapper. If you host the video yourself instead of on YouTube, the video player in the free themes.dev UI library shows a poster with a play button and native controls.

Build faster with Tailwind CSS

Admin themes, website templates and landing pages. Almost all of it free.

Browse the library