Doepud Web Design

Smart Websites for Small Businesses

Technical

Keeping the aspect ratio of YouTube videos with CSS

Nifty CSS hack for retaining the aspect ratio of video embeds

Experiments in fixed aspect ratios is a handy CSS technique from Stephanie Liu for displaying video embeds in a responsive website without losing their aspect ratio.

The HTML:

  <div class="container">
    <iframe class="embed"></iframe>
  </div>

The CSS:

  .container {
    position: relative;
    width: 100%;
    height: 0;
    padding-top: 56.25% // 9 / 16 * 100%
  }

  .embed {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
  }
Last updated: 13 Jun 2021

Popular articles

More Articles

We have high standards

Not just those we set ourselves but those recommended by the World Wide Web Consortium (W3C); in particular the Web Content Accessibility Guidelines (WCAG).

Like to work with us?

We're a tiny web design business based in the Highlands of Scotland, focused on crafting the finest websites. We've been doing this for a long time too.

Speak to us for free and impartial advice. Drop us a line on 0776 1100 661, we love talking about websites.

Working the web since 2003