Scale iFrame css width 100% like an image

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

    @Anachronist is closest here, @Simone not far off. The caveat with percentage padding on an element is that it's based on its parent's width, so if different to your container, the proportions will be off.

    The most reliable, simplest answer is:

    body {
      /* for demo */
      background: lightgray;
    }
    .fixed-aspect-wrapper {
      /* anything or nothing, it doesn't matter */
      width: 60%;
      /* only need if other rulesets give this padding */
      padding: 0;
    }
    .fixed-aspect-padder {
      height: 0;
      /* last padding dimension is (100 * height / width) of item to be scaled */
      padding: 0 0 56.25%;
      position: relative;
      /* only need next 2 rules if other rulesets change these */
      margin: 0;
      width: auto;
    }
    .whatever-needs-the-fixed-aspect {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      /* for demo */
      border: 0;
      background: white;
    }

提交回复
热议问题