css - 为什么 iframe 默认是内联元素?

标签 css html iframe layout

iframe = 内联?

今天在调试一些布局问题时,我很惊讶地看到 <iframe>元素的默认显示属性为 inline .

对我来说这似乎很奇怪,尤其是考虑到您可以申请 heightwidth<iframe>浏览器尊重这一点,简单的内联元素不应如此。

那么谁能解释一下这背后的原因吗?

演示

HTML

<iframe id="test"></iframe>

CSS

alert($('#test').css('display'))

https://jsfiddle.net/0tdLr9pq/

谢谢!

最佳答案

因为 HTML4 规范是这样说的:

The IFRAME element allows authors to insert a frame within a block of text. Inserting an inline frame within a section of text is much like inserting an object via the OBJECT element: they both allow you to insert an HTML document in the middle of another, they may both be aligned with surrounding text, etc.

与周围文本对齐”部分意味着默认情况下它们不应该是 block 级的。

确实,通常内联元素会忽略 heightwidth 属性:

10.3.1 Inline, non-replaced elements

The width property does not apply.

10.6.1 Inline, non-replaced elements

The height property does not apply.

但是对于被替换的元素,如 iframe,情况并非如此。这在 10.3.2 中有解释。和 10.6.2部分。

关于css - 为什么 iframe 默认是内联元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28953472/

相关文章:

HTML-CSS : Styling <object>?

javascript - 使用 Javascript 和 CSS 生成博客文章

javascript - 嵌入谷歌地图

video - 无法用 html5 显示视频

jquery - 单击菜单外部时使用 jquery 关闭菜单

javascript - 用postMessage检查window.parent的起源有什么意义吗?

javascript - 如何使用对话框获得扩展权限

css - 导航栏以 FF 22 Chrome 27 为中心,但不是 IE 8

html - 如何获得宽度和高度完全相同的不同尺寸的图片墙?

javascript - 在移动设备上使用 javascript 检测屏幕密度?