DEV Community

Cover image for πŸŽ₯ Maintain Aspect Ratio for Embedded YouTube Videos
Dzung Nguyen
Dzung Nguyen

Posted on

πŸŽ₯ Maintain Aspect Ratio for Embedded YouTube Videos

πŸ€” Have you ever embedded a YouTube video and noticed it looks weird on different screens? 🀯 <iframe> elements don’t automatically maintain their aspect ratio, which can break responsiveness.

βœ… Solution: Use the CSS aspect-ratio property!

<iframe style="aspect-ratio: 16 / 9; width: 100%;" src="https://www.youtube.com/embed/VIDEO_ID"></iframe>
Enter fullscreen mode Exit fullscreen mode

🎯 This keeps your video proportional and scales smoothly across all screen sizes! πŸš€


Follow me to stay updated with my future posts:

Top comments (0)