Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Easy way to embed responsive YouTube iframe
Rogerio Orioli
Rogerio Orioli

Posted on • Edited on

Easy way to embed responsive YouTube iframe

Use aspect-ratio CSS property

The aspect-ratio CSS property allows you to define the desired width-to-height ratio of an element's box. This means that even if the parent container or viewport size changes, the browser will adjust the element's dimensions to maintain the specified width-to-height ratio. The specified aspect ratio is used in the calculation of auto sizes and some other layout functions.

1 . Create the css rule in your respective css file

 .aspect-video {     aspect-ratio: 16 / 9; }
Enter fullscreen modeExit fullscreen mode

2 . Add this css class to class attribute in iframe tag and change value of attribute width to width="100%"

if you use Tailwind CSS in your project exist the same class calledaspect-video.

See reference in link below
https://tailwindcss.com/docs/aspect-ratio#setting-the-aspect-ratio

 <iframe      width="100%"   src="https://www.youtube.com/embed/"    allow="accelerometer;    autoplay;    clipboard-write;    encrypted-media;    gyroscope;    picture-in-picture;   web-share fullscreen"    allowFullScreen></iframe>
Enter fullscreen modeExit fullscreen mode

REFERENCE :https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio
https://tailwindcss.com/docs/aspect-ratio#setting-the-aspect-ratio

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Paulistano Living in Floripa , Web Developer and Graphic Designer, in the short time skater and collector of Vinyl records. I love Music
  • Location
    Brasil - Florianopólis / SC
  • Work
    Rogerio Orioli at Javascript fullstack developer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp