我有一个固定的高度容器,包含图像和iframe.为了使图像响应并防止垂直和水平溢出,我可以设置以下CSS:
- img {
- max-width: 100%;
- max-height: 100%;
- }
这可以确保纵向图像不会垂直溢出,并且横向图像不会水平溢出.
对于iframe,我使用“padding-ratio”technique,将包装元素的填充设置为iframe的宽高比(例如,16:9视频的56.25%):
- .iframe-wrapper {
- position: relative;
- height: 0;
- padding-top: 56.25%;
- overflow: hidden;
- }
- .iframe-wrapper iframe {
- position: absolute;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- }
虽然这意味着iframe与视口的宽度缩放,但是如果我更改视口的高度,则它不起作用.本质上,我想让iframe模仿图像的工作原理.
解决方法
对于我的用途(在敏感网站上嵌入Vimeo的视频),这在我测试的浏览器中效果很好:
- @media screen and (max-width: 750px) {
- iframe {
- max-width: 100% !important;
- width: auto !important;
- height: auto !important;
- }
- }
它不需要图像占位符,因此它要简单得多.