Theme & Front End CSS Beginner

Make Video Embeds Responsive

Make YouTube and Vimeo iframes scale to the width of their container and keep a 16:9 shape on every screen.

Tested on WordPress 7.1 No plugin needed Updated
style.css 7 lines
iframe[src*="youtube.com"],
iframe[src*="youtube-nocookie.com"],
iframe[src*="player.vimeo.com"] {
	width: 100%;
	height: auto;
	aspect-ratio: 16 / 9;
}

Where to add this

Recommended Additional CSS Appearance → Customize → Additional CSS, or your page builder's global CSS.
Child theme At the end of your child theme's style.css file.
A code snippets plugin As a CSS snippet set to load on the front end.

Embedded videos often come with fixed widths and heights, so they overflow on phones or leave black bars. This CSS makes them fill their container and keep the right shape.

How it works

The selectors match iframes from YouTube, YouTube’s privacy-enhanced domain and Vimeo. Setting the width to 100% and the height to auto, then fixing the aspect-ratio to 16:9, lets the browser work out the height from the width at any screen size.

Things to watch

  • WordPress’s Embed block already does this when your theme supports responsive-embeds. This helps with embeds pasted as HTML, page builders and older themes.
  • For vertical videos like Shorts, add a separate rule with aspect-ratio: 9 / 16.

How to test it

Embed a YouTube video and resize the browser window, or view the page on a phone. The video should fit the width without black bars.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only