Scale iFrame css width 100% like an image

前端 未结 6 1456
悲&欢浪女
悲&欢浪女 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:03

    You could use viewport units here instead of %. Like this:

    iframe {
        max-width: 100vw;
        max-height: 56.25vw; /* height/width ratio = 315/560 = .5625 */
    }
    

    DEMO (Resize to see the effect)

    body {
      margin: 0;
    }
    .a {
      max-width: 560px;
      background: grey;
    }
    img {
      width: 100%;
      height: auto
    }
    iframe {
      max-width: 100vw;
      max-height: 56.25vw;
      /* 315/560 = .5625 */
    }

提交回复
热议问题