html - iframe中body的高度与iframe相同

标签 html css iframe

我对 iframe 中 body 的高度有疑问。

body是一个 block 级元素,所以如果我不设置height,body什么都没有,body的高度就是0;

enter image description here

现在,我发现,当 body 在 iframe 中时,它的高度总是与 iframe 相同。(如果 iframe 有自己的高度)

例如: 一个html文件,iframe是空的,它的高度是500px .

iframe 中的 body 也是空的,它的高度是 500px也是。

为什么body是空的但是高度和iframe一样?

我看到 Quirks Mode在某些博客中,但它会出现在html5<!DOCTYPE html>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        iframe{
            width:500px;
            height:500px;
        }
    </style>
</head>

<body>
    <iframe src="" frameborder="0"></iframe>
</body>
<script>
</script>
</html> 

enter image description here

最佳答案

我之前也遇到过同样的问题,不过我可以回答你的问题。 Iframe 内的主体采用相同的高度,因为它继承了父级高度,即我们定义的任何 iframe 高度。例如,

<iframe width="100%" height="300px"></iframe>

所以 iframe 中 body 的高度应该是“300px”。

关于html - iframe中body的高度与iframe相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46147898/

相关文章:

javascript - iframe 中的当前页面重复 : iframe wont load

asp.net - 如何将 session 值传递给外部js文件

javascript - jquery-ui 可拖动小部件在滚动条上的 firefox 中不能正常工作

javascript - 搜索/过滤 json 函数需要编辑

html - Pandoc lua 过滤器 : How to preserve footnotes' formatting?

html - 在CSS中为两个div设置相同的高度

JavaScript 变量不在循环中更新

javascript - 如何在链接栏下隐藏和显示内容?

css - 当它是 block 按钮时带有图标标签问题的 Bootstrap 按钮

javascript - 使用 javascript/jquery 访问 iframe 的 html