Responsive video iframes (keeping aspect ratio) with only CSS?

后端 未结 4 1476
遥遥无期
遥遥无期 2020-12-08 02:31

I usually use a similar solution to this one. Something like:

.wrapper {
   position: relative;
   width: 100%;
   height: 0;
   padding-bottom: 56.25%;
}
.wr         


        
4条回答
  •  离开以前
    2020-12-08 03:10

    This is kind of hackish however you can use images to preserve the aspect ratio of a video. For example I went to paint and saved an image of 1280 x 720 resolution to use for a 16:9 aspect ratio (in here I will just grab a blank image off the internet somewhere).

    This works because if you change the width of an image while leaving height auto, vise-versa the image will automatically scale - keeping proportions.

    img {
      display: block;
      height: auto;
      width: 100%;
    }
    iframe {
      height: 100%;
      left: 0;
      position: absolute;
      top: 0;
      width: 100%;
    }
    .wrapper {
      float: left;
      position: relative;
    }

提交回复
热议问题