Iframe 可嵌入外部网页,通过 src、width、height 等属性设置内容与尺寸,结合 title、sandbox、allowfullscreen 提升安全与体验,响应式布局需用 CSS 控制宽高比,但受 X-Frame-Options 限制,影响 SEO 与性能,需注意兼容性与安全性。
Iframe 是 HTML 中用于在当前页面中嵌入另一个网页的元素。通过使用 iframe,你可以在一个网页中显示来自其他网站或本地文件的内容,实现页面的嵌套展示。
其中:
示例代码:
法可以通过设置外层容器的宽高比,并结合 CSS 来自适应屏幕大小:
HTML:
CSS:
.iframe-container {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* 16:9 宽高比 */
}
.iframe-container iframe {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
基本上就这些。正确使用 iframe 能有效整合外部资源,但要注意安全和体验平衡。