【问题标题】:How to flex images, iframes and video elements and maintain aspect ratio?如何弯曲图像、iframe 和视频元素并保持纵横比?
【发布时间】:2021-05-12 01:27:33
【问题描述】:

我有 5 个需要使用 flexbox 对齐的媒体项目(图像、视频和 iframe 中的 youtube 视频的混合)。当我删除视频和 iframe 时,一切正常。不知何故,当我添加视频/iframe 时,一切都被拉伸了,视频的宽度更大,视频元素的高度也很高。如何创建一行 5 个项目,高度和宽度都相同?

【问题讨论】:

    标签: html css video iframe flexbox


    【解决方案1】:

    这使得视频或图像所在的容器具有相等的宽度。子 div 中的视频/图像需要缩放以适应父宽度到 100% 的宽度。 html:

    <div class="parent">
       <div class="child" #video1>
         <!--place your image or video here, and make it use the whole width of the 'child' div-->
       </div>
       <div class="child" #image1>
          <!--place your image or video here, and make it use the whole width of the 'child' div-->
       </div>
    </div>
    

    css:

    .parent {
       display: flex;
       width:100%; // or something similar;
    }
    
    .child {
      width: 20%; // 20*5=100% of parent;
    }
    

    或者,如果您不想在小型设备上显示 5 个视频/图像,您可以在子设备上设置 min-width 并在父设备上使用 flex-wrap: wrap

    <div class="parent">
       <div class="child" #image1>
          <!--place your image or video here, and make it use the whole width of the 'child' div-->
       </div>
       <div class="child" #video1>
          <!--place your image or video here, and make it use the whole width of the 'child' div-->
       </div>
    </div>
    

    然后是css:

    .parent {
       display:flex;
       width:100vw;
       flew-wrap: wrap;
    }
    
    .child {
       width:20%;
       min-width: 200px;
    }
    

    【讨论】:

    • 谢谢!这是否保持两个 iframe 作为视频的纵横比?因为视频的纵横比是 4:3,而 iframe 的纵横比是 16:9,但它们必须具有相同的宽度和高度。对象覆盖(或类似的东西)应确保视频在其视图内缩放。
    猜你喜欢
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 2020-05-13
    相关资源
    最近更新 更多