如果我在我的 HTML 中使用自定义或不受支持的元素,它们仍然可以被设置样式并且浏览器将呈现它们。
例如,Internet Explorer 11 及更早版本 (source) 不支持 HTML5 main
元素。当 main
被 IE 渲染时,涉及 margin
和 overflow
的 CSS 规则将被忽略。这意味着无法识别的元素的 display
值是 inline
。
在哪里定义了无法识别的元素的初始设置?
(注意:我不是在问使用自定义元素的优缺点。我只是想知道默认情况下 CSS 的作用。)
最佳答案
与其说是无法识别 元素,不如说是所有 元素。请记住,CSS 支持 XML 和 HTML。在 XML 中,所有元素都无法识别
在 CSS 2.1 规范中,section 6.1.1说:
6.1.1 Specified values
User agents must first assign a specified value to each property based on the following mechanisms (in order of precedence):
If the cascade results in a value, use it. Except that, if the value is 'inherit', the specified value is defined in “The 'inherit' value” below.
Otherwise, if the property is inherited and the element is not the root of the document tree, use the computed value of the parent element.
Otherwise use the property's initial value. The initial value of each property is indicated in the property's definition.
根据定义,用户代理样式表中不会提及无法识别的元素,并且由于我们讨论的是默认行为,作者样式表中也不会提及。所以 1 不适用。
显示属性在 9.2.4 The 'display' property 中定义.在那里的规则中,它说 Inherited: no
,所以 2 不适用。
所以 3 适用。同样根据 9.2.4 的规则,我们有 Initial: inline
,因此元素是内联的。
对于 HTML block 级元素,它们在默认情况下是 block 状的,因为它们在用户代理的样式表中如此列出。同样对于其他显示值,如 table
、list-item
等
关于css - 无法识别的 HTML 元素的默认设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35689456/