iframe里放的视频,如何采用纯css适配

步骤1:设置包含iframe的父元素

首先,确保iframe的父容器具有一个适当的宽高比。通过为父容器设置一个相对定位和一定的宽度和高度,你可以控制它的尺寸。

html 复制代码
<div class="video-container">
    <iframe src="https://www.example.com" frameborder="0" allowfullscreen></iframe>
</div>

步骤2:应用CSS样式

接下来,使用CSS来确保iframe在父容器中保持宽高比并响应不同的屏幕尺寸。

css 复制代码
.video-container {
    position: relative;
    width: 100%;
    padding-bottom: 56.25%; /* 16:9 aspect ratio (height/width = 9/16 = 0.5625) */
    height: 0;
    overflow: hidden;
}

.video-container iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border: 0;
}

解释:

  1. .video-container
    • position: relative; 使得内部元素(即iframe)可以根据父容器定位。
    • padding-bottom: 56.25%; 这是16:9视频比例的常用宽高比(可以根据实际视频比例调整,例如4:3为75%)。
    • height: 0;padding-bottom配合使用,通过比例控制高度。
  2. .video-container iframe
    • position: absolute; 确保iframe相对于父容器的顶角定位。
    • width: 100%;height: 100%; 确保iframe填充整个父容器,从而适应不同的屏幕大小。

这样,无论是桌面端、平板还是手机,iframe里的视频都会根据容器的宽度自适应高度,保持比例不变。

相关推荐
小菜全6 小时前
uniapp基础组件概述
前端·css·vue.js·elementui·css3
心一信息8 小时前
ThreeJS骨骼示例
css·css3·html5
阿啄debugIT2 天前
装饰(Decorator)模式可以在不修改对象外观和功能的情况下添加或者删除对象功能
软件工程·1024程序员节
蔗理苦3 天前
2025-09-05 CSS4——浮动与定位
开发语言·前端·css·html·css3
蔗理苦3 天前
2025-09-05 CSS3——盒子模型
前端·css·css3
前端与小赵5 天前
渐变背景色和渐变字体颜色的实现方法
css·html·css3
Mo_jon5 天前
CSS 瀑布流图片简易实现
前端·css·css3
晓风残月淡6 天前
Mermaid流程图更改样式的方法
css·流程图·css3
星月前端6 天前
css3元素倒影效果属性:box-reflect
前端·css·css3
十一.3666 天前
171-178CSS3新增
前端·javascript·css3