发件人:Default width/height of an IFrame
Demo
“...我在 dev-tech-layout mailing list 上找到了答案——它是 CSS 规范的一部分。默认比例是 2:1...”
"...默认宽度的 300px 定义在 CSS 规范的最后一段,the width of inline replaced elements..."
否则,如果 'width' 的计算值为 'auto',但没有
满足上述条件,则'width'的使用值变为
300 像素。如果 300px 太宽而无法容纳设备,UA 应该使用
比例为 2:1 且适合
代替设备。
"...默认高度 150px 在 CSS 规范的最后一段中定义,the height of inline replaced elements..."
否则,如果 'height' 的计算值为 'auto',但没有
满足上述条件,则必须设置'height'的使用值
到具有 2:1 比例的最大矩形的高度,具有
高度不大于 150 像素,宽度不大于
设备宽度。
你没有给 iframe 指定高度,所以它在 IE 中取 iframe 的原始高度,试试下面
body {
display: flex;
margin: 0;
height: 100vh;
}
span {
background: green;
}
iframe {
background: tan;
height: 100vh; /* this is required to give it height in IE */
border:0; /* toavoid vertical scroll */
}
在这里
Demo
使用calc 表示高度,现代浏览器支持calc
css
body {
display: flex;
margin: 0;
height: calc(100vh - 50px);
flex-direction: column;
}
span {
background: green;
height:calc(100vh - 50px);
}
iframe {
background: tan;
height: calc(100vh - 50px); /* this is required to give it height */
border:0; /* to avoid vertical scroll */
}
header {
background: yellow;
height:50px;
}
main {
display: flex;
flex: 1;
}
Final Demo
使用一些 jquery 来实现同样的效果
jQuery
// If you put your code at the bottom of the page to avoid needing`$(document).ready`, it gets even simpler:
$(window).on('resize', function () {
var demoheight = $(window).height() - $('header').height();
$("body, iframe, span").css("height", demoheight);
}).trigger('resize');
// Another way to do that same thing
// $(document).ready(myfunction);
// $(window).on('resize', myfunction);
// function myfunction() {
// var demoheight = $(window).height() - $('header').height();
// $("body, iframe, span").css("height", demoheight);
// }
// Another technique is to`.trigger()`one event inside the other:
// $(window).on('resize', function () {
// var demoheight = $(window).height() - $('header').height();
// $("body, iframe, span").css("height", demoheight);
// });
// $(document).ready(function () {
// $(window).trigger('resize');
// });
CSS
body {
display: flex;
margin: 0;
flex-direction: column;
}
span {
background: green;
}
iframe {
background: tan;
border:0;
}
header {
background: yellow;
}
main {
display: flex;
flex: 1;
}