Scale iFrame css width 100% like an image

前端 未结 6 1477
悲&欢浪女
悲&欢浪女 2020-12-04 08:21

I want to scale an iFrame through CSS to width: 100%, and the height should scale proportionally to the width.

With an tag this

6条回答
  •  青春惊慌失措
    2020-12-04 09:22

    I like this solution best. Simple, scalable, responsive. The idea here is to create a zero-height outer div with bottom padding set to the aspect ratio of the video. The iframe is scaled to 100% in both width and height, completely filling the outer container. The outer container automatically adjusts its height according to its width, and the iframe inside adjusts itself accordingly.

    The only variable here is the padding-bottom value in the outer div. It's 75% for 4:3 aspect ratio videos, and 56.25% for widescreen 16:9 aspect ratio videos.

提交回复
热议问题